Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This can be done with pure CSS if you're willing to put up with some restrictions and cross-browser issues. However, it can be done easily and flexibly with Javascript. If you need a pure CSS solution, see the other answers.</p> <p>Set the div's width to (for example) 50%, use JavaScript to check its width, and then set the height accordingly. Here's a code example (I'm using jQuery, a JavaScript library):</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(function() { var div = $('#dynamicheight'); var width = div.width(); div.css('height', width); });</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#dynamicheight { width: 50%; /* Just for looks: */ background-color: cornflowerblue; margin: 25px; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="dynamicheight"&gt;&lt;/div&gt;</code></pre> </div> </div> </p> <p>If you want the box to scale with the browser window on resize, move the code to a function and call it on the window resize event. Here's a demonstration of that too (view example full screen and resize browser window):</p> <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false"> <div class="snippet-code snippet-currently-hidden"> <pre class="snippet-code-js lang-js prettyprint-override"><code>$(window).ready(updateHeight); $(window).resize(updateHeight); function updateHeight() { var div = $('#dynamicheight'); var width = div.width(); div.css('height', width); }</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>#dynamicheight { width: 50%; /* Just for looks: */ background-color: cornflowerblue; margin: 25px; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="dynamicheight"&gt;&lt;/div&gt;</code></pre> </div> </div> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload