As the web continues to evolve, making a beautiful UI is one of the most important tasks to increase customer engagement on your website. One way to improve the appearance of your front-end is to apply gradient shadows in CSS. The two most important methods of applying gradient shading are linear gradients and radial gradients.
Gradient shadows can be used to draw user attention to specific information, apply hover or focus effects, or give a website a Web3 look and feel. In this tutorial, we'll analyze two gradient shades with practical examples.
We will use two important CSS concepts to achieve the effect, one is the filter attribute and the other is the ::after pseudo-class. The pseudo class will be used to create a fake background, while the filter attribute will be used to apply a blur effect to the surrounding background.
In this example we will see how to apply a linear gradient shadow effect on a card.
.classname::after{ background: linear-gradient(direction, color1, [color2, color3.......]); inset: -0.5rem; filter: blur(25px); ....... }
Among them, classname refers to the class assigned to a given label, and the direction attribute indicates which direction the linear arrangement of colors should be arranged. This can be provided as "deg", or using a pre-designed string (e.g. "to right").
Step one: Create the HTML document skeleton of the website and specify class names for labels that require gradient effects.
Step 2: Use the ::after pseudo-class with the same class name as the class name assigned to the tag.
Step 3: Use the Linear−gradient() CSS function to fill the background of the pseudo class with the desired gradient color.
Step 4: To ensure that the pseudo-class is never superimposed on the original class, add a z-index attribute to the pseudo-class with a lower value than the value assigned to the original class.
Step 5: Add a little inset attribute to the pseudo class so that the original class does not completely cover the background.
Step 6: Finally apply the gradient shadow effect and apply blur to the pseudo component.
In this example, we will see how to apply the Radial Gradient Shadow effect to the same card effect and observe the changes.
.classname::after{ background: radial-gradient(color1, [color2, color3.......]); inset: -0.5rem; filter: blur(25px); ....... }
Step one: Create the HTML document skeleton of the website and specify class names for labels that require gradient effects.
Step 2: Use the ::after pseudo-class with the same class name as the class name assigned to the tag.
Step 3: Use the Radial−gradient() CSS function to fill the background of the pseudo class with the desired gradient color.
Step 4: To ensure that the pseudo-class is never superimposed on the original class, add a z-index attribute to the pseudo-class with a lower value than the value assigned to the original class.
Step 5: Add a little inset attribute to the pseudo class so that the original class does not completely cover the background.
Step 6: Finally apply the gradient shadow effect and apply blur to the pseudo component.
The radial gradient color originates from the center of the label, as you can see from the example above, the yellow is completely overlaid by the black background, while some traces of red are found at the midpoint of the side of the card. On the other hand, in a linear gradient, no overlay is observed as it distributes all colors evenly along all edges according to the provided direction.
We can also adjust the values of inset and Blur to increase or decrease the area covered by the gradient effect. More negative values make the gradient more prominent, while blur values make the effect more spread out on the sides.
The above is the detailed content of How to create gradient shadow using CSS?. For more information, please follow other related articles on the PHP Chinese website!