Tips and methods of using CSS to implement shadow effects when the mouse is hovering, specific code examples are required
In web design, the mouseover effect is a common interaction One of the ways. By having elements display specific effects on mouse hover, you can increase the user experience and the attractiveness of your website. Among them, using CSS to implement shadow effects when the mouse is hovering is a common and simple method. This article will introduce how to implement this technique and give specific code examples.
1. Simple shadow effect
First, we need to use CSS to define a basic style, and then add additional shadow effects when the mouse hovers. Here is a sample code to implement a simple shadow effect:
HTML:
CSS:
.box {
width: 200px;
height: 200px;
background-color: #f1f1f1;
transition: box-shadow 0.3s;
}
.box:hover {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
Code Description:
2. Multi-layer shadow effect
If we need to achieve multi-layer shadow effects, we can use multiple box-shadow attributes to superimpose. The following is a sample code to achieve a multi-layer shadow effect:
HTML:
CSS:
.box {
width: 200px;
height: 200px;
background-color: #f1f1f1;
transition: box-shadow 0.3s;
}
.box:hover {
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2),
0 5px 15px rgba(0, 0, 0, 0.4), 0 10px 20px rgba(0, 0, 0, 0.6);
}
Code Description:
HTML:
CSS:
.box {
height: 200px;
background-color: #f1f1f1;
transition: box-shadow 0.3s;
position: relative;
overflow: hidden;
}
.box::before {
content: "";
width: 100%;
height: 100% ;
background-color: rgba(0, 0, 0, 0.4);
transform: rotate(-45deg);
top: 50%;
left: -100%;
z-index: -1;
transition: transform 0.3s;
}
.box:hover::before {
transform: rotate(45deg);
}
Code description:
The HTML part is the same as the previous example.
The above is the detailed content of Tips and methods to use CSS to achieve shadow effects when the mouse is hovering. For more information, please follow other related articles on the PHP Chinese website!