简体   繁体   English

如何在弹性项目中垂直居中文本?

[英]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> 

 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. flex格式上下文的范围仅限于父子关系。 Descendants of a flex container beyond the children do not participate in flex layout and will ignore flex properties. 子项之外的Flex容器的后代不参与flex布局,并将忽略flex属性。

In your layout, the .box element is the flex container (the parent) and the div elements are the flex items (the children). 在您的布局中, .box元素是flex容器(父元素),div元素是flex项(子元素)。 Therefore, the justify-content , align-content and align-items rules apply to the divs. 因此, justify-contentalign-contentalign-items规则适用于div。

However, the text is another level down. 但是,文本是另一个级别。 It falls outside the scope of this flex container. 它超出了这个flex容器的范围。 The text is considered a child element of the flex items / grand child of the flex container. 该文本被视为Flex容器的flex项目/ grand子项的子元素。 As a result, the text cannot accept flex properties. 因此,文本无法接受flex属性。

In CSS, text that is not explicitly wrapped by an element is algorithmically wrapped by an inline box. 在CSS中,未由元素显式包装的文本由内联框在算法上包装。 This makes it an anonymous inline element and child of the parent. 这使它成为父内容的匿名内联元素和子元素

From the CSS spec: 从CSS规范:

9.2.2.1 Anonymous inline boxes 9.2.2.1匿名内联框

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. flexbox规范提供了类似的行为。

4. Flex Items 4. Flex项目

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. Flex容器的每个in-flow子项都变为flex项,并且直接包含在flex容器内的每个连续文本行都包含在匿名flex项中。

Therefore, the text are child elements of the flex items. 因此,文本是flex项的子元素。

So all you need to do is make the flex item into a flex container. 所以你需要做的就是将flex项目变成一个flex容器。 You can then repeat the centering rules to vertically and/or horizontally center the text. 然后,您可以重复居中规则以垂直和/或水平居中文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM