我有一张图片,我需要在右下角添加标签。
当我把它推到那里时,它比图像低4px,没有填充也没有边距。
.postImage {
position: relative;
display: inline-block;
}
.post img.thumbnail {
width:100%;
height: 100%;
}
.commercial {
position: absolute;
bottom: 0;
right: 0;
background-color: #666;
color: #fff;
padding: 3px;
font-size: 14px;
font-weight: 100;
}

<div class="postImage">
<img class="thumbnail" src="https://dummyimage.com/600x400/f00/fff">
<span class="commercial">commercial</span>
</div>
&#13;
解决这个问题的最佳方法是什么?我不认为
bottom: 4px;
是正确的。
谢谢
答案 0 :(得分:5)
默认情况下,图片标记在底部占用一些额外的空间,因为它是内联元素。将其更改为block
元素以删除额外空间
.thumbnail {
display: block; /*inline-block, float:left etc..*/
}
.postImage {
position: relative;
display: inline-block;
}
.post img.thumbnail {
width:100%;
height: 100%;
}
.commercial {
position: absolute;
bottom: 0;
right: 0;
background-color: #666;
color: #fff;
padding: 3px;
font-size: 14px;
font-weight: 100;
}
.thumbnail {
display: block;
}
&#13;
<div class="postImage">
<img class="thumbnail" src="https://dummyimage.com/600x400/f00/fff">
<span class="commercial">commercial</span>
</div>
&#13;
答案 1 :(得分:1)
只需将display: block;
添加到缩略图类。
.postImage {
position: relative;
display: inline-block;
}
.post img.thumbnail {
width:100%;
height: 100%;
}
.commercial {
position: absolute;
bottom: 0;
right: 0;
background-color: #666;
color: #fff;
padding: 3px;
font-size: 14px;
font-weight: 100;
}
.thumbnail {
display: block;
}
&#13;
<div class="postImage">
<img class="thumbnail" src="https://dummyimage.com/600x400/f00/fff">
<span class="commercial">commercial</span>
</div>
&#13;
答案 2 :(得分:1)
.postImage {
position: relative;
display: inline-block;
}
.postImage img.thumbnail {
width: 100%;
height: 100%;
display: block;
}
.commercial {
position: absolute;
bottom: 0;
right: 0;
background-color: #666;
color: #fff;
padding: 3px;
font-size: 14px;
font-weight: 100;
}
<div class="postImage">
<img class="thumbnail" src="https://dummyimage.com/600x400/f00/fff">
<span class="commercial">commercial</span>
</div>
答案 3 :(得分:1)
img
默认显示为inline
并更改为有效的display:block
,但don't
想change display
vertical-align:bottom
然后,您可以添加bottom line
,将{@ 1}}与parent div
的{{1}}对齐,如下所示,并且是的底部:0正常工作,因为.commercial定位为绝对。
.postImage {
position: relative;
display: inline-block;
}
.post img.thumbnail {
width:100%;
height: 100%;
}
.commercial {
position: absolute;
bottom: 0;
right: 0;
background-color: #666;
color: #fff;
padding: 3px;
font-size: 14px;
font-weight: 100;
}
img{
vertical-align:bottom;
}
&#13;
<div class="postImage">
<img class="thumbnail" src="https://dummyimage.com/600x400/f00/fff">
<span class="commercial">commercial</span>
</div>
&#13;