今天我们我们来看看使用CSS3怎么给文本添加背景图,让文字变得生动好看!在我们想要创建一个 较大 的文本标题,但 不想 使用普通又枯燥的颜色来修饰时,非常有用!
我们先来看看效果图:
下面我们来研究一下是怎么实现这个效果的:
首先是HT ML 部分 ,定义两个标题
<body> < h1 >Hello world!</h1> <h3>Hello world!</h3> </body>
然后 开始 定义css样式来进行修饰:
body {
dis play : flex;
align -i tems: center ;
justify-content: center;
flex-direction: column;
width: 100%;
text-align: center;
min-h ei ght: 100vh;
font- Size: 100px;
font-f ami ly:Arial, Helvetica, sans -s erif;
}
最后就是给文字添加背景图片:
将文字原本的颜色设置为transparent透明,然后 利用 background-image属性给文字加背景图片
h1 {
color: transparent;
background-image: url("https:// img .js-code .COM /upload/article/000/000/024/612 360 451cede816. jpg ");
}
h3{
color: transparent;
background-image: url("https://img.js-code测试数据/upload/article/000/000/024/6124c86e0808b298.jpg");
}
发现效果是这样的,不如人意。这是因为缺少了一个关键属性 background-clip 。background-clip属性是一个CSS3 新属性 ,要添加前缀来兼容其他浏览器
h1 {
color: transparent;
background-image: url("https://img.js-code测试数据/upload/article/000/000/024/612360451cede816.jpg");
background-clip: text;
- webkit -background-clip: text;
}
h3{
color: transparent;
background-image: url("https://img.js-code测试数据/upload/article/000/000/024/6124c86e0808b298.jpg");
background-clip: text;
-webk IT -background-clip: text;
}
ok,大功告成!下面附上完整代码:
<!DOCTY PE html>
<html>
<head>
< ;m eta charset="utf-8">
<style type="text/css">
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100%;
text-align: center;
min-height: 100vh;
font-size: 100px;
font-f am ily: Arial, Helvetica, sans-serif;
}
h1 {
color: transparent;
background-image: url("https://img.js-code测试数据/upload/article/000/000/024/612360451cede816.jpg");
background-clip: text;
-webkit-background-clip: text;
}
h3 {
color: transparent;
background-image: url("https://img.js-code测试数据/upload/article/000/000/024/6124c86e0808b298.jpg");
background-clip: text;
-webkit-background-clip: text;
}
</style>
</head>
<body>
<h1>Hello world!</h1>
<h3>Hello world!</h3>
</body>
</html>因为我们使用的是静态图片,所以是文本背景图效果也是静态的。如果使用动图会有动态效果:
h3 {
background-image: url("https://img.js-code测试数据/upload/image/161/146/599/1629799857734746.gif"),
url("https://img.js-code测试数据/upload/image/817/380/291/1629799861847258.gif");
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}
PHP中文网平台有非常多的视频教学资 源 ,欢迎大家学习《css视频教程》!
以上就是纯CSS3怎么给文本添加背景图的详细内容,更多请关注其它相关文章!
总结
以上是 为你收集整理的 纯CSS3怎么给文本添加背景图 全部内容,希望文章能够帮你解决 纯CSS3怎么给文本添加背景图 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did200138