search
HomeWeb Front-endCSS TutorialHow to use CSS to achieve the shadow effect of elements

How to use CSS to achieve the shadow effect of elements

The method of using CSS to achieve the projection effect of elements requires specific code examples

In web design, the projection effect can add a sense of three-dimensionality and layering to page elements. Make the page richer and more vivid. CSS (Cascading Style Sheets) provides a variety of methods and properties for implementing drop shadow effects.

1. The box-shadow attribute
The box-shadow attribute is a new attribute in CSS3 that is used to achieve element shadow effects. By adding the box-shadow attribute to an element, you can achieve a shadow effect on the element, making the element float out of the page and enhancing the three-dimensional effect.

The syntax of the box-shadow attribute is as follows:
box-shadow: h-shadow v-shadow blur spread color inset;

The specific meaning of each parameter is as follows:

  1. h-shadow: horizontal shadow position, which can be positive (to the right) or negative (to the left);
  2. v-shadow: vertical shadow position, which can be positive (to the left) down) or negative value (upward);
  3. blur: blur distance, the value is between 0 and positive infinity. The larger the value, the higher the degree of blur;
  4. spread: shadow Size expansion. When the value is negative, the shadow will shrink to the inside of the element. When the value is positive, the shadow will expand to the outside of the element.
  5. color: Shadow color, you can use color value, RGB value or Hexadecimal value;
  6. inset: Optional value, if specified, the shadow effect will become an inner shadow.

The following is a specific code example that implements a text box with a shadow effect:

<!DOCTYPE html>
<html>
<head>
    <style>
        .shadow-box {
            width: 200px;
            height: 40px;
            padding: 10px;
            border: none;
            border-radius: 5px;
            box-shadow: 2px 2px 5px 2px rgba(0, 0, 0, 0.5);
        }
    </style>
</head>
<body>
    <input type="text" class="shadow-box" placeholder="请输入内容">
</body>
</html>

In the above code, by adding class to the input box as shadow- The box style implements a text box with a shadow effect. The value of the box-shadow attribute is set to "2px 2px 5px 2px rgba(0, 0, 0, 0.5)", which means that the horizontal and vertical shadow positions are both 2px, the blur distance is 5px, the shadow size is expanded to 2px, and the shadow color is black , with a transparency of 0.5.

2. Text-shadow attribute
In addition to the box-shadow attribute, CSS also provides the text-shadow attribute to achieve the shadow effect of text. By adding the text-shadow attribute to the text element, you can add a shadow effect to the text to make it more prominent and three-dimensional.

The syntax of the text-shadow attribute is as follows:
text-shadow: h-shadow v-shadow blur color;

The specific meaning of each parameter is as follows:

  1. h-shadow: horizontal shadow position, which can be positive (to the right) or negative (to the left);
  2. v-shadow: vertical shadow position, which can be positive (downward) Or negative value (upward);
  3. blur: blur distance, the value is between 0 and positive infinity. The larger the value, the higher the degree of blur;
  4. color: shadow color, You can use color values, RGB values, or hexadecimal values.

The following is a specific code example that implements a title with a shadow effect:

<!DOCTYPE html>
<html>
<head>
    <style>
        .shadow-title {
            font-size: 24px;
            color: #333;
            text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
        }
    </style>
</head>
<body>
    <h1 id="投影效果标题">投影效果标题</h1>
</body>
</html>

In the above code, add the class shadow-title to the title element The style implements a title with a drop shadow effect. The value of the text-shadow attribute is set to "2px 2px 5px rgba(0, 0, 0, 0.5)", which means that the horizontal and vertical shadow positions are both 2px, the blur distance is 5px, the shadow color is black, and the transparency is 0.5.

Through the above code examples, we can see that the shadow effect of elements can be easily achieved by using the box-shadow property and text-shadow property of CSS. In actual development, we can adjust parameter values ​​and color settings according to specific needs to achieve different projection effects to enhance the visual effects and user experience of the page.

The above is the detailed content of How to use CSS to achieve the shadow effect of elements. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

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

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools