【发布时间】:2015-09-24 21:44:40
【问题描述】:
我做了一个 flexbox 网格,它结合了文本单元格和图像单元格。所有单元格在每个分辨率下都具有相同的高度,文本单元格也是如此。文本单元格的文本垂直居中,这是必须的。现在我需要为每个文本单元格分配不同的背景颜色,但这里有问题。
使用 align-items: center 文本垂直居中,但背景颜色仅应用于文本。如果没有 align-items: center,背景会扩展到所有单元格,但内容不会垂直居中。 这是codepen
关于如何同时实现这两个功能有什么建议吗?
- 垂直居中文本
- 每个文本单元格的背景颜色不同
谢谢!
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
margin: 0;
background: #333;
padding: 30px;
font-family: Helvetica;
}
a {
color: white;
text-decoration: none;
}
h2 {
font-size: 1.2em;
}
.wrap-items {
background-color: #ccc;
padding: 0;
margin: 0 auto;
display: -ms-flexbox;
-ms-flex-wrap: wrap;
-ms-flex-direction: row;
-webkit-flex-flow: row wrap;
flex-flow: row wrap;
display: -webkit-box;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
}
.wrap-items .item {
-webkit-box-flex: 0 1 auto;
-ms-flex: 0 1 auto;
flex: 1 0 33.333%;
width: 33.33333%;
margin: 0;
padding: 0;
color: white;
font-size: 0;
border: none;
background-color: steelblue;
}
.wrap-items .item.span-2 {
-webkit-box-flex: 2 0 auto;
-ms-flex: 2 0 auto;
flex: 2 0 auto;
width: 66.6666%;
height: auto;
}
.wrap-items .item img {
width: 100%;
height: auto;
}
.wrap-items .item > .text {
padding: 10px;
font-size: 20px;
height: 100%;
}
.item.un .text{
background-color: orange;
}
.item.dos {
background-color: brown;
}
.item.tres {
background-color: violet;
}
@media screen and (max-width: 760px) {
.wrap-items .item {
margin: 0;
flex: 50%
}
}
@media screen and (max-width: 400px) {
.wrap-items .item {
margin: 0;
flex: 100%;
}
}
【问题讨论】:
-
Codepen 链接已损坏 - 尝试codepen.io/nuriarai/pen/yNKOxO
标签: css vertical-alignment flexbox centering