In CSS3, you can use the background attribute and linear-gradient() function to implement background linear gradient, the syntax is "background:linear-gradient(gradient direction, color 1, color 2,...);" .
The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.
A gradient is an image that fades smoothly from one color to another, allowing you to show a smooth transition between two or more specified colors. These are often used for subtle coloring in background images, buttons, and many other things.
Gradient specifies the degree of a gradient by defining the start and end points of a gradient line (a gradient line can be geometrically a straight line, a ray, or a spiral, depending on the type of gradient gradient), and then specifying the gradient of points along this line. color. The colors are blended smoothly to fill the rest of the line, and then each type of gradient produces the actual gradient by defining the color of the gradient line using it.
And css3 linear-gradient() can create a linear gradient by specifying the gradient line as a straight line and then placing several colors along the line. We can build an image by creating an infinite canvas and drawing the image using lines perpendicular to the gradient line, with the color of the drawn line being the color of the two intersecting gradient lines. This produces a smooth fade from each color to the next, going in the specified direction.
Use the angle value to specify the direction (or angle) of the gradient.
color-stop1, color-stop2,...
is used to specify the starting and ending colors of the gradient.
The first parameter accepted by this function (characteristic) is the angle of the gradient. It can accept a value representing the angle (available units are deg, rad, grad or turn) or keywords indicating direction (top, right, bottom, left, left top, top right, bottom right or left bottom). The second parameter accepts a series of color nodes (the color of the end point).
Gradient container (gradient box)
A gradient image is different from a traditional background image. It has no dimensions (size restrictions). It is unlimited. Then the visible area of the gradient image is determined by the size of the gradient container.
Normally, if you use linear-gradient for a DOM element's background-image or background, its (gradient) display area is The border-box area of the element (if you don’t know the border-box area of the element, it is recommended to read the box-sizing related documents first). In fact, it is also background-color or the display area where the background image is introduced through the url.
However, if you set a size through CSS's background-size, such as 200px * 200px, then the gradient container (gradient size) is background-sizeSet size200px * 200px. When background-position is not set to another value, it defaults to being displayed in the upper left corner of the DOM element (that is, background-position: left top).
In CSS, gradient is background-image of background, that is to say, the CSS properties that are suitable for background images are suitable for gradients.
Gradient line
In a gradient container, the line that connects the center point of the container and the color stop point is called a gradient line. When introducing the knowledge related to gradient angles in the next section, it will help you better understand gradient lines, so we will introduce more details in the next section.
Gradient angle
Obviously, using linear-gradient controls the direction of the gradient through the angle of the gradient. Next let's learn more details about it.
C point gradient container center point, A is the vertical line passing through C point and passing through CPoint the angle between the gradient lines. This angle is called the gradient angle.
You can define this angle in the following two ways:
Use keywords: to top, to bottom ,to left,to right,to top right,to top left,to bottom rightandto bottom left
Use numbers with units to define the angle, such as 45deg, 1turn, etc.
If you omit the setting of the angle value, the default is to bottom (corresponding to 180deg or .5turn):
In the above example, the gradient angle is not set, and the white to red gradient color gradients from top to bottom. It is the same effect as using the to bottom keyword, as shown below:
The effect of the following two pictures is to use to top and 0deg, and their effects are the same:
Another important point about using the top keyword is that it depends on the size of the gradient container, such as to top right (or other corner keywords).
If you want a gradient from red to blue, the direction is to the top right of the element. Logically, the blue should be in the upper right corner of the element, and the purple gradient in the middle should form a straight line from the upper left corner to the lower right corner. As shown in the figure below:
So to top right does not mean that the gradient line passes through the upper right corner, which means that the gradient angle does not mean 45deg.
That is to say, if linear-gradient uses the top keyword (to top right, to top left, to bottom right and to bottom left), the gradient line first passes through the center point of the element and is connected with The vertices intersect perpendicularly, and the angle formed by the vertical line from the center point is the gradient angle.
Let us see how the gradient line moves when the gradient angle changes dynamically:
The first and last colors in the above picture are placed at the specified positions of the gradient line, and the remaining colors are evenly distributed between the two.
Of course, if it is between 0% and 100%, it is easy for us to control. But there are cases that go beyond this range. For example, in the example above, the last color is at 120% of the gradient line, so the other colors will be evenly distributed based on this position (the default starting position is still 0%, in this example).
If you want to make your browser work more, why can't you specify the position of the colors sequentially on the gradient line? The fact that the color point positions are in accordance with your expected instructions does not prevent you from operating in a non-sequential order. But if the later value is smaller than the previous value, the browser will automatically make corresponding corrections. For example:
Let us start with the first color red, which is positioned at 30% of the gradient line, and the second color orange is at 10%, but this is wrong, as said above, the stopping point of the color is an increment. At this time, the browser will correct the position of the second color. It will be the same as the previous color and will also be distributed at 30% of the gradient line. Then the third color yellow is distributed at 60% of the gradient line, but the fourth color blue immediately following it is 40%. The browser will also correct and set its position to be the same as the previous color position.
Finally, in the above example, the last color blue is in the incorrect position, so the browser will correct its position to be the same as the previous one, in In this case, it is not the adjacent color yellow, nor orange, it will be traced back to the first color red. Therefore, red and blue are both distributed at 30% of the gradient line, so the yellow and orange colors will not be visible.
Tool
The screenshots in the article are all obtained from a simple tool written by Codepen, you can Enter any gradient value in the input box, and you can see the gradient effect as well as the position of the gradient line, gradient angle and gradient color.
Currently this tool still has various flaws and limitations (see comments in JavaScript), so don’t have too high expectations. Of course, you can also improve this tool on this basis to help everyone. Better understand gradients.
The above is the detailed content of How to achieve linear gradient in css3 background. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!
Onenote is one of the best note-taking tools offered by Microsoft. Combined with Outlook and MSTeams, Onenote can be a powerful combination for increasing productivity at work and in personal creative productivity. We have to take notes in a different format, which may be more than just writing things down. Sometimes we need to copy images from different sources and do some editing in our daily work. Images pasted on Onenote can go a long way if you know how to apply the changes. Have you ever encountered a problem when using Onenote that images pasted on Onenote cannot allow you to work easily? This article will look at using images effectively on Onenote. we can
How to skillfully use CSS to build gradient color QR codes? The following article will introduce to you how to use CSS to add color gradients to ordinary black QR codes. I hope it will be helpful to you!
Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.
Microsoft invites WindowsInsider project members in the Canary and Dev channels to test and experience the new Paint application. The latest version number is 11.2306.30.0. The most noteworthy new feature of this version update is the one-click cutout function. Users only need to click once to automatically eliminate the background and highlight the main body of the picture, making it easier for users to perform subsequent operations. The whole step is very simple. The user imports the picture in the new layout application, and then clicks the "removebackground" button on the toolbar to delete the background in the picture. The user can also use a rectangle to select the area to remove the background.
In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".
How to change the default picture background when logging in to win7 system? Tutorial sharing on how to change the default picture background when logging in to win7 system. After setting a login password for our computer, when we turn on the computer and go to the login interface, there will be a picture background. Some users want to modify the background, so how can they modify the background? Many friends don’t know how to operate in detail. The editor below has compiled the steps to change the default picture background when logging in to the win7 system. If you are interested, follow the editor and take a look below! Steps to change the default picture background when logging in to the win7 system 1. First, go to the illustrated path C:WindowsSystem32oobeinfoackgrounds