The linear-gradient() CSS function creates an image consisting of a progressive transition between two or more colors along a straight line. Its result is an object of the data type, which is a special kind of .
Bạn đang xem: Css3 gradients
/* A gradient tilted 45 degrees, starting blue and finishing red */linear-gradient(45deg, blue, red);/* A gradient going from the bottom right lớn the đứng đầu left corner, starting blue và finishing red */linear-gradient(to left top, blue, red);/* màu sắc stop: A gradient going from the bottom to top, starting blue, turning green at 40% of its length, and finishing red */linear-gradient(0deg, blue, green 40%, red);/* màu sắc hint: A gradient going from the left khổng lồ right, starting red, getting to lớn the midpoint màu sắc 10% of the way across the length of the gradient, taking the rest of the 90% of the length khổng lồ change to xanh */linear-gradient(.25turn, red, 10%, blue);/* Multi-position màu sắc stop: A gradient tilted 45 degrees, with a red bottom-left half and a xanh top-right half, with a hard line where the gradient changes from red to blue */linear-gradient(45deg, red 0 50%, blue một nửa 100%);
The position of the gradient line"s starting point. If specified, it consists of the word to và up to lớn two keywords: one indicates the horizontal side (left or right), & the other the vertical side (top or bottom). The order of the side từ khoá does not matter. If unspecified, it defaults lớn to bottom.
The values to top, lớn bottom, lớn left, & to right are equivalent to the angles 0deg, 180deg, 270deg, và 90deg, respectively. The other values are translated into an angle.
The gradient line"s angle of direction. A value of 0deg is equivalent to lớn to top; increasing values rotate clockwise from there.
A color-stop"s value, followed by one or two optional stop positions, (each being either a or a along the gradient"s axis).
An interpolation hint defining how the gradient progresses between adjacent màu sắc stops. The length defines at which point between two màu sắc stops the gradient màu sắc should reach the midpoint of the màu sắc transition. If omitted, the midpoint of the màu sắc transition is the midpoint between two màu sắc stops.
Note: Rendering of màu sắc stops in CSS gradients follows the same rules as màu sắc stops in SVG gradients.
Note also that the first example above does not exactly render as depicted in darkedeneurope.com Firefox (particularly version 80.0b3). You"ll have to set the html height property to lớn 100% or 100vh khổng lồ render as depicted.
As with any gradient, a linear gradient has no intrinsic dimensions; i.e., it has no natural or preferred size, nor a preferred ratio. Its concrete kích cỡ will match the form size of the element it applies to.
To create a linear gradient that repeats so as to lớn fill its container, use the repeating-linear-gradient() function instead.
Because s belong lớn the data type, they can only be used where s can be used. For this reason, linear-gradient() won"t work on background-color và other properties that use the data type.
Composition of a linear gradient
A linear gradient is defined by an axis—the gradient line—and two or more color-stop points. Each point on the axis is a distinct color; lớn create a smooth gradient, the linear-gradient() function draws a series of colored lines perpendicular to the gradient line, each one matching the màu sắc of the point where it intersects the gradient line.
The gradient line is defined by the center of the box containing the gradient image và by an angle. The colors of the gradient are determined by two or more points: the starting point, the ending point, and, in between, optional color-stop points.
The starting point is the location on the gradient line where the first màu sắc begins. The ending point is the point where the last màu sắc ends. Each of these two points is defined by the intersection of the gradient line with a perpendicular line passing from the box corner which is in the same quadrant. The ending point can be understood as the symmetrical point of the starting point. These somewhat complex definitions lead khổng lồ an interesting effect sometimes called magic corners: the corners nearest to lớn the starting & ending points have the same color as their respective starting or ending points.Customizing Gradients
By adding more color-stop points on the gradient line, you can create a highly customized transition between multiple colors. A color-stop"s position can be explicitly defined by using a or a . If you don"t specify the location of a color, it is placed halfway between the one that precedes it & the one that follows it. The following two gradients are equivalent.
linear-gradient(red, orange, yellow, green, blue);linear-gradient(red 0%, orange 25%, yellow 50%, green 75%, xanh 100%);
By default, colors transition smoothly from the màu sắc at one màu sắc stop lớn the color at the subsequent màu sắc stop, with the midpoint between the colors being the half way point between the color transition. You can move this midpoint to lớn any position between two màu sắc stops by adding an unlabelled %color hint between the two colors to indicatewhere the middle of the color transition should be. The following example is solid red from the start lớn the 10% mark & solid blue from 90% to the end. Between 10% & 90% the color transitionsfrom red to blue, however themidpoint of the transitionis at the 30% mark rather than 50%as would have happened without the 30% màu sắc hint.
linear-gradient(red 10%, 30%, blue 90%);
If two or more màu sắc stops are at the same location, the transition will be a hard line between the first and last colors declared at that location.
Color stops should be listed in ascending order. Subsequent color stops of lower value will override the value of the previous color stop creating a hard transition. The following changes from red lớn yellow at the 40% mark, và then transitions from yellow to xanh over 25% of the gradient
linear-gradient(red 40%, yellow 30%, xanh 65%);
Multi-position color stop are allowed. A màu sắc can be declared as two adjacent màu sắc stops by including both positions in the CSS declaration. The following three gradients are equivalent:
linear-gradient(red 0%, orange 10%, orange 30%, yellow 50%, yellow 70%, green 90%, green 100%);linear-gradient(red, orange 10% 30%, yellow 50% 70%, green 90%);linear-gradient(red 0%, orange 10% 30%, yellow 50% 70%, green 90% 100%);
By default, if there is no màu sắc with a 0% stop, the first màu sắc declared will be at that point. Similarly, the last color will continue to lớn the 100% mark, or be at the 100% mark if no length has been declared on that last stop.