文本在图像上的绝对位置

时间:2017-05-04 08:50:23

标签: html css css-position

我有一张图片,我需要在右下角添加标签。

当我把它推到那里时,它比图像低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;
&#13;
&#13;

解决这个问题的最佳方法是什么?我不认为

bottom: 4px;

是正确的。

谢谢

4 个答案:

答案 0 :(得分:5)

默认情况下,图片标记在底部占用一些额外的空间,因为它是内联元素。将其更改为block元素以删除额外空间

.thumbnail {
    display: block; /*inline-block, float:left etc..*/
}

&#13;
&#13;
.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;
&#13;
&#13;

答案 1 :(得分:1)

只需将display: block;添加到缩略图类。

&#13;
&#13;
.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;
&#13;
&#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'tchange display vertical-align:bottom然后,您可以添加bottom line,将{@ 1}}与parent div的{​​{1}}对齐,如下所示,并且是的底部:0正常工作,因为.commercial定位为绝对。

&#13;
&#13;
.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;
&#13;
&#13;