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.
Method 1: Linear Gradient Shadow
In this example we will see how to apply a linear gradient shadow effect on a card.
grammar
.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").
algorithm
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.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Linear Gradient Shadow</title> </head> <body> <div class="gradient"> <h1>Welcome to Tutorials Point</h1> </div> <style> *{ margin: 0px; padding: 0px; } .gradient{ margin-top: 20px; margin-left: 5%; width: 90%; position: relative; border-radius: 10px; padding-top: 15px; padding-bottom: 15px; padding-right: 10px; padding-left: 10px; background-color: black; } h1{ color: white; text-align: center; } .gradient::after{ content: ""; position: absolute; z-index: -100; background: linear-gradient(to right, blue, cyan,lime, green, yellow, orange, red); inset: -0.5rem; filter: blur(25px); } </style> </body> </html>
Method 2: Radial Gradient Shadow
In this example, we will see how to apply the Radial Gradient Shadow effect to the same card effect and observe the changes.
grammar
.classname::after{ background: radial-gradient(color1, [color2, color3.......]); inset: -0.5rem; filter: blur(25px); ....... }
algorithm
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.
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Radial Gradient Shadow</title> </head> <body> <div class="gradient"> <h1>Welcome to Tutorials Point</h1> </div> <style> *{ margin: 0px; padding: 0px; } .gradient{ margin-top: 20px; margin-left: 5%; width: 90%; position: relative; padding-top: 50px; padding-bottom: 50px; border-radius: 10px; padding-right: 10px; padding-left: 10px; background-color: black; } h1{ color: white; text-align: center; } .gradient::after{ content: ""; position: absolute; z-index: -100; background: radial-gradient(yellow, red, blue); inset: -1rem; filter: blur(10px); } </style> </body> </html>
in conclusion
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ToimplementdarkmodeinCSSeffectively,useCSSvariablesforthemecolors,detectsystempreferenceswithprefers-color-scheme,addamanualtogglebutton,andhandleimagesandbackgroundsthoughtfully.1.DefineCSSvariablesforlightanddarkthemestomanagecolorsefficiently.2.Us

The topic differencebetweenem, Rem, PX, andViewportunits (VH, VW) LiesintheirreFerencepoint: PXISFixedandbasedonpixelvalues, emissrelative EtothefontsizeFheelementoritsparent, Remisrelelatotherootfontsize, AndVH/VwarebaseDontheviewporttimensions.1.PXoffersprecis

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

No,CSSdoesnothavetobeinlowercase.However,usinglowercaseisrecommendedfor:1)Consistencyandreadability,2)Avoidingerrorsinrelatedtechnologies,3)Potentialperformancebenefits,and4)Improvedcollaborationwithinteams.

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b
