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>

Updated on: 23-Jun-2020

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements