今天我們我們來看看使用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怎么給文本添加背景圖的詳細內容。(拼多多培訓)