请参阅下图,我的底部灰色(父div背景颜色)高度随父div height
而增加。我希望描述div的全高,如第一个div(10px
填充所有方面。)
我在尝试什么:
代码:
body,html {
margin: 0px;
padding: 0px;
font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;
font-size: 14px;
}
.container
{
margin: 20px;
float: left;
}
.mainC{
width: 150px;
padding: 10px;
border: 1px solid #c7c7c7;
background-color: #e7e7e7;
position:relative;
float:left;
margin: 0 5px;
}
.mainC .title{
padding: 5px;
background-color: gray;
color: white;
text-align: center;
}
.mainC .discBody{
background-color: #ffffff;
padding: 5px;
height: 81%;
}
<div class="container">
<div class="mainC" style="height:200px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:225px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:250px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:275px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:300px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
我无法控制height
的内联样式(.mainC
)。内联高度可能是其他任何内容。
问题:如何为所有div设置".discBody"
的正确高度?用于显示.mainC
的填充始终为10px
。
演示:http://jsfiddle.net/sachinkk/jdp09s1c/
谢谢!
答案 0 :(得分:3)
我会选择通过添加和额外的div来更改标记以实现此效果。
在下面的代码中,我在每个div
容器中使用了名为.mainC-inner
的绝对定位.mainC
,并更改/移动了.mainC
中的某些样式
我选择使用边框来获得灰色轮廓和白色背景。这允许您使用以下内容将新.mainC-inner
div拉伸到.mainC
父容器的确切高度:
.mainC-inner {
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
border:10px solid #e7e7e7;
background: #fff;
}
这适用于所有浏览器,包括IE8。这是一个小提琴http://jsfiddle.net/nrnank32/
body,html {
margin: 0px;
padding: 0px;
font-family: 'Helvetica Neue',Helvetica,Arial,sans-serif;
font-size: 14px;
}
.container
{
margin: 20px;
float: left;
}
.mainC {
width: 150px;
border: 1px solid #c7c7c7;
background-color: #e7e7e7;
position:relative;
float:left;
margin: 0 5px;
}
.mainC-inner {
position: absolute;
top:0;
left:0;
bottom:0;
right:0;
border:10px solid #e7e7e7;
background: #fff;
}
.mainC .title{
padding: 5px;
background-color: gray;
color: white;
text-align: center;
}
.mainC .discBody{
background-color: #ffffff;
padding: 5px;
}
&#13;
<div class="container">
<div class="mainC" style="height:200px;">
<div class="mainC-inner">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
<div class="mainC" style="height:225px;">
<div class="mainC-inner">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
<div class="mainC" style="height:250px;">
<div class="mainC-inner">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
<div class="mainC" style="height:275px;">
<div class="mainC-inner">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
<div class="mainC" style="height:300px;">
<div class="mainC-inner">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
</div>
&#13;
答案 1 :(得分:2)
主要问题是.mainC .discBody{height:81%}
,每个盒子的高度都不同,因为他们的父母有不同的高度。 CSS calc()
可以提供帮助,browser support IE 9及更高版本。
.mainC .title {
padding: 5px;
height: 30px;
box-sizing: border-box;
}
.mainC .discBody {
height: 81%; /* keep this as fallback */
height: calc(100% - 40px); /* 10px for the spacing */
}
更新演示如下,同样在http://jsfiddle.net/ptao72w2/1/
body, html {
margin: 0px;
padding: 0px;
font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
}
.container {
margin: 20px;
float: left;
}
.mainC {
width: 150px;
padding: 10px;
border: 1px solid #c7c7c7;
background-color: #e7e7e7;
position:relative;
float:left;
margin: 0 5px;
}
.mainC .title {
padding: 5px;
background-color: gray;
color: white;
text-align: center;
height: 30px;
box-sizing: border-box;
}
.mainC .discBody {
background-color: #ffffff;
padding: 5px;
height: 81%;
height: calc(100% - 40px);
}
<div class="container">
<div class="mainC" style="height:200px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:225px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:250px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:275px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
<div class="mainC" style="height:300px;">
<div class="title">
<span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>
答案 2 :(得分:0)
答案 3 :(得分:0)
如果您的内容确定了框大小而不是样式,那么这并不困难:
body,
html {
margin: 0px;
padding: 0px;
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size: 14px;
}
.container {
margin: 20px;
}
.mainC {
width: 150px;
padding: 10px;
border: 1px solid #c7c7c7;
background-color: #e7e7e7;
position: relative;
float: left;
margin: 0 5px;
}
.mainC .title {
padding: 5px;
background-color: gray;
color: white;
text-align: center;
}
.mainC .discBody {
background-color: #ffffff;
padding: 5px;
}
<div class="container">
<div class="mainC">
<div class="title"> <span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here This is discription part here This is discription part here</p>
</div>
</div>
<div class="mainC">
<div class="title"> <span>Heading</span>
</div>
<div class="discBody">
<p>This is discription part here This is discription part here This is discription part here This is discription part here This is discription part here</p>
</div>
</div>
<div class="mainC">
<div class="title"> <span>Much Much Longer Heading</span>
</div>
<div class="discBody">
<p>This is discription part here</p>
</div>
</div>
</div>