I have a reasonable understanding of css, the box-model, and positioning in general.
But there is one mystery I've never grokked, nor have I been able to google my way to an understanding of it on the many times it has slowed down my implementation of a layout:
The mystery of the ul inside a positioned div
Why must I set the ul
font-size
to 0, and line-height
to 1, and then reset them back to what I want them to be in the li
in order to get the ul
to be the right height and (appear to) be contained in the div
?
Where does the mysterious extra height come from if I set the ul
line-height
to the nominal line height I want? Why is this extra height different on every browser?
There are many other ways to end up with mysterious heights and offsets, but I feel if I understood these cases I would have a sufficient model.
Does anyone possess a mental model that will allow me to predict these behaviours? (and thus hopefully remember them, so that I don't waste time re-figuring it out every 6 months)?
Here is a playground demonstrating what I seek to understand.
section {
background-color: gray;
width: 500px;
height: 700px;
padding-top: 10px;
font-size: 12px;
line-height: 25px;
}
.reference {
background-color: aquamarine;
height: 25px;
width: 75px;
float: left;
padding: 0;
margin: 0;
}
.container {
padding: 0;
margin: 0;
background-color: white;
height: 25px;
width: 423px;
}
ul {
list-style-type: none;
background-color: rgba(255, 50, 50, 0.25);
}
li {
display: inline;
}
.case-one {
display: inline-block;
}
.reference-two {
position: absolute;
top: 130px;
}
.reference-two-b {
position: absolute;
top: 200px;
}
.case-two {
position: absolute;
left: 85px;
}
.case-two-a {
top: 130px;
}
.case-two-b {
top: 200px;
}
.case-two ul {
position: relative;
font-size: 0px;
}
.case-two-a ul {
line-height: 1;
}
.case-two-b ul {
line-height: 25px;
}
.case-two li {
display: inline;
font-size: 12px;
line-height: 25px;
}
<section>
<div class="container case-one">this I fully understand</div>
<div class="reference">case one</div>
<br/>
<br/>
<div class="container case-one">
<div style="background-color: rgba(50,50,200,0.4); width:8px;height: 12px; position: absolute;"></div>
<ul>
<li>but</li>
<li>not this.</li>
<li>why is the ul offset?</li>
<li>why by font-size, not line-height?</li>
</ul>
</div>
<div class="reference">case one b</div>
<div class="container case-two case-two-a">
<ul>
<li>why does</li>
<li>setting the ul font-size to 0 work?</li>
</ul>
<div style="line-height: 1.2;">also, why does the ul line-height have to be set to 1? If it is set to 25px, the ul grows, see below</div>
</div>
<div class="reference reference-two">case two</div>
<div class="container case-two case-two-b">
<div style="background-color: rgba(50,50,200,0.4);left:4px;width:8px; height: 30px; position: absolute;"></div>
<div style="background-color: rgba(50,200,50,0.4);width:8px; height: 29px; position: absolute;left:16px;"></div>
<div style="background-color: rgba(200,50,50,0.4);width:8px; height: 28px; position: absolute;left:28px;"></div>
<ul>
<li>why does</li>
<li>setting the ul line-height to 25px result in it being 30px high?</li>
</ul>
<div style="line-height: 1.4;margin-top: 8px;">where does the 5px come from?? Why is it 4px on chrome? Why is it 3px on firefox? Anyone else get any other heights, lol?</div>
</div>
<div class="reference reference-two-b">case two</div>
</section>