17

I want the flex items to take full height, but the content inside them to be vertically centred.

justify-content: centre doesn't work, and align-self: centre on the item itself shrinks its height to its own content, while I want all items to be the same height.

In this example I want the numbers to be vertically centred: http://codepen.io/ilyador/pen/ogYbWO?editors=110

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: flex;  
}

.flex-item {
  flex: 1 1;
  background: tomato;
  padding: 5px;
  margin-top: 10px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
  border: solid 1px red;
}
<ul class="flex-container">
  <li class="flex-item">1fbdms s s sdj dfkg kjfg dkfj gdfjkgdfkj gdfkjg dfkjgdkhdfjk gkjdfkjdfgdfg jkdfgdfjkgk </li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
</ul>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
ilyo
  • 35,851
  • 46
  • 106
  • 159
  • Just wondering...why do you need the items to have 100% height? – Danield Jan 01 '15 at 10:04
  • I need all the items to be the same height as the tallest one – ilyo Jan 01 '15 at 10:06
  • *Why* do you need them to be the same height as the tallest? You want to *center* them - don't you? – Danield Jan 01 '15 at 10:08
  • What are you suggesting? – ilyo Jan 01 '15 at 10:09
  • Something like this: http://codepen.io/danield770/pen/ByQjJL – Danield Jan 01 '15 at 10:17
  • Thanx but no, I need each to have it's own background. – ilyo Jan 01 '15 at 10:19
  • 1
    Ok, that's what I was asking all along.... you need the items to take 100% height because each on e has their own background. – Danield Jan 01 '15 at 10:25
  • 1
    You need to use nested flexboxes: http://stackoverflow.com/a/32828900/3597276 – Michael Benjamin Oct 10 '15 at 01:51
  • @Michael_B that is what I was hoping to avoid but apparently could not... Table-cells are much easier in this way – Andrey Jul 13 '16 at 16:25
  • 1
    @Andrey, nesting flex containers is not really a big deal. And you keep the ability to use flex properties, which provides more flexibility than tables. – Michael Benjamin Jul 13 '16 at 16:32
  • @Michael_B oh, seems I was wrong — you just need ```display: flex``` on inner elements to center their content, but inside them you do not need to add extra inner wrapper element with ```display: flex``` just to make a centering, as I originally thought. In other words — you always have an element to put ```display: flex``` on – Andrey Jul 13 '16 at 16:55
  • 1
    @Andrey, you are correct. You do not need to add a wrapper to content because CSS automatically creates [*anonymous boxes*](https://www.w3.org/TR/css-flexbox-1/#flex-items). For more details about centering *the content of a flex item* see [**here**](http://stackoverflow.com/a/33049198/3597276) and [**here**](http://stackoverflow.com/a/37844240/3597276). Cheers! – Michael Benjamin Jul 13 '16 at 17:02

2 Answers2

18

I was able to accomplish vertical centering of your numbers with this:

.flex-item {
  display:flex;
  flex-direction:column;
  justify-content:space-around;
}

If you want something to be vertically centered, set the container to display:flex and then use justify-content to accomplish it. With justify-content you could either set it to space-around or to center. Either will accomplish your goal.

http://codepen.io/anon/pen/RNoajg

paceaux
  • 1,762
  • 1
  • 16
  • 24
8

Give the flex items display: flex and align-items: center. The flex items contents will now be vertically centered.

body {
  display: flex;
  height: 100vh;
  margin: 0;
}
body > div {
  flex: 1;
  display: flex;
  align-items: center;
  background: #F00;
}
body > div:nth-child(odd) {
  background: #F90;
}
<div>
  <p>This</p>
</div>
<div>
  <p>text</p>
</div>
<div>
  <p>is</p>
</div>
<div>
  <p>vertically</p>
</div>
<div>
  <p>centered</p>
</div>
misterManSam
  • 24,303
  • 11
  • 69
  • 89