Syntax:
<linear-gradient> = linear-gradient([ [ <angle> | to <side-or-corner> ] ,]? <color-stop>[, <color-stop>]+)
<side-or-corner> = [left | right] || [top | bottom]
<color-stop> = <color> [ <length> | <percentage> ]?Value:
The following values are used to represent the direction of the gradient, which can be set using angles or keywords:
<angle>: Specify the direction (or angle) of the gradient using an angle value.
to left: Set the gradient from right to left. Equivalent to: 270deg
to right: Set the gradient from left to right. Equivalent to: 90deg
to top: Set the gradient from bottom to top. Equivalent to: 0deg
to bottom: Set the gradient from top to bottom. Equivalent to: 180deg. This is the default value, which is equivalent to leaving it blank. <color-stop> is used to specify the starting and ending colors of the gradient:
<color>: Specify color.
<length>: Use the length value to specify the starting and ending color positions. Negative values are not allowed
<percentage>: Use percentage to specify the starting and ending color positions.
Description: Create an image with a linear gradient.
If you want to create an image with a diagonal gradient, you can use a multi-keyword method like to top left to achieve this.
Draw the simplest linear gradient using the default gradient direction
Sample code:
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
<meta charset="utf-8" />
<title>linear-gradient()_CSS参考手册_web前端开发参考手册系列</title>
<meta name="author" content="https://www.4s5.cn/" />
<style>
div {
width: 200px;
height: 100px;
margin-top: 10px;
border: 1px solid #ddd;
}
.test {
background: linear-gradient(#fff, #333);
}
.test2 {
background: linear-gradient(#000, #f00 50%, #090);
}
.test3 {
background: linear-gradient(0deg, #000 20%, #f00 50%, #090 80%);
}
.test4 {
background: linear-gradient(45deg, #000, #f00 50%, #090);
}
.test5 {
background: linear-gradient(to top right, #000, #f00 50%, #090);
}
</style>
</head>
<body>
<div class="test"></div>
<div class="test2"></div>
<div class="test3"></div>
<div class="test4"></div>
<div class="test5"></div>
</body>
</html>