Chapter 2 CSS3 gradient effect-original learning points of water:
1. Linear Gradient
2. Radial Gradient
Lecturer: Li Yanhui
This chapter mainly discusses the CSS3 background gradient function in HTML5. There are two main gradient methods: linear gradient and radial (radioactive) gradient.
1. Linear Gradient
CSS3 provides the linear-gradient attribute for gradient of background color. In the past, this effect had to be achieved using images. First, let's take a look at its style sheet as follows:
|
|
Linear-gradient (azimuth, starting color, end color) |
|
Location |
Optional parameter, indicating the orientation of the gradient. The following values can be used: to top, to top right, to right, to bottom, to bottom left, to left, and to top left. |
|
Starting color |
Required parameter, color value |
|
End color |
Required parameter, color value |
// Two required parameters
background-image: linear-gradient(orange,green);
// Add a Orientation
background-image: linear-gradient(to top,orange,green);
Through top, left, right, and bottom, the gradient direction is sometimes relatively single. We can set the orientation using the value of the angle unit. For example, 0 deg (0deg) is equivalent to top; the angle increases along the counterclockwise direction as your angle increases.
// Set the orientation through the angle, 0 ~ Between 360 degrees, which can be a negative value
background-image: linear-gradient(45deg,orange,green);
// Multi-color linear gradient
background-image: linear-gradient(-45deg,orange,green,blue,red);
// Set the multi-color linear position by percentage
background-image: linear-gradient(-45deg, orange 0%, green 20%, blue 40%, red 100%);
By default, the percentage position of the starting color is 0%, the percentage position of the ending color is 100%, and other positions are allocated by the average value. You can also use px pixels to set it, but it is difficult to calculate it.
// Combine the background and use a transparent gradient to implement a strong sense of attention
background-image: linear-gradient(to top right, rgba(0,0,0,0.6), rgba(0,0,0,0));
// Duplicate gradient attribute values
background-image: repeating-linear-gradient(to top, orange 10px, green 30px);
Currently, the latest mainstream browsers support the gradient attribute of CSS3. What is the support for the previous browsers? See the following table:
|
|
Opera |
Firefox |
Chrome |
Safari |
IE |
|
Some support must have a prefix |
11.5 |
None |
4 |
~ 9 |
4 ~ 5 |
None |
|
Prefix required |
None |
3.6 ~ 15 |
10 |
~ 25 |
5.1 ~ 6 |
None |
|
No prefix is supported. |
12.1 + |
16 + |
|
26 + |
6.1 + |
10.0 + |
Some support is mentioned here, indicating that the gradient may not be complete yet, but you can use it by adding a prefix. The details are incomplete and cannot be verified. The version is too old. The complete format with or without a prefix is as follows:
// Add a compatible prefix
background-image: -webkit-linear-gradient(to top, orange, green); background-image: -moz-linear-gradient(to top, orange, green); background-image: -o-linear-gradient(to top, orange, green); background-image: linear-gradient(to top, orange, green);
The value of repeating-linear-gradient is basically the same as that of linear-gradient.
Ii. Radial Gradient
CSS3 provides a radial gradient, also known as a radioactive gradient: the radial-gradient attribute value. It is extended from a point to a circle. The attribute value style table is as follows:
| Radial-gradient (azimuth, starting color, end color) |
| Location |
Optional. radial orientation. Available values include pixels, percentages, fixed values, and compound values. |
| Starting color |
Required parameter, color value |
| End color |
Required parameter, color value |
// Two required parameters
background-image: radial-gradient(orange, green);
// If you want to set the first optional parameter, you can set it to cirlce (Circular) or ellipse (elliptical ). The default is an elliptical shape.
background-image: radial-gradient(circle, orange, green);
|
Shape |
Description |
|
Circle |
Circle |
|
Ellipse |
Elliptical, default |
// You can not only set the shape, but also set the divergence direction of the shape.
background-image: radial-gradient(circle at top, orange, green);
|
Direction |
Description |
|
Top |
Divergence from top |
|
Left |
Divergence from left |
|
Right |
Divergence from right side |
|
Bottom |
Divergence from bottom |
|
Center |
Divergence from Center |
// You can also combine the orientation, such as the lower right corner.
background-image: radial-gradient(circle at right bottom, orange, green);
// You can set the Divergence Distance, that is, the radius of the circle.
background-image: radial-gradient(circle closest-side, orange, green);
|
Radius keyword |
Description |
|
Closest-side |
Specify the radius length of the radial gradient to the edge closest to the center of the circle. |
|
Closest-corner |
Specify the radius length of the radial gradient from the center of the center to the nearest corner of the center. |
|
Farthest-side |
Specify the radius length of the radial gradient as the farthest side from the center of the center to the center of the center. |
|
Farthest-corner |
Specify the radius length of the radial gradient to the farthest angle from the center of the center to the center of the center. |
// The keyword is a bit ambiguous. The radius can be expressed in pixels, but the percentage is not accepted.
background-image: radial-gradient(circle 50px, orange, green);
// Similarly, there are repeated background Methods
background-image: repeating-radial-gradient(circle 50px, orange, green);
// Compatibility mode
background-image: -webkit-radial-gradient(circle, orange, green); background-image: -moz-radial-gradient(circle, orange, green); background-image: -o-radial-gradient(circle, orange, green); background-image: radial-gradient(circle, orange, green);
// If two duplicate backgrounds are added with a prefix, the compatibility mode is enabled.
background-image: -webkit-repeating-radial-gradient; background-image: -moz-repeating-radial-gradient;background-image: -o-repeating-radial-gradient;background-image: repeating-radial-gradient;