css3漸變屬性-Gradients
CSS3 漸變(gradients)可以讓你在兩個或多個指定的顏色之間顯示平穩的過渡。
CSS3 定義了兩種類型的漸變(gradients):
- 線性漸變(Linear Gradients)- 向下/向上/向左/向右/對角方向
- 徑向漸變(Radial Gradients)- 由它們的中心定義
一、linear-gradient(線性漸變)📌
為了創建一個線性漸變,你必須至少定義兩種顏色節點。顏色節點即你想要呈現平穩過渡的顏色。同時,你也可以設置一個起點和一個方向(或一個角度)
語法:
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);
direction表示漸變方向,color-stop1、color-stop2表示漸變顏色
1.默認情況下,線性漸變是從上到下,也就是可以不用設置direction參數
div{ height: 200px; background-image: linear-gradient(red, blue); }
從頂部開始的線性漸變。起點是紅色,慢慢過渡到藍色:
2.設置direction參數,改變線性漸變方向
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ margin: 20px; display: inline-block; width: 150px; height: 150px; background-color: red; text-align: center; line-height: 150px; } .box1{ /* 默認從上到下,二者效果一致 */ /* background-image: linear-gradient(red, blue); */ background-image: linear-gradient(to bottom, red, blue); } .box2{ /* 向上漸變 */ background-image: linear-gradient(to top, red, blue); } .box3{ /* 向右漸變 */ background-image: linear-gradient(to right, red, blue); } .box4{ /* 向左漸變 */ background-image: linear-gradient(to left, red, blue); } .box5{ /* 對角線漸變 */ background-image: linear-gradient(to left bottom, red, blue); } .box6{ /* 對角線漸變 */ background-image: linear-gradient(to right top, red, blue); } </style> </head> <body> <div class="box1">默認(從上到下)</div> <div class="box2">to top(向上)</div> <div class="box3">to right(向右)</div> <div class="box4">to left(向左)</div> <div class="box5">左下角漸變</div> <div class="box6">右上角漸變</div> </body> </html>
當然,除了通過上述關鍵字來指定漸變方向,還可以用角度來指定漸變方向(角度)==>
.box1{ background-image: linear-gradient(0deg, red, blue); } .box2{ background-image: linear-gradient(45deg, red, blue); } .box3{ background-image: linear-gradient(-45deg, red, blue); } .box4{ background-image: linear-gradient(90deg, red, blue); } .box5{ background-image: linear-gradient(180deg, red, blue); }
3.使用多個顏色漸變
.box1{ background-image: linear-gradient(red, green, blue); } .box2{ background-image: linear-gradient(red, orange, yellow, green, blue, indigo, violet) } .box3{ background-image: linear-gradient(red 10%, green 85%, blue 90%); }
注意: 當指定百分比時,顏色是不均勻分布。
4.使用透明度漸變
CSS3 漸變也支持透明度(transparent),可用於創建減弱變淡的效果。
background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
5.重復線性漸變
將linear-gradient前面加上repeating(repeating-linear-gradient)即可實現 重復
線性漸變
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ margin: 20px; display: inline-block; width: 150px; height: 150px; text-align: center; line-height: 150px; } .box1{ background-image: repeating-linear-gradient(to right ,red, blue 10%, green 20%); } .box2{ background-image: repeating-linear-gradient(45deg,red,blue 7%,green 10%); } .box3{ background-image: repeating-linear-gradient(190deg,red,blue 10%,green 10%); } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> </body> </html>
二、radial-gradient(徑向漸變)📍
為了創建一個徑向漸變,你也必須至少定義兩種顏色節點。顏色節點即你想要呈現平穩過渡的顏色。同時,你也可以指定漸變的中心、形狀(圓形或橢圓形)、大小。
默認情況下,漸變的中心是 center(表示在中心點),漸變的形狀是 ellipse(表示橢圓形),漸變的大小是 farthest-corner(表示到最遠的角落)。
語法:
background-image: radial-gradient(shape size at position, start-color, ..., last-color);
1.默認情況下,徑向漸變從內向外分布
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ margin: 20px; display: inline-block; width: 150px; height: 150px; text-align: center; line-height: 150px; } .box1{ /* 均勻分布 */ background-image: radial-gradient(red, blue); } .box2{ /* 不均勻分布 */ background-image: radial-gradient(red 5%, blue 18%, orange 50%); } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> </body> </html>
2.設置形狀(shape)
參數定義了形狀。它可以是值 circle 或 ellipse。其中,circle 表示圓形,ellipse 表示橢圓形。默認值是 ellipse。
.box1{ /* 橢圓形 Ellipse(默認) */ background-image: radial-gradient(red, blue, orange); } .box2{ /* 圓形 Circle */ background-image: radial-gradient(circle, red 5%, blue 18%, orange 50%); }
3.不同尺寸大小關鍵字的使用(size)
size 參數定義了漸變的大小。它可以是以下四個值:
- farthest-side:指定半徑的長度,圓心到最遠的邊漸變過程
- closest-corner:指定半徑的長度,圓心到最近的叫角漸變過程
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div{ margin: 20px; display: inline-block; width: 200px; height: 150px; text-align: center; line-height: 150px; } .box1{ /* 圓心到最近的邊漸變過程 50px 50px表示圓心位置 */ background-image:radial-gradient(closest-side at 50px 50px,red, black); } .box2{ /* 圓心到最遠的邊漸變過程 */ background-image:radial-gradient(farthest-side at 50px 100px,red, black); } .box3{ /* 圓心到最近的角漸變過程 */ background-image:radial-gradient(closest-corner at 100px 50px,red, black); } .box4{ /* 默認 */ background-image:radial-gradient(farthest-corner at 100px 100px,red, black); } </style> </head> <body> <div class="box1"></div> <div class="box2"></div> <div class="box3"></div> <div class="box4"></div> </body> </html>
4. 重復徑向漸變
repeating-radial-gradient() 函數用於重復徑向漸變
.box1{ background-image:repeating-radial-gradient(red 0px,red 15px,blue 15px,blue 30px); } .box2{ background-image:repeating-radial-gradient(red 2%,black 15%,blue 20%); }
至此,以上就是關於css3漸變屬性Gradients~~~