How to set height equal to the dynamic width (CSS fluid layout) in JavaScript?

JavascriptObject Oriented ProgrammingFront End Technology

To set height equal to the dynamic width in JavaScript, you can try to run the following code −

Example

<!DOCTYPE html>
<html>
   <head>
      <style>
         .wrap {
            width:400px;
            height:300px;
            border:2px solid yellow;
         }
         .myChild{
            width: 80%;
            border:1px solid red;
         }
      </style>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
      </script>
      <script>
         var res = $('.myChild').width();
         $('.myChild').css({
            'height': res + 'px'
         });
      </script>
   </head>
   <body>
      <div class = "wrap">
         <div class = "myChild"></div>
      </div>
   </body>
</html>
raja
Published on 27-Feb-2018 13:13:51
Advertisements