VOOZH about

URL: https://www.geeksforgeeks.org/bootstrap/how-to-set-active-class-to-nav-menu-from-bootstrap/

⇱ How to set active class to nav menu from bootstrap? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to set active class to nav menu from bootstrap?

Last Updated : 23 Jul, 2025

To dynamically set the active class on Bootstrap navigation menus based on scroll or click events, JavaScript is employed to track the web page's position. Functions are utilized to handle these events, ensuring the active class is applied to the relevant navigation sections accordingly.

Approach

  • The HTML includes Bootstrap CSS and JavaScript libraries for styling and functionality.
  • Sections are defined with IDs and linked in the navigation bar.
  • CSS styles define colors and layouts for each section.
  • JavaScript adds functionality to handle click events on navigation links, toggling the active class dynamically.
  • Additional JavaScript code detects scrolling actions to highlight the active section in the navigation bar based on the user's position on the page.

Example: This example implements the approach mentioned above.

Output:

👁 gif2
Active Navigation Output
Comment

Explore