How can I style the currently selected link in my Webflow Navbar without a Current state class? I want to differentiate the link for the active page, such as adding a line below the link text.

TL;DR
  • Determine your website's URL structure to identify the active page.
  • Access the Project Settings in Webflow and go to the Custom Code tab.
  • Add custom CSS in the Head Code section using a <style> tag for styling.
  • Insert JavaScript in the Footer Code to dynamically add an active-link class to the current link based on the URL path.
  • Publish and test your site to ensure the active link styling works correctly.

Styling the currently selected link without using the Current state class can be accomplished using custom code. Here's how you can achieve this in Webflow.

1. Identify the Active Page URL

  • First, determine the URL structure of your website. You will need this for the custom code to recognize which page is active.

2. Access Custom Code Block

  • Go to Project Settings in your Webflow Dashboard.
  • Navigate to the Custom Code tab.

3. Add Custom CSS for Active Link

  • Use a <style> tag in the Head Code section to add custom CSS. This might look something like:

  ```css

  <style>

  a.active-link {

    border-bottom: 2px solid #000; / Add line below /

  }

  </style>

  ```

4. Attach JavaScript to Determine Active Page

  • Scroll to the Footer Code and insert the JavaScript needed to add an active-link class to the currently selected link.

  ```html

  <script>

    document.addEventListener("DOMContentLoaded", function() {

      var currentUrl = window.location.pathname;

      var navLinks = document.querySelectorAll("nav a");

      navLinks.forEach(function(link) {

        if (link.getAttribute("href") === currentUrl) {

          link.classList.add("active-link");

        }

      });

    });

  </script>

  ```

5. Publish and Test

  • Publish your site for changes to take effect.
  • Test the navigation links by visiting different pages to ensure the style applies to the correct link.

Summary

By adding custom CSS and JavaScript, you can style the active link in your Webflow Navbar without relying on the Current state class. The method involves checking the URL path and dynamically adding a class to the link that matches the active page.

Rate this answer

Other Webflow Questions