简体   繁体   English

文字对齐:中心; 忽略显示:flex元素?

[英]Text-align: center; ignored for display: flex element?

For the following the anchor text isn't centered. 对于以下内容,锚文本不居中。 If I change the anchor's display from flex to block then the text is centered. 如果我将锚点的显示从flex更改为block,则文本居中。 Why is this? 为什么是这样?

Ive only tested on Chrome so far. 到目前为止,我只在Chrome上测试过。

<ul>
  <li><a href="#">Link</a></li>
  <li><a href="#">Link</a></li>
  <li><a href="#">Link</a></li>
  <li><a href="#">Link</a></li>
  <li><a href="#">Link</a></li>
  <li><a href="#">Link</a></li>
</ul>



*, *:before, *:after {
  -moz-box-sizing: border-box; 
  -webkit- box-sizing: border-box;   box-sizing: border-box;
 }

ul {
  display: flex;
  flex-flow: row wrap;
  padding: 0;
  width: 40%;
  margin: auto;
  border: 1px solid gold;
}
li {
  list-style-type: none;
}
a {
  background: grey;
  width: 200px;
  border: 1px solid red;
  text-align: center;
  display: flex;
}

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

Note: Before you go ahead to read my answer, I would like you to notify that am not using any of the PROPRIETARY PREFIXES, if it doesn't work for you, its time to update your browser, or try adding the prefixes like -moz or -webkit and see if it works for you. 注意:在您继续阅读我的答案之前,我希望您通知我没有使用任何专有的前缀,如果它不适合您,是时候更新您的浏览器,或尝试添加前缀,如-moz-webkit ,看看它是否适合你。

The correct way to do is to use display: flex; 正确的方法是使用display: flex; for the ul element and flex-grow on li elements... 对于ul元素和li元素的flex-grow ...

ul {
  display: flex;
  padding: 0;
  width: 40%;
  margin: auto;
  border: 1px solid gold;
}

li {
  list-style-type: none;
  flex-grow: 1;
}

Demo 演示

You are using a fixed width on a tag ie width: 200px; 您正在使用一个固定的宽度a标签,即width: 200px; and that kills the idea of flex layout, so take that out, and alter your CSS with the one provided by me. 并且这会破坏flex布局的想法,所以请把它拿出来,并用我提供的CSS改变你的CSS。

Here, I've used flex-grow with a value of 1 so that each of the li element shares the equal amount of width 在这里,我使用了flex-grow ,其值为1因此每个li元素共享相同的width


If you are looking to wrap the cells if they exceed the containers width, than you need to use the following properties on ul element like 如果你想要将单元格包装超过容器宽度,那么你需要在ul元素上使用以下属性

ul {
  display: flex;
  padding: 0;
  width: 40%;
  margin: auto;
  border: 1px solid gold;
  flex-direction: row;
  flex-flow: row wrap;
}

Demo (Resize your window to see the effect) 演示 (调整窗口大小以查看效果)

在此输入图像描述

Sample 1, the wrapped element will stretch to full width 样品1,包裹的元素将拉伸至全宽


在此输入图像描述

Sample 2, Elements wrapped equally further 样品2,元素进一步包裹

Also note that am using min-width: 100px; 另请注意,我使用的是min-width: 100px; on li elements to force wrap in the demonstration li元素上强制包装在演示中


As you commented that you wanted three li on each row, than instead of using flex-grow: 1; 正如你评论的那样,你想要每行三个li,而不是使用flex-grow: 1; you need to use flex: 1 1 33%; 你需要使用flex: 1 1 33%; like 喜欢

li {
  list-style-type: none;
  flex-grow: 1;
  min-width: 100px;
  flex: 1 1 33.33%;
}

Demo 演示

在此输入图像描述

Sample 3, Equal number of li on each row 样本3,每行相等的li数

在此输入图像描述

Sample 4, Equal number of li on each row even when resized 样本4,即使调整大小,每行也有相等数量的li

On further resize, they will accommodate automatically say two in a row and one in a row.. 在进一步调整大小时,它们将自动容纳连续两个并且连续一个..

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

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