Creating The “Moving Highlight” Navigation Bar With JavaScript And CSS<\/h1>\nBlake Lundquist<\/address>\n 2025-06-11T13:00:00+00:00
\n 2025-06-12T20:33:08+00:00
\n <\/header>\n
I recently came across an old jQuery tutorial demonstrating a \u201cmoving highlight\u201d navigation bar<\/strong> and decided the concept was due for a modern upgrade. With this pattern, the border around the active navigation item animates directly from one element to another as the user clicks on menu items. In 2025, we have much better tools to manipulate the DOM via vanilla JavaScript. New features like the View Transition API<\/a> make progressive enhancement more easily achievable and handle a lot of the animation minutiae.<\/p>\n
<\/a>(Large preview<\/a>)<\/figcaption><\/figure>\nIn this tutorial, I will demonstrate two methods of creating the \u201cmoving highlight\u201d navigation bar using plain JavaScript and CSS. The first example uses the getBoundingClientRect<\/code> method to explicitly animate the border between navigation bar items when they are clicked. The second example achieves the same functionality using the new View Transition API.<\/p>\nThe Initial Markup<\/h2>\n
Let\u2019s assume that we have a single-page application where content changes without the page being reloaded. The starting HTML and CSS are your standard navigation bar with an additional div<\/code> element containing an id<\/code> of #highlight<\/code>. We give the first navigation item a class of .active<\/code>.<\/p>\n\nSee the Pen [Moving Highlight Navbar Starting Markup [forked]](https:\/\/codepen.io\/smashingmag\/pen\/EajQyBW) by Blake Lundquist<\/a>.<\/p>See the Pen Moving Highlight Navbar Starting Markup [forked]<\/a> by Blake Lundquist<\/a>.<\/figcaption><\/figure>\nFor this version, we will position the #highlight<\/code> element around the element with the .active<\/code> class to create a border. We can utilize absolute<\/code> positioning and animate the element across the navigation bar to create the desired effect. We\u2019ll hide it off-screen initially by adding left: -200px<\/code> and include transition<\/code> styles for all properties so that any changes in the position and size of the element will happen gradually.<\/p>\n#highlight {\n z-index: 0;\n position: absolute;\n height: 100%;\n width: 100px;\n left: -200px;\n border: 2px solid green;\n box-sizing: border-box;\n transition: all 0.2s ease;\n}\n<\/code><\/pre>\n\n
\n 2025-06-12T20:33:08+00:00
\n <\/header>\n

In this tutorial, I will demonstrate two methods of creating the \u201cmoving highlight\u201d navigation bar using plain JavaScript and CSS. The first example uses the Let\u2019s assume that we have a single-page application where content changes without the page being reloaded. The starting HTML and CSS are your standard navigation bar with an additional See the Pen [Moving Highlight Navbar Starting Markup [forked]](https:\/\/codepen.io\/smashingmag\/pen\/EajQyBW) by Blake Lundquist<\/a>.<\/p> For this version, we will position the getBoundingClientRect<\/code> method to explicitly animate the border between navigation bar items when they are clicked. The second example achieves the same functionality using the new View Transition API.<\/p>\n
The Initial Markup<\/h2>\n
div<\/code> element containing an
id<\/code> of
#highlight<\/code>. We give the first navigation item a class of
.active<\/code>.<\/p>\n
#highlight<\/code> element around the element with the
.active<\/code> class to create a border. We can utilize
absolute<\/code> positioning and animate the element across the navigation bar to create the desired effect. We\u2019ll hide it off-screen initially by adding
left: -200px<\/code> and include
transition<\/code> styles for all properties so that any changes in the position and size of the element will happen gradually.<\/p>\n
#highlight {\n z-index: 0;\n position: absolute;\n height: 100%;\n width: 100px;\n left: -200px;\n border: 2px solid green;\n box-sizing: border-box;\n transition: all 0.2s ease;\n}\n<\/code><\/pre>\n