溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CSS3怎么給文本添加背景圖

發(fā)布時間:2021-08-25 09:49:30 來源:億速云 閱讀:99 作者:chen 欄目:web開發(fā)

本篇內(nèi)容主要講解“CSS3怎么給文本添加背景圖”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“CSS3怎么給文本添加背景圖”吧!

今天我們我們來看看使用CSS3怎么給文本添加背景圖,讓文字變得生動好看!在我們想要創(chuàng)建一個較大的文本標(biāo)題,但不想使用普通又枯燥的顏色來修飾時,非常有用!

我們先來看看效果圖:

CSS3怎么給文本添加背景圖

下面我們來研究一下是怎么實現(xiàn)這個效果的:

首先是HTML部分,定義兩個標(biāo)題

<body>
  <h2>Hello world!</h2>
  <h4>Hello world!</h4>
</body>

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怎么給文本添加背景圖

最后就是給文字添加背景圖片:

  • 將文字原本的顏色設(shè)置為transparent透明,然后利用background-image屬性給文字加背景圖片

h2 {
	color: transparent;
	background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg");
  
}
h4{
	color: transparent;
	background-image: url("https://img.php.cn/upload/article/000/000/024/6124c86e0808b298.jpg");
}

CSS3怎么給文本添加背景圖

發(fā)現(xiàn)效果是這樣的,不如人意。這是因為缺少了一個關(guān)鍵屬性background-clip。background-clip屬性是一個CSS3新屬性,要添加前綴來兼容其他瀏覽器

h2 {
	color: transparent;
	background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg");
	background-clip: text;
	-webkit-background-clip: text;
  
}
h4{
	color: transparent;
	background-image: url("https://img.php.cn/upload/article/000/000/024/6124c86e0808b298.jpg");
	background-clip: text;
	-webkit-background-clip: text;
}

CSS3怎么給文本添加背景圖

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

<!DOCTYPE html>
<html>
	<head>
		<meta 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-family: Arial, Helvetica, sans-serif;
			}

			h2 {
				color: transparent;
				background-image: url("https://img.php.cn/upload/article/000/000/024/612360451cede816.jpg");
				background-clip: text;
				-webkit-background-clip: text;

			}

			h4 {
				color: transparent;
				background-image: url("https://img.php.cn/upload/article/000/000/024/6124c86e0808b298.jpg");
				background-clip: text;
				-webkit-background-clip: text;
			}
		</style>
	</head>
	<body>
		<h2>Hello world!</h2>
		<h4>Hello world!</h4>
	</body>
</html>

因為我們使用的是靜態(tài)圖片,所以是文本背景圖效果也是靜態(tài)的。如果使用動態(tài)會有動態(tài)效果:

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怎么給文本添加背景圖”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI