简体   繁体   English

等高列在flexbox中居中

[英]Equal height columns with centered content in flexbox

I'd like to have two columns of equal height and their content should be middle aligned, so in the very center of each div. 我想有两列等高,它们的内容应该居中对齐,因此在每个div的正中间。

Problem: 'equal height' and 'middle aligned' seem to exclude themselves, the one doesn't work with the other. 问题: “等高”和“中间对齐”似乎将自己排除在外,一个与另一个不兼容。

Question: How can I create a row with two columns with different width, equal height and their content centered in the middle of each column? 问题:如何创建一个具有两列的行,这两列具有不同的宽度,相同的高度,并且其内容居中于每一列的中间?

<!-- 'middle aligned' and 'equal height' don't like each other ? -->
<div class="ui equal height center aligned grid">
    <div class="row">
        <div class="twelve wide purple column">
        <p>Text Text Text</p>
        <p>Text Text Text</p>
        <p>Text Text Text</p>
        <p>Text Text Text</p>
        </div>
        <div class="four wide red column  middle aligned">
            <div class="row">Forward</div>

        </div>
    </div>
</div>

http://jsfiddle.net/kjjd66zk/1/ http://jsfiddle.net/kjjd66zk/1/

The key to this layout is to apply equal heights to the primary flex container. 此布局的关键是将相等的高度应用于主伸缩容器。

Then make the flex items nested flex containers, which can center the content of the flex items. 然后,将flex项目嵌套在flex容器中,以使flex项目的内容居中。

Hence, the top level creates the equal height. 因此,顶层创建相等的高度。 The second level does the centering. 第二级进行居中。

(See the note at the bottom for more details.) (有关更多详细信息,请参阅底部的注释。)

Here's an example based on your code structure: 这是一个基于您的代码结构的示例:

 body { height: 300px; /* for demo purposes */ color: white; } flex-container { display: flex; /* primary flex container */ flex-direction: row; /* horizontal alignment of flex items (default value; can be omitted) */ align-items: stretch; /* will apply equal heights to flex items (default value; can be omitted) */ height: 100%; } flex-item { display: flex; /* nested flex container */ flex-direction: column; /* vertical alignment of flex items */ justify-content: center; /* center flex items vertically */ align-items: center; /* center flex items horizontally */ } flex-item:first-child { flex: 3; /* consume 3x more free space than sibling */ background-color: #a333c8; } flex-item:last-child { flex: 1; background-color: #db2828; } 
 <flex-container> <flex-item><!-- also flex container --> <p>Text Text Text</p> <p>Text Text Text</p> <p>Text Text Text</p> <p>Text Text Text</p> </flex-item> <flex-item><!-- also flex container --> <div>Forward</div> </flex-item> </flex-container> 

jsFiddle demo jsFiddle演示


People sometimes consider a flex item and its content to be one element. 人们有时将弹性项目及其内容视为一个要素。 This is not correct. 这是不正确的。

The HTML structure of a flex container has three levels: flex容器的HTML结构具有三个级别:

  • the container 容器
  • the item 该项目
  • the content 内容

Therefore, the content inside an item does not represent the item; 因此,项目内部的内容不代表该项目; it represents a separate element. 它代表一个单独的元素。

If the content inside an item is text, it becomes an anonymous element. 如果项目内的内容是文本,则它将成为匿名元素。

From the flexbox spec: 从flexbox规范:

4. Flex Items 4.弹性项目

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容器的每个流入子元素都将成为一个flex项目,而直接包含在flex容器内的每个连续文本都将包裹在一个匿名flex项目中。

That's why, in the solution above, the flex item becomes a flex container. 这就是为什么在上面的解决方案中,flex项变为flex容器。 It enables the use of flex properties on the children of the flex item (the content). 它允许在flex项目(内容)的子项上使用flex属性。

You need to give height to your elements 您需要给元素增高

.height {
  height: 300px;
}
.row {
  height: 100%;
}
.row > div {
  height: 100%;
}

Updated fiddle 1 更新小提琴1

If you want them to center vertical, update above .row > div rule to this 如果您希望它们位于垂直居中位置,请在.row > div规则上方更新为此

.row > div {
  height: 100%;
  display: flex !important;
  flex-direction: column;
  justify-content: center;
}

Updated fiddle 2 更新小提琴2

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

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