当前位置:主页 > 查看内容

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

发布时间:2021-04-22 00:00| 位朋友查看

简介:效果: 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 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://m.jb51.net/css/749159.html
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:CSS3 对过渡(transition)进行调速以及延时 下一篇:没有了

推荐图文


随机推荐