When I set display: flex
on my <a>
, it looks just like I want:
.container {
display: flex;
box-sizing: content-box;
flex-direction: row;
align-items: center;
border: 1px solid #333;
background: white;
}
.firstchild {
display: flex;
flex-direction: row;
align-items: center;
margin-right: 15px;
}
.firstgrandchild {
display: inline-block;
}
<a class="container">
<span class="first">
<span class="firstchild">First - child</span>
</span>
<p class="second">Second</p>
</a>
Turning on Firefox's flexbox highlighter shows that it renders like I want: two items separated by a small margin, and the container growing to the full width on the right side:
However, if I now change the <a>
to be a <button>
, it suddenly collapses, and the first grand child breaks out of the first child:
.container {
display: flex;
box-sizing: content-box;
flex-direction: row;
align-items: center;
border: 1px solid #333;
background: white;
}
.firstchild {
display: flex;
flex-direction: row;
align-items: center;
margin-right: 15px;
}
.firstgrandchild {
display: inline-block;
}
<button class="container">
<span class="firstchild">
<span class="firstgrandchild">First child - first grand child</span>
</span>
<p class="secondchild">Second child</p>
</button>
Why does this happen? What styling do I need to add to the button to make it render the same as the anchor?