Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would margin not be contained by parent element?
    primarykey
    data
    text
    <p>When an element with a margin is contained within another element, the parent does not consistently wrap that margin.</p> <p>Many things will cause the parent to wrap the child's margin:</p> <ul> <li>border:solid;</li> <li>position:absolute;</li> <li>display:inline-block;</li> <li>overflow:auto</li> </ul> <p>(And this just from small testing, no doubt there is more.)</p> <p>I would assume this has to do with collapsing margins, but:</p> <ol> <li>The W3C spec page has no description of such behavior.</li> <li>There is no overlapping margins here.</li> <li>Behavior of all browsers seems to be consistent on this issue.</li> <li>The behavior is affected by triggers that are not related to the margins </li> </ol> <p>What is the logic by which an element which defaults to overflow:auto should contain different material than the one where the overflow is set to auto.</p> <p>Why should everything but the default behavior of a regular div assume that the margin is contained by the parent - and why should the regular default not include the margin?</p> <p>EDIT: The final answer is that the W3C really does specify this behavior, but that</p> <ul> <li>The specs do not really make any sense.</li> <li><a href="http://blog.siteroller.net/collapsed-margins-and-free-margins" rel="noreferrer">The specs mix, without any word of explanation</a>: <ul> <li>'free margins' (margins that would touch the top or bottom of their parent are not contained by the parent) and</li> <li>'collapsed margins' (adjacent margins are allowed to overlap).</li> </ul></li> </ul> <p>Demo:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Margins overextending themselves&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html;charset=utf-8" &gt; &lt;style type="text/css"&gt; body{ margin:0; } div.b{ background-color:green; } div.ib{ display:inline-block; background-color:red; } div.pa{ background-color:yellow; position:absolute; bottom:0; right:0; } div.oa{ background-color:magenta; overflow:auto; } div.brdr{ background-color:pink; border:solid; } h1{margin:100px; width:250px; border:solid;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="b"&gt; &lt;h1&gt;Is the margin contained?&lt;/h1&gt; &lt;/div&gt; &lt;div class="ib"&gt; &lt;h1&gt;Is the margin contained?&lt;/h1&gt; &lt;/div&gt; &lt;div class="pa"&gt; &lt;h1&gt;Is the margin contained?&lt;/h1&gt; &lt;/div&gt; &lt;div class="oa"&gt; &lt;h1&gt;Is the margin contained?&lt;/h1&gt; &lt;/div&gt; &lt;div class="brdr"&gt; &lt;h1&gt;Is the margin contained?&lt;/h1&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;` </code></pre>
    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.
 

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