首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >显示:丢失背景色的表格单元格

显示:丢失背景色的表格单元格
EN

Stack Overflow用户
提问于 2015-09-02 11:21:54
回答 2查看 1.6K关注 0票数 1

我已经为产品框创建了以下代码。我需要中间框有一个红色的背景,如果没有图像出现(是为一个CMS系统),我已经使用了display: table-cell,以便我可以垂直对齐的图像,但这似乎是一个问题。它也需要半响应,因此%基于宽度。我怎么能让中间的图像框有一个红色的背景?

Codepen:http://codepen.io/nickelse/pen/VvwRya?editors=110

HTML:

代码语言:javascript
复制
<div class="category-products cf">
  <div class="cp-1">
    <div class="image">
      <img src="http://lorempixel.com/output/abstract-h-c-260-300-1.jpg" alt="">
    </div><!-- .image -->

    <div class="title">
      This Is A Product Title
    </div><!-- .title -->

    <div class="price">
      £10.99
    </div><!-- .price -->
  </div><!-- .cp-1 -->

  <div class="cp-2">
    <div class="image">

    </div><!-- .image -->

    <div class="title">
      This Is A Product Title
    </div><!-- .title -->

    <div class="price">
      £10.99
    </div><!-- .price -->
  </div><!-- .cp-2 -->

  <div class="cp-3">
    <div class="image">
      <img src="http://lorempixel.com/output/abstract-h-c-260-300-1.jpg" alt="">
    </div><!-- .image -->

    <div class="title">
      This Is A Product Title
    </div><!-- .title -->

    <div class="price">
      £10.99
    </div><!-- .price -->
  </div><!-- .cp-3 -->  
</div><!-- .category-products -->

CSS:

代码语言:javascript
复制
body {
  margin: 20px 0;  
  font-family: sans-serif;
}

.category-products {
  width: 1000px;
  margin: auto;
}

.cp-1{
  width: 32%;
  float: left;
  background: #999;
  text-align: center;
}

.cp-2{
  width: 32%;
  float: left;
  background: #666;
  text-align: center;
  margin-left: 2%;
}

.cp-3{
  width: 32%;
  float: left;
  background: #333;
  text-align: center;
  margin-left: 2%;
}

.image {
  height: 350px;
  width: inherit;
  display: table-cell;
  vertical-align: middle;
  text-align: center;
  border: 1px solid #b9b9b9;
  background: #fe0000;
}

.title {
  font-size: 20px;
  line-height: 20px;
  padding: 12px 0;
  font-weight: bold;
}

.price {
  font-size: 18px;
}


/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}

.cf:after {
    clear: both;
}

/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */
.cf {
    *zoom: 1;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-02 11:33:39

使用display: flex而不是display: table-cell (码页):

代码语言:javascript
复制
.image {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 350px;
  border: 1px solid #b9b9b9;
  background: #fe0000;
}
票数 1
EN

Stack Overflow用户

发布于 2015-09-02 12:02:33

您可以使用行高并删除显示细化( display :tabe;为了正常工作,需要表/表行显示父)。

单条线上的线高度会将最小高度设置为您的容器.

div是块的,它会100%宽。

如果为空,则插入内联块伪元素以触发行高.

代码语言:javascript
复制
body {
  margin: 20px 0;
  font-family: sans-serif;
}
.category-products {
  width: 1000px;
  margin: auto;
}
.cp-1 {
  width: 32%;
  float: left;
  background: #999;
  text-align: center;
}
.cp-2 {
  width: 32%;
  float: left;
  background: #666;
  text-align: center;
  margin-left: 2%;
}
.cp-3 {
  width: 32%;
  float: left;
  background: #333;
  text-align: center;
  margin-left: 2%;
}
.image:before {
  content: '';
  display: inline-block;
}
.image {
  line-height: 350px;
  text-align: center;
  border: 1px solid #b9b9b9;
  background: #fe0000;
}
.image img {
  vertical-align: middle;
}
.title {
  font-size: 20px;
  line-height: 20px;
  padding: 12px 0;
  font-weight: bold;
}
.price {
  font-size: 18px;
}
/**
 * For modern browsers
 * 1. The space content is one way to avoid an Opera bug when the
 *    contenteditable attribute is included anywhere else in the document.
 *    Otherwise it causes space to appear at the top and bottom of elements
 *    that are clearfixed.
 * 2. The use of `table` rather than `block` is only necessary if using
 *    `:before` to contain the top-margins of child elements.
 */

.cf:before,
.cf:after {
  content: " ";
  /* 1 */
  display: table;
  /* 2 */
}
.cf:after {
  clear: both;
}
/**
 * For IE 6/7 only
 * Include this rule to trigger hasLayout and contain floats.
 */

.cf {
  *zoom: 1;
}
代码语言:javascript
复制
<div class="category-products cf">
  <div class="cp-1">
    <div class="image">
      <img src="http://lorempixel.com/output/abstract-h-c-260-300-1.jpg" alt="">
    </div>
    <!-- .image -->

    <div class="title">
      This Is A Product Title
    </div>
    <!-- .title -->

    <div class="price">
      £10.99
    </div>
    <!-- .price -->
  </div>
  <!-- .cp-1 -->

  <div class="cp-2">
    <div class="image">

    </div>
    <!-- .image -->

    <div class="title">
      This Is A Product Title
    </div>
    <!-- .title -->

    <div class="price">
      £10.99
    </div>
    <!-- .price -->
  </div>
  <!-- .cp-2 -->

  <div class="cp-3">
    <div class="image">
      <img src="http://lorempixel.com/output/abstract-h-c-260-300-1.jpg" alt="">
    </div>
    <!-- .image -->

    <div class="title">
      This Is A Product Title
    </div>
    <!-- .title -->

    <div class="price">
      £10.99
    </div>
    <!-- .price -->
  </div>
  <!-- .cp-3 -->
</div>
<!-- .category-products -->

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32351784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档