问题
I would like to have the flex children rendered inline-block
so that border-bottom
is under the li
's width instead of the container's width.
Apparently flex children can't be set as inline-block?
Is there a workaround to this problem?
.menu {
display: flex;
flex-flow: column;
list-style: none;
padding: 0;
}
.menu > li {
display: inline-block;
margin-bottom: 1rem;
border-bottom: 3px solid black;
}
<ul class="menu">
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
https://codepen.io/joshuajazleung/pen/EbwZmJ
回答1:
Add align-items: flex-start
to the container:
.menu {
display: flex;
flex-flow: column;
align-items: flex-start; /* NEW */
list-style: none;
padding: 0;
}
.menu > li {
margin-bottom: 1rem;
border-bottom: 3px solid black;
}
<ul class="menu">
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
OR... switch from display: flex
to display: inline-flex
.
.menu {
display: inline-flex;
flex-flow: column;
list-style: none;
padding: 0;
}
.menu > li {
margin-bottom: 1rem;
border-bottom: 3px solid black;
}
<ul class="menu">
<li>Tab 1</li>
<li>Tab 2</li>
<li>Tab 3</li>
</ul>
More details here: Make flex items take content width, not width of parent container
来源:https://stackoverflow.com/questions/47319794/make-flex-children-inline-block