Home >Web Front-end >CSS Tutorial >How to Create a Hover Effect with an Expanding Bottom Border?
In this question, the objective is to create a hover effect where the bottom border expands when the cursor hovers over the element. The provided code snippet uses a pseudo element with an opacity transition to reveal the border on hover.
Solution:
To achieve the desired effect, we can use CSS transform: scaleX() to expand the border rather than the opacity. Here's how it works:
Create a pseudo element with a bottom border:
h1:after { position: absolute; left: 0; content: ''; height: 40px; width: 275px; border-bottom: solid 3px #019fb6; }
Apply a scaleX() transform with a transition:
h1:after { ... transform: scaleX(0); transition: transform 250ms ease-in-out; }
Expand the border on hover by setting scaleX to 1:
h1:hover:after { transform: scaleX(1); }
Modification for Expansion Direction:
Additionally, we can control the direction of the border expansion by adjusting the transform-origin property of the pseudo element:
To expand from the center:
h1:after { ... transform-origin: 50% 50%; }
To expand from the right:
h1:after { ... transform-origin: 100% 50%; }
To expand from the left:
h1:after { ... transform-origin: 0% 50%; }
The above is the detailed content of How to Create a Hover Effect with an Expanding Bottom Border?. For more information, please follow other related articles on the PHP Chinese website!