html - 使用 flexbox 使多个文本居中
问题描述
如何使用 flexbox 将多个文本居中居中。Margin: auto;
并且text align: right;
不起作用。
这是我的代码:http: //jsfiddle.net/L5mgy1sj/28/
我需要very long text 2
并very long text 3
坐在中间可能有一个边缘5px;
将它们分开;
完整视图:http: //jsfiddle.net/L5mgy1sj/28/show
提前致谢。
解决方案
像这样使用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>
推荐阅读
- cookies - 使用任何人的网站为我的网站设置 cookie
- javascript - 事件冒泡,addEventListener 未取消
- android - 如何在 android OS 中的特定时间启动应用程序?
- javascript - 如何在 NodeJS 的循环内处理多个 Google Places API 请求?
- php - Laravel中具有不同值的大规模更新表[设置]
- css - 如何创建 SASS 以使具有相同 CSS 的类与逗号一起声明?
- browser - 如何让 WebGL 支持 gpu 驻留代码?
- asp.net - 变量不是类的成员
- javascript - Vuex - 创建全局方法来调度动作
- c# - 如何将上一个和下一个按钮添加到 ListView