Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The trick is to understand what the 100% is taken of. Reading CSS specs can help you there.</p> <p>To make a long story short - there is such a thing as "containing block" - which is not necessary the parent element. Simply said, it is the first element up the hierarchy that has position:relative or position:absolute. Or the body element itself if there is nothing else. So, when you say "width: 100%", it checks the width of the "containing block" and sets the width of your element to the same size. If there was something else there, then you might get contents of a "containing block" that are larger than itself (thus "overflowing").</p> <p>Height works the same way. <s>With one exception. You can't get height to 100% of the browser window. The very top level element, against which 100% can be calculated, is the body (or html? not sure) element, and that stretches just enough to contain its contents. Specifying height:100% on it will have no effect, because it has no "parent element" against which to measure 100%. Window itself doesn't count. ;)</p> <p>To make something stretch exactly 100% of the window, you have two choices:<ol> <li>Use JavaScript</li> <li>Don't use DOCTYPE. This is not a good practice, but it puts the browsers in "quirks mode", in which you can do height="100%" on elements and it will stretch them to the window size. Do note, that the rest of your page will probably have to be changed too to accommodate for the DOCTYPE changes.</ol></s></p> <p><strong>Update:</strong> I'm not sure if I wasn't wrong already when I posted this, but this certainly is outdated now. Today you can do this in your stylesheet: <code>html, body { height: 100% }</code> and it will actually stretch to the whole of your viewport. Even with a DOCTYPE. <code>min-height: 100%</code> could also be useful, depending on your situation.</p> <p>And I <strong>wouldn't advise anyone to make a quirks-mode document</strong> anymore either, because it causes way more headaches than solves them. Every browser has a different quirks-mode, so getting your page to look consistently across browsers becomes two orders of magnitude more difficult. Use a DOCTYPE. Always. Preferably the HTML5 one - <code>&lt;!DOCTYPE html&gt;</code>. It's easy to remember and works like a charm in all browsers, even the 10 years old ones.</p> <p>The only exception is when you have to support something like IE5 or something. If you're there, then you're on your own anyway. Those ancient browsers are nothing like the browsers today, and little advice that is given here will help you with them. On the bright side, if you're there, you probably just have to support ONE kind of browser, which gets rid of the compatibility problems.</p> <p>Good luck!</p> <p><strong>Update 2:</strong> Hey, it's been a long time! 6 years later, new options are on the scene. I just had a discussion in the comments below, here are more tricks for you that work in today's browsers.</p> <p><strong>Option 1</strong> - absolute positioning. Nice and clean for when you know the precise height of the first part.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-css lang-css prettyprint-override"><code>body, html {width: 100%; height: 100%; margin: 0; padding: 0} .first-row {position: absolute;top: 0; left: 0; right: 0; height: 100px; background-color: lime;} .second-row {position: absolute; top: 100px; left: 0; right: 0; bottom: 0; background-color: red } .second-row iframe {display: block; width: 100%; height: 100%; border: none;}</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="first-row"&gt; &lt;p&gt;Some text&lt;/p&gt; &lt;p&gt;And some more text&lt;/p&gt; &lt;/div&gt; &lt;div class="second-row"&gt; &lt;iframe src="https://jsfiddle.net/about"&gt;&lt;/iframe&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>Some notes - the <code>second-row</code> container is needed because <code>bottom: 0</code> and <code>right: 0</code> doesn't work on iframes for some reason. Something to do with in being a "replaced" element. But <code>width: 100%</code> and <code>height: 100%</code> works just fine. <code>display: block</code> is needed because it's an <code>inline</code> element by default and whitespace starts creating weird overflows otherwise.</p> <p><strong>Option 2</strong> - tables. Works when you don't know the height of the first part. You can use either actual <code>&lt;table&gt;</code> tags or do it the fancy way with <code>display: table</code>. I'll go for the latter because it seems to be in fashion these days.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-css lang-css prettyprint-override"><code>body, html {width: 100%; height: 100%; margin: 0; padding: 0} .row-container {display: table; empty-cells: show; border-collapse: collapse; width: 100%; height: 100%;} .first-row {display: table-row; overflow: auto; background-color: lime;} .second-row {display: table-row; height: 100%; background-color: red; overflow: hidden } .second-row iframe {width: 100%; height: 100%; border: none; margin: 0; padding: 0; display: block;}</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="row-container"&gt; &lt;div class="first-row"&gt; &lt;p&gt;Some text&lt;/p&gt; &lt;p&gt;And some more text&lt;/p&gt; &lt;/div&gt; &lt;div class="second-row"&gt; &lt;iframe src="https://jsfiddle.net/about"&gt;&lt;/iframe&gt; &lt;/div&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>Some notes - the <code>overflow: auto</code> makes sure that the row always includes all of its contents. Otherwise floating elements can sometimes overflow. The <code>height: 100%</code> on the second row makes sure it expands as much as it can squeezing the first row as small as it gets.</p> <p><strong>Option 3</strong> - flexbox. The cleanest one of them all, but with a less than stellar browser support. IE10 will need <code>-ms-</code> prefixes for the flexbox properties, and anything less will not support it at all.</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-css lang-css prettyprint-override"><code>body, html {width: 100%; height: 100%; margin: 0; padding: 0} .row-container {display: flex; width: 100%; height: 100%; flex-direction: column; background-color: blue; overflow: hidden;} .first-row {background-color: lime; } .second-row { flex-grow: 1; border: none; margin: 0; padding: 0; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="row-container"&gt; &lt;div class="first-row"&gt; &lt;p&gt;Some text&lt;/p&gt; &lt;p&gt;And some more text&lt;/p&gt; &lt;/div&gt; &lt;iframe src="https://jsfiddle.net/about" class="second-row"&gt;&lt;/iframe&gt; &lt;/div&gt;</code></pre> </div> </div> </p> <p>Some notes - the <code>overflow: hidden</code> is because the iframe still generates some sort of overflow even with <code>display: block</code> in this case. It isn't visible in the fullscreen view or the snippet editor, but the small preview window gets an extra scrollbar. No idea what that is, iframes are weird.</p>
 

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