'Why are my active classes not working when i use the media screen?
I'm using React on this application and I don't know why I'm having this problem. I created a navbar with some active classname in the links so when the link is active they turn to bold. It's working perfectly when I have the normal screen, but when I try to use a media screen it only works for my first link (home) and I don't know why this isn't working for the rest of the links. So here is my code:
HTML:
import { Link } from "gatsby" import Logo from "../assets/Logo.svg" import "../styles/navbar.css" const Navbar = () => { const [show, setShow] = useState(false) return ( <nav className="navbar"> <Link to="/" className="nav-brandLogo"> <Logo /> </Link> <Link to="#" className="nav-btn" onClick={() => setShow(!show)}> <span className="bar"></span> <span className="bar"></span> <span className="bar"></span> </Link> <div className={show ? "nav-links show-links" : "nav-links"}> <Link to="/" className="nav-link" activeClassName="active-link" onClick={() => setShow(false)} > Home </Link> <Link to="/projects" className="nav-link" activeClassName="active-link" onClick={() => setShow(false)} > Projetos </Link> <Link to="/team" className="nav-link" activeClassName="active-link" onClick={() => setShow(false)} > Nossa Equipe </Link> <div className="nav-link contact-link"> <Link to="/projects" className="btn" onClick={() => setShow(false)}> Entre para a Hut! </Link> </div> </div> </nav> ) } export default NavbarCSS:
* { box-sizing: border-box; } .navbar{ display: flex; position: relative; justify-content: space-between; align-items: center; background-color: #64008A; padding: 0px 3rem; } .nav-brandLogo{ padding-top: 7px; } .nav-hutLogo svg{ max-height: 70px; max-width: 153px; align-items: center; } .nav-btn{ position: absolute; top: 1.5rem; right: 3rem; display: none; flex-direction: column; justify-content: space-between; width: 30px; height: 21px; } .nav-links{ height: 100%; display: flex; flex-direction: row; white-space: nowrap; } .nav-link { font-weight: 400px; display: block; text-decoration: none; color: white; padding-right: 5rem; } .contact-link{ padding: 0; } .nav-link .btn{ padding: 15px; background-color: #96D600; border-radius: 8px; color: white; font-weight: 600; font-size: 16px; } .active-link{ font-weight: bold; } .nav-btn .bar { height: 3px; width: 100%; background-color: white; border-radius: 10px; } @media (max-width: 900px){ .navbar{ flex-direction: column; align-items: flex-start; } .nav-btn{ display: flex; color: white; } .nav-links{ display: none; width: 100%; } .show-links{ display: flex; flex-direction: column; width: 50%; margin: 0 25%; height: 14.2rem; } .nav-link{ text-align: center; color: white; padding: 1rem 0; border-top: 1px solid #cbd3e1; letter-spacing: 1px; } .contact-link{ padding-top: 1.5rem; } .active-link{ font-weight: bold; } }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
