<gradient>
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
Kiểu dữ liệu <gradient> trong CSS là một kiểu đặc biệt của <image> bao gồm sự chuyển đổi dần dần giữa hai hoặc nhiều màu sắc.
Try it
background: linear-gradient(#f69d3c, #3f87a6);
background: radial-gradient(#f69d3c, #3f87a6);
background: repeating-linear-gradient(#f69d3c, #3f87a6 50px);
background: repeating-radial-gradient(#f69d3c, #3f87a6 50px);
background: conic-gradient(#f69d3c, #3f87a6);
<section class="display-block" id="default-example">
<div id="example-element"></div>
</section>
#example-element {
min-height: 100%;
}
Một gradient CSS không có kích thước nội tại; tức là nó không có kích thước tự nhiên hay ưa thích, cũng không có tỷ lệ ưa thích. Kích thước cụ thể của nó sẽ khớp với kích thước của phần tử mà nó áp dụng.
Cú pháp
Kiểu dữ liệu <gradient> được định nghĩa bằng một trong các loại hàm được liệt kê bên dưới.
Gradient tuyến tính
Gradient tuyến tính chuyển đổi màu sắc dần dần dọc theo một đường thẳng tưởng tượng. Chúng được tạo ra bằng hàm linear-gradient().
Gradient hướng tâm
Gradient hướng tâm chuyển đổi màu sắc dần dần từ một điểm trung tâm (gốc). Chúng được tạo ra bằng hàm radial-gradient().
Gradient hình nón
Gradient hình nón chuyển đổi màu sắc dần dần xung quanh một vòng tròn. Chúng được tạo ra bằng hàm conic-gradient().
Gradient lặp lại
Gradient lặp lại nhân đôi một gradient nhiều lần khi cần thiết để lấp đầy một vùng nhất định. Chúng được tạo ra bằng các hàm repeating-linear-gradient(), repeating-radial-gradient(), và repeating-conic-gradient().
Nội suy
Như với bất kỳ phép nội suy nào liên quan đến màu sắc, các gradient được tính toán trong không gian màu alpha-premultiplied. Điều này ngăn các sắc thái xám không mong muốn xuất hiện khi cả màu sắc lẫn độ trong suốt đều thay đổi. (Lưu ý rằng các trình duyệt cũ có thể không sử dụng hành vi này khi sử dụng từ khóa transparent.)
Cú pháp chính thức
<gradient> =
<linear-gradient()> |
<repeating-linear-gradient()> |
<radial-gradient()> |
<repeating-radial-gradient()> |
<conic-gradient()> |
<repeating-conic-gradient()>
<linear-gradient()> =
linear-gradient( [ <linear-gradient-syntax> ] )
<repeating-linear-gradient()> =
repeating-linear-gradient( [ <linear-gradient-syntax> ] )
<radial-gradient()> =
radial-gradient( [ <radial-gradient-syntax> ] )
<repeating-radial-gradient()> =
repeating-radial-gradient( [ <radial-gradient-syntax> ] )
<conic-gradient()> =
conic-gradient( [ <conic-gradient-syntax> ] )
<repeating-conic-gradient()> =
repeating-conic-gradient( [ <conic-gradient-syntax> ] )
<linear-gradient-syntax> =
[ [ <angle> | <zero> | to <side-or-corner> ] || <color-interpolation-method> ]? , <color-stop-list>
<radial-gradient-syntax> =
[ [ [ <radial-shape> || <radial-size> ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <color-stop-list>
<conic-gradient-syntax> =
[ [ [ from [ <angle> | <zero> ] ]? [ at <position> ]? ] || <color-interpolation-method> ]? , <angular-color-stop-list>
<side-or-corner> =
[ left | right ] ||
[ top | bottom ]
<color-interpolation-method> =
in [ <rectangular-color-space> | <polar-color-space> <hue-interpolation-method>? | <custom-color-space> ]
<color-stop-list> =
<linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#?
<radial-shape> =
circle |
ellipse
<radial-size> =
<radial-extent> |
<length [0,∞]> |
<length-percentage [0,∞]>{2}
<position> =
<position-one> |
<position-two> |
<position-four>
<angular-color-stop-list> =
<angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#?
<rectangular-color-space> =
srgb |
srgb-linear |
display-p3 |
display-p3-linear |
a98-rgb |
prophoto-rgb |
rec2020 |
lab |
oklab |
<xyz-space>
<polar-color-space> =
hsl |
hwb |
lch |
oklch
<hue-interpolation-method> =
[ shorter | longer | increasing | decreasing ] hue
<custom-color-space> =
<dashed-ident>
<linear-color-stop> =
<color> <color-stop-length>?
<linear-color-hint> =
<length-percentage>
<radial-extent> =
closest-corner |
closest-side |
farthest-corner |
farthest-side
<length-percentage> =
<length> |
<percentage>
<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>
<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}
<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}
<angular-color-stop> =
<color> <color-stop-angle>?
<angular-color-hint> =
<angle-percentage> |
<zero>
<xyz-space> =
xyz |
xyz-d50 |
xyz-d65
<color-stop-length> =
<length-percentage>{1,2}
<color-stop-angle> =
[ <angle-percentage> | <zero> ]{1,2}
<angle-percentage> =
<angle> |
<percentage>
Ví dụ
>Ví dụ gradient tuyến tính
Một gradient tuyến tính.
.linear-gradient {
background: linear-gradient(
to right,
red,
orange,
yellow,
green,
blue,
indigo,
violet
);
}
Ví dụ gradient hướng tâm
Một gradient hướng tâm.
.radial-gradient {
background: radial-gradient(red, yellow, dodgerblue);
}
Ví dụ gradient hình nón
Một ví dụ gradient hình nón.
.conic-gradient {
background: conic-gradient(pink, coral, lime);
}
Ví dụ gradient lặp lại
Ví dụ về gradient tuyến tính và hướng tâm lặp lại.
.linear-repeat {
background: repeating-linear-gradient(
to top left,
pink,
pink 5px,
white 5px,
white 10px
);
}
.radial-repeat {
background: repeating-radial-gradient(
lime,
lime 15px,
white 15px,
white 30px
);
}
.conic-repeat {
background: repeating-conic-gradient(lime, pink 30deg);
}
Thông số kỹ thuật
| Specification |
|---|
| CSS Images Module Level 4> # gradients> |