I'm making a CSS based burger menu button, and I noticed that the <span>
bars are vertically centered by default. Inspecting and playing around with the bars' margin keeps the elements vertically centered.
But if I try the same code and replace the <button>
with a <div>
, it's no more vertically aligned.
.toggle-btn {
display: block;
background: #eee;
border: none;
width: 50px;
height: 50px;
padding: 0;
margin: 20px auto 30px;
}
.toggle-btn .bar {
display: block;
background: #000;
width: 24px;
height: 3px;
margin: 0 auto;
}
.toggle-btn .bar + .bar {
margin-top: 5px;
}
p {
margin: 0;
text-align: center;
}
<p>this is a button</p>
<button class="toggle-btn">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</button>
<p>this is a div</p>
<div class="toggle-btn">
<span class="bar"></span>
<span class="bar"></span>
<span class="bar"></span>
</div>
This is completely new to me, and there doesn't seem to be any browser default CSS that seems to cause this. I feel like I'm missing something obvious.
I'm curious on how this happens. What causes the bars inside a <button>
to be vertically centered?