我已经为产品框创建了以下代码。我需要中间框有一个红色的背景,如果没有图像出现(是为一个CMS系统),我已经使用了display: table-cell,以便我可以垂直对齐的图像,但这似乎是一个问题。它也需要半响应,因此%基于宽度。我怎么能让中间的图像框有一个红色的背景?
Codepen:http://codepen.io/nickelse/pen/VvwRya?editors=110
HTML:
<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:
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;
}发布于 2015-09-02 11:33:39
使用display: flex而不是display: table-cell (码页):
.image {
display: flex;
justify-content: center;
align-items: center;
height: 350px;
border: 1px solid #b9b9b9;
background: #fe0000;
}发布于 2015-09-02 12:02:33
您可以使用行高并删除显示细化( display :tabe;为了正常工作,需要表/表行显示父)。
单条线上的线高度会将最小高度设置为您的容器.
div是块的,它会100%宽。
如果为空,则插入内联块伪元素以触发行高.
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;
}<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 -->
https://stackoverflow.com/questions/32351784
复制相似问题