Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make flex children inline-block [duplicate]

Tags:

html

css

flexbox

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

like image 417
Joshua Leung Avatar asked Nov 16 '17 00:11

Joshua Leung


People also ask

Does Flex work on inline elements?

In particular, the flex items themselves always behave like block-level boxes (although they do have some properties of inline-blocks). You cannot display flex items inline; otherwise you don't actually have a flex layout.

What is the difference between display flex and display inline block?

The main difference between display: flex and display: inline-flex is that display: inline-flex will make the flex container an inline element while its content maintains its flexbox properties. In the below picture, the flex container comprises Computer, Science, Portal, and the yellow area. Example: html.

What is inline-flex in HTML?

inline-flex. Displays an element as an inline-level flex container. inline-grid. Displays an element as an inline-level grid container. inline-table.


1 Answers

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

like image 183
Michael Benjamin Avatar answered Sep 22 '22 13:09

Michael Benjamin