Home >Web Front-end >CSS Tutorial >How Can I Prevent Element Shifting on Hover When Adding a CSS Border?
Avoiding Element Movement with CSS Borders on Hover
When applying a border on hover to an element, you may encounter a shifting effect as the added border width pushes the element out. To prevent this while maintaining the border, you can employ the following technique:
Solution
To ensure that the element remains in its original position, set the border to transparent by default and only make it visible on hover:
.jobs .item { background: #eee; border: 1px solid transparent; } .jobs .item:hover { background: #e1e1e1; border: 1px solid #d0d0d0; }
This method allows the border to exist without affecting the element's position, creating the desired visual effect without any unwanted movement.
The above is the detailed content of How Can I Prevent Element Shifting on Hover When Adding a CSS Border?. For more information, please follow other related articles on the PHP Chinese website!