首页 > 解决方案 > 使用 flexbox 使多个文本居中

问题描述

如何使用 flexbox 将多个文本居中居中。Margin: auto;并且text align: right;不起作用。

这是我的代码:http: //jsfiddle.net/L5mgy1sj/28/

我需要very long text 2very long text 3坐在中间可能有一个边缘5px;将它们分开;

完整视图:http: //jsfiddle.net/L5mgy1sj/28/show

提前致谢。

标签: htmlcssresponsive-designflexbox

解决方案


像这样使用margin:auto

.flex-container {
  display: flex;
  flex-wrap: wrap;
}

.flex-item:nth-child(2) {
  margin-left: auto;
  margin-right: 5px;
}

.flex-item:nth-child(3) {
  margin-right: auto;
  margin-left: 5px;
}

.flex-item {
  line-height: 50px;
  color: white;
  background: pink;
  font-weight: bold;
  font-size: 2em;
  text-align: center;
}

@media only screen and (max-width: 400px) {
  .flex-container {
    justify-content: center;
  }
  .flex-item:nth-child(n) {
    margin: 5px;
  }
}
<div class="flex-container">
  <div class="flex-item"> text 1</div>
  <div class="flex-item"> text 2</div>
  <div class="flex-item"> text 3</div>
  <div class="flex-item"> text 4</div>
</div>


推荐阅读