I'm facing an issue when I try to implement this:https://www.gatsbyjs.com/docs/gatsby-link/#add-custom-styles-for-the-currently-active-link using the ActiveClassName option.
I have created a navbar.js component which contains the links:
import React from "react";
import { Link } from "gatsby";
import navbarStyles from "./navbar.module.css";
export default function Navbar() {
return (
<nav className={navbarStyles.navbar}>
<ul>
<li>
<Link to="/contact/" activeClassName="active">
Contact
</Link>
</li>
<li>
<Link to="/blog/" activeClassName="active">
Blog
</Link>
</li>
</ul>
</nav>
);
}
and Styles are in a navbar.module.css (where I've defined a 'active' class) :
/* navbar */
nav ul li a {
color: #FFF;
}
nav ul li a.active {
color: #000;
}
My navbar link default color is correct #FFF My link is correctly updated with the active class when I go to the corresponding page, eg. :
<a aria-current="page" class="active" href="/contact/">Contact</a>
But .active style is never applied to the active link :( (Black color)
Strange thing here, when I extract the '.active' CSS from navbar.module.css and put it in my global.css file, things are working!
How can I make things work keeping my styles in the CSS modules?
Thanks for your help
Clearly, your styles are being overriten somewhere in your CSS code. Check the importation of the CSS assets and inspect the element to check from where is the current styling is taken (check the computed tab in the inspector).
Your code is perfectly valid and your CSS rule too, despite mixing CSS modules and CSS standard styling. You can also try something like: