重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

CSS3怎么给文本添加背景图

本篇内容主要讲解“CSS3怎么给文本添加背景图”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“CSS3怎么给文本添加背景图”吧!

创新互联专注于企业成都营销网站建设、网站重做改版、湘阴网站定制设计、自适应品牌网站建设、H5建站商城系统网站开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为湘阴等各大城市提供网站开发制作服务。

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

我们先来看看效果图:

CSS3怎么给文本添加背景图

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

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


  

Hello world!

  

Hello world!

CSS3怎么给文本添加背景图

然后开始定义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;
}

CSS3怎么给文本添加背景图

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

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

h2 {
	color: transparent;
	background-image: url("/upload/otherpic56/612360451cede816.jpg");
  
}
h4{
	color: transparent;
	background-image: url("/upload/otherpic56/6124c86e0808b298.jpg");
}

CSS3怎么给文本添加背景图

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

h2 {
	color: transparent;
	background-image: url("/upload/otherpic56/612360451cede816.jpg");
	background-clip: text;
	-webkit-background-clip: text;
  
}
h4{
	color: transparent;
	background-image: url("/upload/otherpic56/6124c86e0808b298.jpg");
	background-clip: text;
	-webkit-background-clip: text;
}

CSS3怎么给文本添加背景图

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



	
		
		
			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;
			}

			h2 {
				color: transparent;
				background-image: url("/upload/otherpic56/612360451cede816.jpg");
				background-clip: text;
				-webkit-background-clip: text;

			}

			h4 {
				color: transparent;
				background-image: url("/upload/otherpic56/6124c86e0808b298.jpg");
				background-clip: text;
				-webkit-background-clip: text;
			}
		
	
	
		

Hello world!

Hello world!

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

h4 {
   background-image: url("https://img.php.cn/upload/image/161/146/599/1629799857734746.gif"),
   url("https://img.php.cn/upload/image/817/380/291/1629799861847258.gif");
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

CSS3怎么给文本添加背景图

到此,相信大家对“CSS3怎么给文本添加背景图”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


分享文章:CSS3怎么给文本添加背景图
标题URL:http://cqcxhl.cn/article/gpccdd.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP