Home > Web Front-end > CSS Tutorial > How to use CSS to achieve the shadow effect of elements

How to use CSS to achieve the shadow effect of elements

WBOY
Release: 2023-11-21 18:36:37
Original
2277 people have browsed it

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>
Copy after login

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 class="shadow-title">投影效果标题</h1>
</body>
</html>
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template