-2

I'm trying to center my text vertically in my divs, but I'm using display: flex so I can't use display: table.

What's the best way to vertically center the text here, ideally without adding lots of html elements (hoping to keep the 'thing' divs as clean as possible)?

Fiddle

.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
}

.thing {
  background: red;
  color: white;
  margin: 5px;
  text-align: center;
  display: table;
  flex: 1 0 30%;
  vertical-align: middle;
}
<div class="container">
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
</div>
tacoshy
  • 10,642
  • 5
  • 17
  • 34
user4893295
  • 533
  • 6
  • 25

3 Answers3

2

justify-content: center; and align-items: center; on flex containers will force children to vertically and horizontally center. Add these rules to .thing and you'll get your desired results.

.container {
    height: 100vh;
    width: 100vw;
    display: flex;
    flex-wrap: wrap;
}
.thing {
    background: red;
    color:white;
    margin: 5px;
    text-align: center;
    display: flex; /* create flex container */
    justify-content: center; /* center horizontally */
    align-items: center; /* center vertically */
    flex: 1 0 30%;
    vertical-align: middle;
}
<div class="container">
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
</div>
Lynel Hudson
  • 2,335
  • 1
  • 18
  • 34
1

.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
}

.thing {
  background: #f00;
  color: #fff;
  margin: 5px;
  text-align: center;
  display: table;
  flex: 1 0 30%;
  display: flex;
  align-items: center;
  justify-content: center;
  vertical-align: middle;
}
<div class="container">
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
</div>

This is the most common method of centering text with CSS. To center the text inside each box, we make each containing box a flex container. Then set align-items to center to perform centering on the block axis, and justify-content to center to perform centering on the inline axis.

mrwolferinc
  • 143
  • 5
  • Ok i will change it – mrwolferinc Jun 19 '21 at 18:46
  • revoked the down-vote after the edit. However you still have a mistake in there. Its not `block-axis` and `inline-axis`. Those terms makes also no sense at all. Its `main-axis` (justify-content) and `cross-axis` (align.-items). Those axis depend on the flex-direction. The main-axis is always the same direction as the flex-direction. – tacoshy Jun 19 '21 at 21:24
0

Simply add .thing { display: flex; flex-direction: column; justif-content: center; } to the CSS. flex-direction: column; will make sure that the normal block-level element behavior is maintained. justify-content: center; aligns the items at the cenetr of the main-axis (in this case the vertical axis).

.container {
  height: 100vh;
  width: 100vw;
  display: flex;
  flex-wrap: wrap;
}

.thing {
  background: red;
  color: white;
  margin: 5px;
  text-align: center;
  flex: 1 0 30%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
<div class="container">
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
  <div class="thing">
    text
  </div>
</div>
tacoshy
  • 10,642
  • 5
  • 17
  • 34