简体   繁体   English

在图像上显示文本

[英]Displaying text over an image

I am trying to display a text over an image when hovering. 我正在尝试在悬停时在图像上显示文本。 I was trying to follow this guide , but I didnt manage to convert that to my project. 我试图遵循这个指南 ,但我没有设法将其转换为我的项目。 I could make an overlay, but that would have to be a fixed overlay, I would like to have it so it changes when the resolution changes. 我可以做一个叠加,但这必须是一个固定的叠加,我想拥有它,所以当分辨率改变时它会改变。 I wanted the overlay to be same size as thumbmail. 我希望叠加层与thumbmail的大小相同。

HTML: HTML:

  <div class="col-md-6 col-sm-2">
  {% for phone in phones %}
  <div class="llol col-md-8 col-md-2">
      <a href="{{ url_for('phones.phone', id=phone.id) }}" class="thumbnail">
        <img src="{{ url_for('static', filename='phone_pics/' + phone.image) }}" alt="" class="cus-img">
      {% if phone.stock == 0 %}
      <div class="overlayOUT">
          <div class="textOUT">OUT OF STOCK!</div>
      </div>

      </a>
      {% else %}
      </a>
      {% endif %}
      <p style="text-align: center;"><b>{{phone.brand.name}}</b> <span style="color: #006666;">{{phone.model}}</span><span class="badge">{{phone.stock}}</span></p>
  </div>
  {% endfor %}
</div> 

CSS: CSS:

    .thumbnail img{
    height:240px;
    width:100%; 
    }
    .overlayOUT{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin-left: 15px;
    height: 250px;
    width: 183px;
    opacity: 0.8;}

    .textOUT{
    color: white;
    padding: auto;
    font-size: 20px;
    position: absolute;
    top: 48%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
    background-color: #FA8072;  
    }

Just add display: none; 只需添加display: none; prop in .textOUT 支持.textOUT

.textOUT{
    color: white;
    padding: auto;
    font-size: 20px;
    position: absolute;
    top: 48%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
    background-color: #FA8072;  
    display: none;
}

And add hover CSS , i hope it'll help you out. 并添加悬停CSS ,我希望它能帮助你。 Thanks 谢谢

.thumbnail:hover .textOUT{
    display: block;
}

The easiest way to do it is by simply creating a container for the text, setting its opacity to 0, and changing the opacity to 1 on the hover event. 最简单的方法是简单地为文本创建容器,将其不透明度设置为0,并在悬停事件上将不透明度更改为1。 You're not using vanilla HTML, and I don't have access to your variables, so I made a sample that can be implemented the same way. 您没有使用vanilla HTML,我无法访问您的变量,因此我制作了一个可以以相同方式实现的示例。

The .container class is setting the positioning of both the image and the hidden text container. .container类正在设置图像和隐藏文本容器的位置。 Then, just set the .text-container class to be positioned on top of the image, and set a hover event on this class to change it from 0 opacity to 1. 然后,只需将.text-container类设置为位于图像顶部,并在此类上设置悬停事件,将其从0不透明度更改为1。

 .container { box-sizing: border-box; position: relative; display: inline-block; overflow: hidden; width: 400px; height: 300px; } img { width: 100%; height: 100%; } .text-container { opacity: 0; position: absolute; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.6); color: #fff; transition: all 0.4s ease-in-out 0s; } .text-container:hover { opacity: 1; } .text { text-align: center; position: relative; top: 50%; } 
 <div class="container"> <img src="https://www.romanticasheville.com/sites/default/files/images/basic_page/Asheville_Waterfalls.jpg"> <div class="text-container"> <div class="text">Beautiful Waterfall</div> </div> </div> 

You can read about many different ways to hover things in over images here . 您可以在这里阅读有关在图像中悬停事物的许多不同方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM