如何从flexbox网格文本扩展背景&高度相等的图像

时间:2015-07-07 11:58:53

标签: css vertical-alignment flexbox centering

我做了一个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%;
  }
}

1 个答案:

答案 0 :(得分:0)

使用转换而不是 flexbox 来垂直居中文本。

1)从align-items: center班级

中删除.wrap-items

2)像这样调整.wrap-items .item > .text类:

.wrap-items .item > .text {
  padding: 10px;
  font-size: 20px;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

Updated 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;
  position: relative;
}
.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;
  position: absolute;
  top: 50%;
  transform: translate(0, -50%);
}
.item.un {
  background-color: orange;
}
.item.dos {
  background-color: brown;
}
.item.tres {
  background-color: purple;
}
@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%;
  }
}
<div class="wrap-items">
  <!-- row 1 -->
  <div class="item span-2">
    <div class="text">
      <h2>THE TITLE</h2>
      <p>Just be water my friend</p>
      <p>Small people can do very big things.</p>
    </div>
  </div>
  <div class="item">
    <img src="https://placekitten.com/g/800/600" alt>
  </div>
  <!-- row 2 -->
  <div class="item un">
    <div class="text ">
      <a href="">one or more lines of text inside the box</a>
    </div>
  </div>
  <div class="item">
    <img src="https://placekitten.com/g/800/600" alt>
  </div>
  <div class="item dos">
    <div class="text">
      <a href="">text in the middle</a>
    </div>
  </div>
  <!-- row 3 -->
  <div class="item">
    <img src="https://placekitten.com/g/800/600" alt="">
  </div>
  <div class="item tres">
    <div class="text">
      <a href="">three lines of text centered vertically in the middle of the box</a>
    </div>
  </div>
  <div class="item">
    <img src="https://placekitten.com/g/800/600" alt>
  </div>
</div>