css如何实现文字在背景图片之上?本篇文章小编给大家分享一下css实现文字在背景图片之上代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
效果:
非常感谢!
您已投票:{{item.voteTime}}
抱歉,您未完成投票~
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,才解决了这个问题。