Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center text vertically in a flex item?

I just want the text to be like:

display: table-cell;
vertical-align: middle;

But using flex boxes

div {
  border: 1px solid black;
}

.box {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-content: center;
  align-items: stretch;
  height: 200px;
}

.box div {
  order: 1;
  flex: 0 1 auto;
  align-self: auto;
  min-width: 0;
  min-height: auto;
}

.box div.C {
  flex: 1 1 auto;
}
<div class="box">
  <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 class="F">F</div>
</div>
like image 453
Vitim.us Avatar asked Jun 25 '17 03:06

Vitim.us


People also ask

How do I center vertically in Flex?

Centering Vertically By default flex items will fill vertical space of their parent element. To vertically center our div we can add a single CSS property. By using align-items: center we can vertically center all flex items to the parent container along the cross axis of the flex container.

How do I center text vertically in an element?

Answer: Use the CSS line-height property Suppose you have a div element with the height of 50px and you have placed some link inside the div that you want to align vertically center. The simplest way to do it is — just apply the line-height property with value equal to the height of div which is 50px .

Does vertical align work with Flex?

With Flexbox, you can stop worrying. You can align anything (vertically or horizontally) quite painlessly with the align-items , align-self , and justify-content properties.


1 Answers

div {
  border: 1px solid black;
}

.box {
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-content: center;
  align-items: stretch;
  height: 200px;
}

.box div {
  order: 1;
  flex: 0 1 auto;
  align-self: auto;
  min-width: 0;
  min-height: auto;
  display: flex;            /* NEW */
  align-items: center;      /* NEW */
}

.box div.C {
  flex: 1 1 auto;
}
<div class="box">
  <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 class="F">F</div>
</div>

Explanation

The scope of a flex formatting context is limited to a parent-child relationship. Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties.

In your layout, the .box element is the flex container (the parent) and the div elements are the flex items (the children). Therefore, the justify-content, align-content and align-items rules apply to the divs.

However, the text is another level down. It falls outside the scope of this flex container. The text is considered a child element of the flex items / grand child of the flex container. As a result, the text cannot accept flex properties.

In CSS, text that is not explicitly wrapped by an element is algorithmically wrapped by an inline box. This makes it an anonymous inline element and child of the parent.

From the CSS spec:

9.2.2.1 Anonymous inline boxes

Any text that is directly contained inside a block container element must be treated as an anonymous inline element.

The flexbox specification provides for similar behavior.

4. Flex Items

Each in-flow child of a flex container becomes a flex item, and each contiguous run of text that is directly contained inside a flex container is wrapped in an anonymous flex item.

Therefore, the text are child elements of the flex items.

So all you need to do is make the flex item into a flex container. You can then repeat the centering rules to vertically and/or horizontally center the text.

like image 124
Michael Benjamin Avatar answered Oct 06 '22 01:10

Michael Benjamin