Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To build on top of <a href="https://stackoverflow.com/questions/1303729/iframe-100-height-inside-body-with-padding/1305171#1305171">bobince's answer</a>:</p> <p><a href="http://erik.eae.net/" rel="nofollow noreferrer">Erik Arvidsson</a> came up with a way to (kinda, sorta) add <a href="http://webfx.eae.net/dhtml/boxsizing/boxsizing.html" rel="nofollow noreferrer">box-sizing support to IE6/IE7</a>. However, his solution doesn't support units other than <code>px</code>. Like you, I needed a percentage height, so I added support for percents.</p> <p>Once you've downloaded and unzipped the <a href="http://webfx.eae.net/dhtml/boxsizing/boxsizing.zip" rel="nofollow noreferrer">zip file</a>, open boxsizing.htc and replace the following border/padding functions:</p> <pre><code>/* border width getters */ function getBorderWidth(el, sSide) { if (el.currentStyle["border" + sSide + "Style"] == "none") return 0; var n = parseInt(el.currentStyle["border" + sSide + "Width"]); return n || 0; } function getBorderLeftWidth() { return getBorderWidth((arguments.length &gt; 0 ? arguments[0] : element), "Left"); } function getBorderRightWidth() { return getBorderWidth((arguments.length &gt; 0 ? arguments[0] : element), "Right"); } function getBorderTopWidth() { return getBorderWidth((arguments.length &gt; 0 ? arguments[0] : element), "Top"); } function getBorderBottomWidth() { return getBorderWidth((arguments.length &gt; 0 ? arguments[0] : element), "Bottom"); } /* end border width getters */ /* padding getters */ function getPadding(el, sSide) { var n = parseInt(el.currentStyle["padding" + sSide]); return n || 0; } function getPaddingLeft() { return getPadding((arguments.length &gt; 0 ? arguments[0] : element), "Left"); } function getPaddingRight() { return getPadding((arguments.length &gt; 0 ? arguments[0] : element), "Right"); } function getPaddingTop() { return getPadding((arguments.length &gt; 0 ? arguments[0] : element), "Top"); } function getPaddingBottom() { return getPadding((arguments.length &gt; 0 ? arguments[0] : element), "Bottom"); } /* end padding getters */ </code></pre> <p>Then replace <code>updateBorderBoxWidth</code> and <code>updateBorderBoxHeight</code> with the following:</p> <pre><code>function updateBorderBoxWidth() { element.runtimeStyle.width = ""; if (getDocumentBoxSizing() == getBoxSizing()) return; var csw = element.currentStyle.width; var w = null; if (csw != "auto" &amp;&amp; csw.indexOf("px") != -1) { w = parseInt(csw); } else if (csw != "auto" &amp;&amp; csw.indexOf("%") != -1) { var origDisplay = element.runtimeStyle.display; element.runtimeStyle.display = "none"; w = Math.max(0, (parseInt(element.parentNode.clientWidth) - ( getBorderLeftWidth(element.parentNode) + getPaddingLeft(element.parentNode) + getPaddingRight(element.parentNode) + getBorderRightWidth(element.parentNode) )) * (parseInt(csw) / 100)); element.runtimeStyle.display = origDisplay; } if (w !== null) { if (getBoxSizing() == "border-box") { setBorderBoxWidth(w); } else { setContentBoxWidth(w); } } } function updateBorderBoxHeight() { element.runtimeStyle.height = ""; if (getDocumentBoxSizing() == getBoxSizing()) return; var csh = element.currentStyle.height; var h = null; if (csh != "auto" &amp;&amp; csh.indexOf("px") != -1) { h = parseInt(csh); } else if (csh != "auto" &amp;&amp; csh.indexOf("%") != -1) { var origDisplay = element.runtimeStyle.display; element.runtimeStyle.display = "none"; h = Math.max(0, (parseInt(element.parentNode.clientHeight) - ( getBorderTopWidth(element.parentNode) + getPaddingTop(element.parentNode) + getPaddingBottom(element.parentNode) + getBorderBottomWidth(element.parentNode) )) * (parseInt(csh) / 100)); element.runtimeStyle.display = origDisplay; } if (h !== null) { if (getBoxSizing() == "border-box") { setBorderBoxHeight(h); } else { setContentBoxHeight(h); } } } </code></pre> <p>Then just use the file as you would otherwise:</p> <pre><code>.border-box { behavior: url("boxsizing.htc"); box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } </code></pre> <p>Here's a pretty thorough test I put together while developing my modifications:</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;box-sizing: border-box;&lt;/title&gt; &lt;style type="text/css"&gt; html, body { margin: 0; padding: 0; width: 100%; height: 100%; background: yellow; } body { padding-top: 50px; padding-bottom: 50px; } p { margin: 0; } #header, #footer { height: 50px; position: absolute; width: 100%; overflow: hidden; } #header { background: red; top: 0; } #footer { background: blue; bottom: 0; } #content { width: 100%; height: 100%; border: none; margin: 0; padding: 0; background: black; color: white; overflow: auto; position: relative; padding-top: 40px; padding-bottom: 40px; } #nested-header, #nested-footer { position: absolute; height: 40px; width: 100%; background: #CCC; } #nested-header { top: 0; } #nested-footer { bottom: 0; } #nested-content-wrap { height: 100%; } #nested-floater { height: 100%; float: left; width: 100px; } #nested-content { height: 100%; background: green; color: black; overflow: auto; position: relative; } #inner-nest { height: 100%; position: relative; } #inner-head { height: 30px; width: 100%; background: #AAA; position: absolute; top: 0; } #inner-content { padding-top: 30px; height: 100%; overflow: auto; } .border-box { behavior: url("boxsizing.htc"); box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } .content-box { behavior: url("boxsizing.htc"); box-sizing: content-box; -moz-box-sizing: content-box; -webkit-box-sizing: content-box; } legend { color: black; } form { margin: 1em 0; } .wrap { height: 100px; background: #000; overflow: hidden; } .test { width: 100px; height: 100%; background: #AAA; border-color: #EEE; padding-left: 20px; padding-top: 20px; padding-bottom: 5px; float: left; } .fill { width: 100%; height: 100%; background: #CCC; } .gauge { width: 99px; background: white; border-right: 1px solid green; height: 100%; float: left; } .notes { background: #8FC561; } .clear { clear: both; } /* 120px x 120px square; this will create a black 20px frame on the inside */ .boxtest-wrapper { width: 100px; height: 100px; float: left; background: black; color: white; margin: 1em; padding: 20px; } #boxtest-4-container { width: 100%; height: 100%; } .boxtest { width: 100%; height: 100%; background: white; color: black; border: 5px solid green; overflow: hidden; } &lt;/style&gt; &lt;script type="text/javascript"&gt; function addBorderBox() { var wrap1 = document.getElementById("wrap-1"); var wrap2 = document.getElementById("wrap-2"); var borderBox = document.createElement("div"); borderBox.className = "test border-box"; var borderBoxFill = document.createElement("div"); borderBoxFill.className = "fill"; var borderBoxContent = document.createTextNode("Generated border box fill"); borderBoxFill.appendChild(borderBoxContent); borderBox.appendChild(borderBoxFill); var gauge = document.createElement("div"); gauge.className = "gauge"; var gaugeText1 = "width: 100px"; var gaugeText2 = "height: 100%"; var gaugeText3 = "bottom should be visible"; gauge.appendChild(document.createTextNode(gaugeText1)); gauge.appendChild(document.createElement("br")); gauge.appendChild(document.createTextNode(gaugeText2)); gauge.appendChild(document.createElement("br")); gauge.appendChild(document.createTextNode(gaugeText3)); wrap1.appendChild(borderBox); wrap2.appendChild(gauge); } &lt;/script&gt; &lt;/head&gt; &lt;body id="body" class="border-box"&gt; &lt;div id="header"&gt; &lt;p&gt;Header - 50px;&lt;/p&gt; &lt;/div&gt; &lt;div id="content" class="border-box"&gt; &lt;div id="nested-header"&gt; &lt;p&gt;Nested Header - 40px;&lt;/p&gt; &lt;/div&gt; &lt;div id="nested-content-wrap"&gt; &lt;div id="nested-floater"&gt; &lt;p&gt;Float - 100px;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;This element should never scroll.&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;div id="nested-content"&gt; &lt;div id="inner-nest"&gt; &lt;div id="inner-head"&gt; &lt;p&gt;Inner Head - 30px;&lt;/p&gt; &lt;/div&gt; &lt;div id="inner-content" class="border-box"&gt; &lt;div style="float: right; "&gt; &lt;p&gt;The fourth square should look just like the other three:&lt;/p&gt; &lt;div id="boxtest-wrapper-1" class="boxtest-wrapper"&gt; &lt;div id="boxtest-1" class="boxtest border-box"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="boxtest-wrapper-2" class="boxtest-wrapper"&gt; &lt;div id="boxtest-2" class="boxtest border-box"&gt;&lt;/div&gt; &lt;/div&gt; &lt;br class="clear" /&gt; &lt;div id="boxtest-wrapper-3" class="boxtest-wrapper"&gt; &lt;div id="boxtest-3" class="boxtest border-box"&gt;&lt;/div&gt; &lt;/div&gt; &lt;div id="boxtest-wrapper-4" class="boxtest-wrapper"&gt; &lt;div id="boxtest-4-container"&gt; &lt;!-- boxtest-4-container isn't special in any way. it just has width and height set to 100%. --&gt; &lt;div id="boxtest-4" class="boxtest border-box"&gt;&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;p&gt;Inner Content - fluid&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The top of the scrollbar should be covered by the &amp;ldquo;Inner Head&amp;rdquo; element.&lt;/li&gt; &lt;li&gt;The bottom of the scrollbar should be visible without having to scroll &amp;ldquo;Inner Head&amp;rdquo; out of view.&lt;/li&gt; &lt;/ul&gt; &lt;p&gt;Document Compat Mode: &lt;strong id="compatMode"&gt; &lt;script type="text/javascript"&gt; var compatMode = document.compatMode; if (compatMode != "CSS1Compat") { document.getElementById("compatMode").style.color = "red"; } document.write(compatMode); &lt;/script&gt; &lt;/strong&gt; &lt;/p&gt;&lt;br /&gt; &lt;div class="notes"&gt; &lt;h2&gt;Notes&lt;/h2&gt; &lt;ul&gt; &lt;li&gt;In IE6 and IE7 (and possibly IE8; untested), you'll notice a slight shift of contents that have &lt;code&gt;box-sizing&lt;/code&gt; set to &lt;code&gt;border-box&lt;/code&gt;. This is the amount of time it takes for &lt;a href="box-sizing.htc"&gt;box-sizing.htc&lt;/a&gt; to finish downloading.&lt;/li&gt; &lt;li&gt;This workaround is not live. Anything that causes a &lt;a href="http://www.phpied.com/rendering-repaint-reflowrelayout-restyle/"&gt;reflow or repaint&lt;/a&gt; will not currently trigger an update to widths and heights of &lt;code&gt;border-box&lt;/code&gt; elements.&lt;/li&gt; &lt;li&gt;See &lt;a href="http://webfx.eae.net/dhtml/boxsizing/boxsizing.html"&gt;http://webfx.eae.net/dhtml/boxsizing/boxsizing.html&lt;/a&gt; for the original solution to the IE6/IE7 &lt;code&gt;border-box&lt;/code&gt; problem. &lt;a href="box-sizing.htc"&gt;box-sizing.htc&lt;/a&gt; has been modified to allow for percentage widths and heights.&lt;/li&gt; &lt;li&gt;To see what this example should look like without the use of &lt;a href="box-sizing.htc"&gt;box-sizing.htc&lt;/a&gt;, view it in Firefox or IE8.&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;br class="clear" /&gt; &lt;form&gt; &lt;fieldset&gt; &lt;legend&gt;DOM Update Test&lt;/legend&gt; &lt;input type="button" value="Click to add border-box" onclick="addBorderBox(); " /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;div id="wrap-1" class="wrap"&gt; &lt;div class="test content-box" id="content-box-1" style="border-width: 5px; border-style: solid;"&gt; &lt;div class="fill"&gt;Content box fill&lt;/div&gt; &lt;/div&gt; &lt;div class="test content-box" id="content-box-2" style="border-width: 5px; border-style: solid; padding: 5px;"&gt; &lt;div class="fill"&gt;Content box fill&lt;/div&gt; &lt;/div&gt; &lt;div class="test border-box" id="border-box-1" style="border-width: 5px; border-style: solid;"&gt; &lt;div class="fill"&gt;Border box fill&lt;/div&gt; &lt;/div&gt; &lt;div class="test border-box" id="border-box-2" style="border-width: 5px; border-style: solid; padding: 5px;"&gt; &lt;div class="fill"&gt;Border box fill&lt;/div&gt; &lt;/div&gt; &lt;div class="test" id="default-box-1" style="border-width: 5px; border-style: solid;"&gt; &lt;div class="fill"&gt;Default box fill&lt;/div&gt; &lt;/div&gt; &lt;div class="test" id="default-box-2" style="border-width: 5px; border-style: solid; padding: 5px;"&gt; &lt;div class="fill"&gt;Default box fill&lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="wrap-2" class="wrap"&gt; &lt;!-- subtract 1 from width for 1px right border --&gt; &lt;div class="gauge" style="width: 129px;"&gt;width: 130px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be cut off&lt;/div&gt; &lt;div class="gauge" style="width: 119px;"&gt;width: 120px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be cut off&lt;/div&gt; &lt;div class="gauge"&gt;width: 100px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be visible&lt;/div&gt; &lt;div class="gauge"&gt;width: 100px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be visible&lt;/div&gt; &lt;div class="gauge" style="width: 129px;"&gt;width: 130px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be cut off&lt;/div&gt; &lt;div class="gauge" style="width: 119px;"&gt;width: 120px&lt;br /&gt;height: 100%&lt;br /&gt;bottom should be cut off&lt;/div&gt; &lt;/div&gt; &lt;br class="clear" /&gt; &lt;script type="text/javascript"&gt; var lipsum = "&lt;p&gt;Lorem ipsum dolor sit amet.&lt;/p&gt;"; for (var i = 0; i &lt; 100; i++) { document.write(lipsum); } &lt;/script&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="nested-footer"&gt; &lt;p&gt;Nested Footer - 40px;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="footer"&gt; &lt;p&gt;Footer - 50px;&lt;/p&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

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