Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline block in a flexbox in the column direction

How do I make C, D, E which have a display: inline-block occupy only as much space as they need to fit the text inside of them and can appear next to each another (side by side in a row) in a flexbox which has a flex-direction set to column? Please note that I do not want to wrap C, D, E in a container to get the desired result

body {
  padding: 0;
  margin: 0;
}

.container {
  display: flex;
  flex-direction: column;
}

.a,.b,.c,.d,.e {
  height: 50px;
  line-height: 50px;
  border: 1px solid;
  text-align: center;
}

.c,.d,.e {
  display: inline-block;
}

.a {
  background: cyan;
}

.b {
  background: yellow;
}

.c {
  background: orange;
}

.d {
  background: gray;
}

.e {
  background: pink;
}
<div class="container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
</div>
like image 663
takeradi Avatar asked Oct 26 '16 09:10

takeradi


People also ask

Does flexbox work with inline elements?

With inline-flex or inline-grid , you have all the power of a flexbox or grid layout system within a block that lays out in the inline direction.

How do I move an inline-block to the right?

You can use Flexbox instead and set margin-left: auto on inner div to move it to right. Save this answer.

When the flex-direction is column reverse?

The flex container's main-axis is the same as the block-axis. The main-start and main-end points are the same as the before and after points of the writing-mode. column-reverse. Behaves the same as column but the main-start and main-end are opposite to the content direction.


1 Answers

You can do that using flexbox properties itslef - instead of inline-block use align-self: center (or flex-start as you see fit)

.c,.d,.e {
  align-self: center;
}

See demo below:

body {
  padding: 0;
  margin: 0;
}

.container {
  display: flex;
  flex-direction: column;
}

.a,.b,.c,.d,.e {
  height: 50px;
  line-height: 50px;
  border: 1px solid;
  text-align: center;
}

.c,.d,.e {
  align-self: center;
}

.a {
  background: cyan;
}

.b {
  background: yellow;
}

.c {
  background: orange;
}

.d {
  background: gray;
}

.e {
  background: pink;
}
<div class="container">
  <div class="a">A</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="d">D</div>
  <div class="e">E</div>
</div>
like image 131
kukkuz Avatar answered Oct 06 '22 00:10

kukkuz