如何在不同大小的多个图像上叠加文本? CSS

时间:2016-11-19 22:14:35

标签: html css overlay image-size

我有一位客户希望在每张图片左下角出售的所有照片上都有卖出的文字。我已经完成了实现,但问题是某些图像的大小不同。有一个"一个类适合所有" css的类型我可以用来正确定位文本而不管图像的大小?

守则:



.sold-overlay {
  position: absolute; 
  top: 80%; 
  left: 59%; 
  width: 25%;
  border: 1px solid #e60000;
  color: white;
  font-size: 50%;
  background-color: red;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row">
    <div class="col-lg-8 col-lg-offset-2">
      <div class="modal-body">
        <h2>I Am Travon Martin</h2>                          
        <img class="img-responsive img-centered" src="img/portfolio/sold/img-6847.jpg" alt="">
        <h2 class="sold-overlay">Sold</h2>
        <p>I'm sorry, this artwork has been sold to a happy customer.</p>                           
        <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-times"></i> Close Project</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

我将红色Sold横幅放在左下角,并将图像调整为100%,这样它将始终填充其列/响应容器:

&#13;
&#13;
.sold-overlay {
position: absolute; 
bottom: 0; 
left: 0; 
width: 25%;
border: 1px solid #e60000;
color: white;
font-size: 50%;
background-color: red;
}

.relative {
position: relative;  
}

.relative img {
  display: block;
  width: 100%;
  }
&#13;
<div class="container">
                <div class="row">
                    <div class="col-lg-8 col-lg-offset-2">
                        <div class="modal-body">
                            <h2>I Am Travon Martin</h2>                      
                          <div class="relative">
                            <img class="img-responsive img-centered" src="http://placehold.it/200x100" alt="">
                            <div class="sold-overlay"><h2>Sold</h2></div>
                            </div>
                            
                            <p>I'm sorry, this artwork has been sold to a happy customer.</p>                           
                           
                        </div>
                       <button type="button" class="btn btn-primary" data-dismiss="modal"><i class="fa fa-times"></i> Close Project</button>
                    </div>
                </div>
            </div>
&#13;
&#13;
&#13;