I was building a website using a CMS, and the navbar had a pretty common hover effect: the menu items turned bold when you hovered over them.

Nothing fancy. Simple. Basic, I thought.

Here's a simplified version of how I built it:

 <nav class="navbar"> <a href="#" class="logo">Navbar</a> <div class="nav-links"> <a href="#" class="nav-link">Home</a> <a href="#" class="nav-link">Products</a> <a href="#" class="nav-link">Services</a> <a href="#" class="nav-link">About</a> <a href="#" class="nav-link">Contact</a> </div> </nav> 
.navbar { display: flex; align-items: center; justify-content: space-between; gap: 32px; padding: 20px 24px; background: white; border: 1px solid #ddd; border-radius: 8px; } .logo { font-size: 20px; font-weight: 700; color: #222; text-decoration: none; white-space: nowrap; } .nav-links { display: flex; align-items: center; gap: 24px; } .nav-link { color: #333; font-size: 15px; font-weight: 400; text-decoration: none; white-space: nowrap; } .navbar .nav-link:hover, .navbar .nav-link:focus-visible { font-weight: 700; } 

Then I noticed that the items to its right started shifting slightly.

Navbar with shifting items
Well… changing the font weight can also change the width of the text. And since my navbar items are laid out horizontally, that tiny change can affect the position of everything that comes after it.

I thought changing the font size or weight as a hover effect was pretty straightforward. Turns out, there's a little more to it.

So, here are two potential solutions I explored:

Option 1: Simulate boldness

This is probably the quickest fix.

Instead of actually changing the font weight, we can use text-shadow to make the text look a little thicker. It won't look exactly like real bold text, but for a simple hover indicator, it might be good enough.

step 1 : replace font-weight property with text-shadow

.navbar .nav-link:hover, .navbar .nav-link:focus-visible { /* font-weight: 700; */ text-shadow: 0.35px 0 currentColor, -0.35px 0 currentColor; } 

Here's the result
Bold simulated using text-shadow
Option 2: Reserve the text width

The idea is to reserve enough space for the text in its bold state, even when it's not being hovered.

We'll add an invisible pseudo-element containing the same text, styled with the bold font weight. To keep things flexible, we'll use a data-text attribute to pass the text to our CSS.

step 1: add data-text attribute to each item

 <nav class="navbar"> <a href="#" class="logo">Navbar</a> <div class="nav-links"> <a href="#" class="nav-link" data-text="Home">Home</a> <a href="#" class="nav-link" data-text="Products">Products</a> <a href="#" class="nav-link" data-text="Services">Services</a> <a href="#" class="nav-link" data-text="About">About</a> <a href="#" class="nav-link" data-text="Contact">Contact</a> </div> </nav> 

step 2 : add pseudo-element in css

.navbar .nav-link::after { content: attr(data-text); display: block; font-weight: 700; height: 0; visibility: hidden; overflow: hidden; } 

Make sure you have bring back the font-weight property if you have removed it.
Navbar with reserved space
The invisible pseudo-element reserves the width needed for the bold text. As a result, the link is already wide enough before you hover over it, and the neighboring items no longer shift.

Which one should you use?

Both approaches solve the problem in different ways.

If you don't mind a slight difference in how the text looks, text-shadow is a quick and simple option.

But if you want to keep the actual font weight and avoid the neighboring items shifting, reserving the text width with a pseudo-element is the approach I'd choose.

It's a small detail, but these are the kinds of things that make a UI feel more polished.

What would you use in your navbar?