I've got a few questions about position: absolute centered elements. I was looking on almost every position absolute articles, but didn't find something that would explain me this strange behaviour that I am now asking about. Here is codepen that relates to the questions(expect third question): https://codepen.io/anon/pen/LQrjzz
- Why does div jump down, when changed div width units from px to percentage(40px = 12.2%) How can I fix this, if I want to use percentage?
- Why does div jump down, when resizing a window even when there is still space(https://i.imgur.com/4DPu9Vp.gifv)? How can I fix this?
- Why in this snippet second div's height = 0? I've expected its height to be 50px as width.
main{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#first{
width: 100px;
height: 100px;
background: black;
}
#second{
width: 50%;
height: 50%;
background: aqua;
}
<main>
<div id="first"></div>
<div id="second"></div>
</main>
//code from codepen
HTML:
<main>
<h1>Hello World</h1>
<p>Lorem ipsum dolor</p>
<div></div>
</main>
CSS:
main{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 2px red solid;
padding: 10px;
}
h1{
font-size: 32px;
width: 10em;
border: 2px black dotted;
}
p{
font-size: 32px;
width: 10em;
border: 2px green dotted;
display: inline-block;
vertical-align: middle;
}
div{
display: inline-block;
width: 40px; /* 12.2% */
height: 40px;
background: lawngreen;
vertical-align: middle;
border: 2px blue dotted;
}
I hope those questions weren't dumb. Thank you very much.