【问题标题】:How to make the gradient come on top of image and behind the text?如何使渐变出现在图像顶部和文本后面?
【发布时间】:2016-08-28 00:14:26
【问题描述】:

我一直在尝试将宏大放置在图像顶部和文本后面,但我尝试从网络上找到的一切都没有奏效。 我需要在 div 中包含图像的来源,因为如果它在 css 代码中,它将将相同的图像应用于我创建的所有其他模板。

谢谢:)

<div class="titles">
    <div class="thumb">
        <img class="img overlay" 
         height=260px 
         width=240px 
         alt="Shokugeki no Souma: Ni no Sara" 
         src="https://img7.anidb.net/pics/anime/184719.jpg" />
            <div class="titulo">Shokugeki no Souma: Ni no Sara</div>
            <div class="epis">Epis. 12</div>
    </div>

.titles .thumb {
    position: relative;
    float: left;
    height: 260px;
    width: 245px;
    max-height: 260px;
    max-width: 260px;
    margin: 0 auto;
    padding: 5px;
 }

 .thumb .titulo {
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 8px;
    margin: 5px;
    font-size: 15px;
    font-weight: bold;
  color: white;
}

.thumb .epis {
    position: absolute;
    top: 0;
    right: 0;
    padding: 8px;
    margin: 10px;
    font-size: 15px;
    font-weight: bold;
  color: white;
}

.titles .thumb .img:hover {
    max-height: 260px;
    max-width: 260px;
    height: 260px;
    width: 240px;
    opacity: 0.8;
}

.img.overlay {
  background: linear-gradient( 
    to bottom,   
      black, 
      rgba(64,64,64,1),
      rgba(64,64,64,1),
      rgba(64,64,64,1),
      rgba(64,64,64,1),
      rgba(64,64,64,1),
      rgba(64,64,64,1), 
      black);
}

【问题讨论】:

    标签: html css linear-gradients


    【解决方案1】:

    您介意在尝试实现此功能时多解释一下出了什么问题吗?

    我的建议是将叠加层放在自己单独的 div 中,而不是放在图像标签内。然后在开始标题 div 之前关闭覆盖 div。然后你可以设置你的“thumb”类在你的css中具有一定的宽度和高度,并将覆盖 div 的样式设置为 height:100% width:100% 以便它完全覆盖你的缩略图。如果您绝对定位标题文本,则可以将其放置在渐变的顶部。例如

    <div class="titles">
        <div class="thumb">
            <img alt="Shokugeki no Souma: Ni no Sara" src="https://img7.anidb.net/pics/anime/184719.jpg" />
         <div class="overlay"></div>      
                <div class="titulo">Shokugeki no Souma: Ni no Sara</div>
                <div class="epis">Epis. 12</div>
        </div>
    </div>
    

    【讨论】:

    • 嗨,感谢您的回复,当我尝试实现渐变时,它总是停留在图像的后面,我“尝试过”(就像 2 周前一样开始使用 html+css 编码,所以我我还没有真正体验过它,所以我不确定我是否做得正确)你解释的方式,也使用z-index和改变绝对/相对位置但到目前为止没有,我能得到的最接近的是这样的: codepen.io/DrakonArc/pen/grVxGx 我在图片的 url 中添加了一个字母,所以它最终会在 url 路径中出现错误,并且变成这样,渐变就在文本后面。
    • 我在这里分叉了你的 Codepen 并实现了我的上述建议:codepen.io/bananahavana/full/pbrLzN 这是你想要完成的吗?
    • 想向您展示它在网站中的外观,但我们似乎无法向用户发送消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 2019-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    相关资源
    最近更新 更多