Home >Web Front-end >CSS Tutorial >How Can I Prevent Horizontal Menu Items from Shifting When Text Becomes Bold on Hover?
When elements within a horizontal menu shift upon hovering, it can be attributed to the variation in size between regular and bold text. This article explores the solution to this common problem.
In the example provided, HTML lists and CSS are used to create a horizontal menu. When hovering over links, the text becomes bold, causing the links to shift because of the difference in width between regular and bolded text.
The solution involves pre-setting the width of the elements using an invisible pseudo-element. This pseudo-element contains the same content and styling as the parent's hover state.
a::before { display: block; content: attr(title); font-weight: bold; height: 0; overflow: hidden; visibility: hidden; }
By using a pseudo-element with these properties, the width of the element is pre-set even before the hover state is applied, eliminating the shifting issue.
The above is the detailed content of How Can I Prevent Horizontal Menu Items from Shifting When Text Becomes Bold on Hover?. For more information, please follow other related articles on the PHP Chinese website!