我有一个包含图像的DIV和另一个DIV。父DIV设置为position: absolute;
,子DIV设置为position: relative
。我的想法是在照片上方显示照片标题。
子级DIV
的宽度应为父级的100%
宽度,在左侧,右侧和底部减去10px
,再加上黑色背景。
.article-container {
position: relative;
}
.photo-caption {
width: 100%;
background-color: black;
position: absolute;
bottom: 0px;
margin-right: 10px;
margin-left: 10px;
margin-bottom: 10px;
}
<div class="span15 article-container">
<img src="images/example-image-1.png" />
<div class="photo-caption">This is the subtitle text on top.</div>
</div>
[左边距将.photo-caption
超出.article-container
的边界。右边距似乎没有任何作用。
我也尝试用box-sizing
修复此问题。似乎可以将.photo-caption
的宽度减小到父级宽度,但是仍然有突出的部分。
.photo-caption {
left:0;
right:0;
background-color: black;
position: absolute;
bottom: 0px;
margin-right: 10px;
margin-left: 10px;
margin-bottom: 10px;
}
.photo-caption {
width: calc(100% - 20px); // 20 = right margin + left margin
background-color: black;
position: absolute;
bottom: 0px;
margin-right: 10px;
margin-left: 10px;
margin-bottom: 10px;
}
注意,您可能要检查calc()浏览器支持here
style="width:98%; margin-left: 1%;"
.photo-caption {
display:block;
background-color: black;
position: absolute;
bottom: 0px;
margin-right: 10px;
margin-left: 10px;
margin-bottom: 10px;
padding:10px
}
困惑点:1)自动设置宽度时,视觉效果有所不同。在右边会产生相同的边距。但是由于没有width属性,宽度可以自由更改。
2)当元素向右浮动时,效果相同。
上述这两个要点将使人产生不同的边际权利印象。