Computer >> Computer tutorials >  >> Programming >> Javascript

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


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>