纯CSS3怎么给文本添加背景图


今天我们我们来看看使用CSS3怎么给文本添加背景图,让文字变得生动好看!在我们想要创建一个较大的文本标题,但不想使用普通又枯燥的颜色来修饰时,非常有用!

我们先来看看效果图:

 

 

下面我们来研究一下是怎么实现这个效果的:

首先是HTML部分,定义两个标题

<body>
	<h1>拼多多培训</h1>
	<h3>拼多多培训</h3>
</body>

 

 然后开始定义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-family:Arial, Helvetica, sans-serif;
}

 

 最后就是给文字添加背景图片:

将文字原本的颜色设置为transparent透明,然后利用background-image属性给文字加背景图片

h1 {
    color: transparent;
    background-image: url("001.jpg");
}

h3{
    color: transparent;
    background-image: url("002");
}

  

 

 

 

 

 发现效果是这样的,不如人意。这是因为缺少了一个关键属性background-clip。background-clip属性是一个CSS3新属性,要添加前缀来兼容其他浏览器

h1 {
    color: transparent;
    background-image: url("001.jpg");
	background-clip: text;
    -webkit-background-clip: text;
}

h3{
    color: transparent;
    background-image: url("002.jpg");
	background-clip: text;
    -webkit-background-clip: text;
}

 

 ok,大功告成!下面附上完整代码:

<html>
<head>
<meta charset="utf-8">
<title></title>
<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-family:Arial, Helvetica, sans-serif;
}
h1 {
    color: transparent;
    background-image: url("001.jpg");
	background-clip: text;
    -webkit-background-clip: text;
}

h3{
    color: transparent;
    background-image: url("002.jpg");
	background-clip: text;
    -webkit-background-clip: text;
}
</style>
</head>
<body>
	<h1>拼多多培训</h1>
	<h3>拼多多培训</h3>
</body>
</html>

因为我们使用的是静态图片,所以是文本背景图效果也是静态的。如果使用动图会有动态效果:

h3{
    color: transparent;
    background-image: url("003.gif"),url("004.gif");
	background-clip: text;
    -webkit-background-clip: text;
}

以上就是纯CSS3怎么给文本添加背景图的详细内容。(拼多多培训


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM