一、介绍
文字背景图即具有图片背景的文字,本篇讲解如何通过css3制作文字背景图,个人觉得比较实用,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
二、实现方法
1、方法一:利用CSS3属性mix-blend-mode:lighten;实现
使用 mix-blend-mode 能够轻易实现,我们只需要构造出黑色文字,白色底色的文字 div ,叠加上图片,再运用 mix-blend-mode 即可,代码如下:
<div class="container">
<div class="pic"></div>
<div class="text">TEXT-IMAGE</div>
</div>
.pic {
position: relative;
width: 100%;
height: 100%;
background: url($img);
background-repeat: no-repeat;
background-size: cover;
}
.text {
position: absolute;
width:100%;
height:100%;
color: #000;
mix-blend-mode: lighten;
background-color: #fff;
}
2、方法二:利用CSS3属性-webkit-background-clip:text;实现
使用了这个属性的意思是,以区块内的文字作为裁剪区域向外裁剪,文字的背景即为区块的背景,文字之外的区域都将被裁剪掉。代码如下:
<div class="pic">
image
</div>
.pic{
width: 500px;
height: 500px;
margin: 40px auto;
background: url("1.jpg") no-repeat center center;
background-size: cover;
font-size: 120px;
font-weight: bold;
text-align: center;
line-height: 500px;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
该方法缺点是只支持webkit内核的浏览器,兼容性差。
三、总结
关于“CSS3如何制作文字背景图”主要利用属性mix-blend-mode:lighten和-webkit-background-clip:text来实现,大家可以自己手动试一下,加深下印象。也可以通过点击文字背景图demo链接查看。好了,文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识。