css实现文字在背景图片之上

css实现文字在背景图片之上,第1张

css实现文字背景图片之上

效果:

<div class="imgs">
  <!-- 背景图 -->
  <div class="background">
    <img :src="item.voteTime ? imgSrc1:imgSrc2" width="100%" height="100%" alt="" />
  </div>
  <!-- 文字 -->
  <div class="front">
    <div v-if="item.voteTime">
      <p>非常感谢!</p>
      <p>您已投票:<span>{{item.voteTime}}</span></p>
    </div>
    <p v-else style="color:#999999">抱歉,您未完成投票~</p>
  </div>
</div>

data() {
  return {
    imgSrc1:require('@/common/imgs/yitoupiao.png'),
    imgSrc2:require('@/common/imgs/weiwancheng.png'),
  }
},

外面大的div:设置宽高;
背景图片:1)如果是铺满则宽高都设置100%,2)如果只占一个部分可设置定位。


重点:z-index一定要比文字的层级低,否则会被遮住;
文字:根据需求放置位置可定位可不定位,重点z-index设置高于图片;

.imgs {
  background: #fff;
  position: relative;
  width: 100%;
  height: 250px;
  color: #195541;
  .background{
    // width:100%;  
    // height:100%;  /**宽高100%是为了图片铺满屏幕 */
    // z-index:-1;
    z-index:1;
    position: absolute;
    width: 250px;
    height: 100%;
    right: 20px;
    bottom: 0px;
  }
  .front{
    z-index:2;
    position: absolute;
    text-align: center;
    top: 39%;
    left: 25%;
    font-weight: normal;
    line-height: 40px;
    font-size: 28px;
  }
}

开发过程中遇到一个bug:就是我一开始设置的背景图片z-index为-1,导致在h5上,背景图片一会儿能显示一会儿没法显示,后来改为正数1,才解决了这个问题。


到此这篇关于css实现文字在背景图片之上的文章就介绍到这了,更多相关css文字在背景图片上内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/615932.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-14
下一篇 2022-04-14

发表评论

登录后才能评论

评论列表(0条)

保存