For some reason my NAV Links are being displayed at the bottom of the screen in hamburger view. Is there a way to display them at the top of the screen? Ive tried removing "justify-content:end" but as in doing so it also messes up the non mobile view. How do I fix this issue without effecting the full screen mode?
Navbar.js Below
import React, { useState, useEffect } from 'react';
import { Link } from "react-router-dom";
import { Button } from './Button';
import './Navbar.css';
function Navbar() {
const [click, setClick] = useState(false);
const [button, setButton] = useState(true);
const handleClick = () => setClick(!click);
const closeMobileMenu = () => setClick(false);
const showButton = () => {
if (window.innerWidth <= 960) {
setButton(false);
} else {
setButton(true);
}
};
useEffect(() => {
showButton()
}, []);
window.addEventListener('resize', showButton);
return (
<>
<nav className='navbar'>
<div className='navbar-container'>
<Link to='/' className='navbar-logo' onClick={closeMobileMenu}>
Voyage
<i class="fa-solid fa-anchor"/>
</Link>
<div className='menu-icon' onClick={handleClick}>
<i className={click ? 'fas fa-times' : 'fas fa-bars'} />
</div>
<ul className={click ? 'nav-menu active' : 'nav-menu'}>
<li className='nav-item'>
<Link to='/' className='nav-links' onClick={closeMobileMenu}>
Home
</Link>
</li>
<li className='nav-item'>
<Link
to='/services'
className='nav-links'
onClick={closeMobileMenu}
>
Services
</Link>
</li>
<li className='nav-item'>
<Link
to='/products'
className='nav-links'
onClick={closeMobileMenu}
>
Products
</Link>
</li>
<li>
<Link
to='/sign-up'
className='nav-links-mobile'
onClick={closeMobileMenu}
>
Sign Up
</Link>
</li>
</ul>
{button && <Button buttonStyle='btn--outline'>SIGN UP</Button>}
</div>
</nav>
</>
);
}
export default Navbar;
NavBar.css Below
.navbar {
background: linear-gradient(90deg, rgb(28, 27, 27) 0%, rgb(26, 23, 23) 100%);
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
position: sticky;
top: 0;
z-index: 999;
}
.navbar-container {
display: flex;
justify-content: center;
align-items: center;
height: 80px;
max-width: 1500px;
}
.navbar-logo {
color: #fff;
justify-self: start;
margin-left: 20px;
cursor: pointer;
text-decoration: none;
font-size: 2rem;
display: flex;
align-items: center;
}
.fa-anchor {
margin-left: 0.6rem;
font-size: 2.1rem;
}
.nav-menu {
display: grid;
grid-template-columns: repeat(4, auto);
grid-gap: 10px;
list-style: none;
text-align: center;
width: 60vw;
justify-content: end;
margin-right: 2rem;
}
.nav-item {
height: 80px;
}
.nav-links {
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0.5rem 1rem;
height: 100%;
}
.nav-links:hover {
border-bottom: 4px solid #fff;
transition: all 0.2s ease-out;
}
.fa-bars {
color: #fff;
}
.nav-links-mobile {
display: none;
}
.menu-icon {
display: none;
}
#media screen and (max-width: 960px) {
.NavbarItems {
position: relative;
}
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
height: 90vh;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
}
.nav-menu.active {
background: #242222;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav-links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav-links:hover {
background-color: #fff;
color: #242424;
border-radius: 0;
}
.navbar-logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%);
}
.menu-icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav-links-mobile {
display: block;
text-align: center;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
text-decoration: none;
font-size: 1.5rem;
background-color: transparent;
color: #fff;
padding: 14px 20px;
border: 1px solid #fff;
transition: all 0.3s ease-out;
}
.nav-links-mobile:hover {
background: #fff;
color: #242424;
transition: 250ms;
}
}
Related
<body>
<header class="main-header">
<div class="container">
<img src="images/logo.svg" alt="Manage">
<button class="toggle-button">
<span class="toggle-button__bar"></span>
<span class="toggle-button__bar"></span>
<span class="toggle-button__bar"></span>
</button>
<nav class="main-nav">
<ul role="list" class="main-nav__list">
<li class="main-nav__list--item">Pricing</li>
<li class="main-nav__list--item">Product</li>
<li class="main-nav__list--item">About Us</li>
<li class="main-nav__list--item">Careers</li>
<li class="main-nav__list--item">Community</li>
</ul>
</nav>
<button class="button button-header">Get Started</button>
</div>
</header>
<script type="module" src="/script/script.js"></script>
/* CSS/SASS code
I do not know why my logo/image is being placed in the middle of the header, instead of
the top left corner of the header. I do not know how to fix this.
If anybody has some nice suggestions, feel free to tell */
#mixin display-flex() {
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
align-items: center;
}
#mixin media-min-width($width) {
#media (min-width: $width) {
#content;
}
}
.main-header {
position: relative;
padding: $pad-header 0;
.container {
#include display-flex();
justify-content: space-around;
a {
display: inline-block;
text-decoration: none;
font-size: $fs-nav;
font-weight: $fw-semi-bold;
}
.main-nav {
display: none;
#include media-min-width(50rem) {
display: block;
}
}
.button {
color: hsl(0deg, 0%, 98%);
font-weight: 700;
background: hsl(12deg, 88%, 59%);
padding: 0.7rem 1rem;
border: 0;
border-radius: 100vmax;
&:hover {
cursor: pointer;
}
:focus {
outline: none;
}
}
.button-header {
display: none;
#include media-min-width(50rem) {
display: block;
}
}
.toggle-button {
position: absolute;
top: 50%;
right: 2%;
transform: translateY(-50%);
background: transparent;
border: 0;
cursor: pointer;
#include media-min-width(50rem) {
display: none;
}
.toggle-button__bar {
display: block;
background: $clr-secondary;
width: 2rem;
height: 0.3rem;
margin: 0.3rem 0;
&:last-of-type {
margin: 0;
}
}
}
}
}
.main-nav__list {
#include display-flex();
justify-content: center;
gap: clamp(1.7rem, 1.9rem, 2.5rem);
.main-nav__list--item a {
cursor: pointer;
}
}
I'm struggling to get my react menu toggler to close on click of a list item. For instance, once someone opens the menu I would like it to close when they click "about", "solutions", etc. Also for some reason, the entire menu is not opening on my phone but in dev tools mobile view it works fine. On mobile it's stuck at the top and only shows what seems to be a strip of the bottom of the menu but nothing more. Any reason this may be happening?
const [active, setActive] = useState("nav__menu");
const [icon, setIcon] = useState("nav__toggler");
const navToggle = () => {
if (active === "nav__menu") {
setActive("nav__menu nav__active");
} else setActive("nav__menu");
// Icon Toggler
if (icon === "nav__toggler") {
setIcon("nav__toggler toggle");
} else setIcon("nav__toggler");
};
return (
<div className="header">
<nav className="nav">
<Link className='nav-logo' to='/'>
<img src={NavLogo} alt="Home" className="nav-logo-img" />
</Link>
<ul className={active}>
<li className="nav__item">
<NavLink exact="true" to="/about">
<div className="nav-link">About</div>
</NavLink>
</li>
<li className="nav__item">
<NavLink exact="true" to="/solutions">
<div className="nav-link">Solutions</div>
</NavLink>
</li>
<li className="nav__item">
<Link to="/consultation" className='menu-consult-btn'>
<img src={BtnDroplet}className="btn-droplet" />
<h4>Free Consultation</h4>
</Link>
</li>
</ul>
<div onClick={navToggle} className={icon}>
<div className="line1"></div>
<div className="line2"></div>
<div className="line3"></div>
</div>
</nav>
</div>
);
/* Header Component */
.header{
width: 100%;
height: 100px;
background-color: var(--bg-color);
position: fixed;
z-index: 1000000000;
}
.nav {
display: flex;
align-items: center;
justify-content: space-between;
width: 80%;
margin: auto;
padding: 25px 0;
z-index: 999999;
}
.nav-logo-img{
width: 35%;
}
.nav__menu {
display: flex;
align-items: center;
gap: 2rem;
white-space: nowrap;
}
.nav__toggler {
display: none;
}
.nav__toggler div {
width: 2.5rem;
height: 0.2rem;
margin: 0.4rem;
background: var(--text-color);
transition: 0.3s ease-in;
}
/* Media Queries */
/* Header Component */
#media screen and (max-width: 900px) {
.nav__toggler {
display: block;
cursor: pointer;
z-index: 10;
background: none;
}
.nav__menu {
position: fixed;
top: 0;
right: 0;
height: 100vh;
width: 100vw;
background: var(--sapphire);
flex-direction: column;
transform: translateX(100%);
transition: 0.5s ease-in;
z-index: 9;
padding: 0;
}
.nav__menu li:first-child{
margin-top: 125px;
}
.nav__menu li{
padding: 25px 0;
}
/* Active Class */
.nav__active {
transform: translateX(0%);
}
/* Toggle Icon Animation */
.toggle .line1 {
transform: rotate(-45deg) translate(-4px, 5px);
}
.toggle .line2 {
opacity: 0;
}
.toggle .line3 {
transform: rotate(45deg) translate(-8px, -10px);
}
}
#media(max-width: 500px){
.nav{
padding: 25px 0;
}
.nav-logo-img{
width: 30%;
}
}
I'm having an issue getting my nav bar to pass over the content of my hero and title text. The background image will pass beneath my nav bar as desired, along with any further text that I add. But the title text and button pass over the bar. I think it has something to do either with their positions (absolute), or their z-indeces. I've tried manipulating these, but to undesirable effect. Changing the h1's position to fixed or relative completely distorts its placement. The nav bar should remain fixed at the top and everything should pass beneath it. Can anyone help me with this?
Here is my code:
navbar.js:
import { useState } from "react";
import styles from "../component-css/navbar.module.css";
import { GiHamburgerMenu } from "react-icons/gi";
const NavBar = (props) => {
return (
<div className={styles.canvas}>
<div className={styles.container}>
<div className={styles.buttonContainer}>
<a className={styles.navButton} href="#">
Home
</a>
</div>
<div className={styles.buttonContainer}>
<a className={styles.navButton} href="#">
About
</a>
</div>
<div className={styles.buttonContainer}>
<a className={styles.navButton} href="#">
Contact
</a>
</div>
<div>
<div className={styles.void} />
</div>
<div className={`${styles.buttonContainer} ${styles.menu}`}>
<span className={styles.navButton}>
<GiHamburgerMenu />
</span>
</div>
</div>
{
props.logo
? <img className={styles.logoSticky} src="/MegaManLogo.png" alt="Mega Man Logo" />
: null
}
</div>
);
};
export default NavBar;
navbar.module.css:
.canvas {
position: absolute;
}
.container {
display: grid;
width: 100%;
position: fixed;
top: 0;
grid-template-columns: repeat(3, .2fr) 1.3fr .25fr;
grid-template-rows: .08fr;
grid-column-gap: 0px;
grid-row-gap: 0px;
height: 25px;
background: rgb(14, 14, 175);
/* box-shadow: 0px 2px 0px rgba(94, 94, 94, 0.5); */
overflow: hidden;
}
.buttonContainer {
background: transparent;
height: inherit;
width: 100%;
text-align: center;
overflow: hidden;
transition-duration: 0.5s;
}
.buttonContainer:hover {
background:rgb(109, 109, 216);
}
.navButton {
background: transparent;
color: white;
text-decoration: none;
transition-duration: .5s;
line-height: 1.5rem;
font-family: sans-serif;
font-size: .7rem;
margin-top: 50%;
}
.menu {
font-size: 1rem;
line-height: 1.7;
transform: translateX(20px);
}
.menu:hover {
background: transparent;
}
.logoSticky {
margin-left: 44vw;
margin-top: -100vh;
position: sticky;
width: 10vw;
height: auto;
z-index: 30;
transform: translateY(23px);
}
hero.js:
import { useState, useEffect } from 'react';
import styles from "../component-css/hero.module.css"
import { GiHamburgerMenu } from 'react-icons/gi'
const Hero = () => {
const [scrollPosition, setScrollPosition] = useState(0);
const handleScroll = () => {
const position = window.pageYOffset;
setScrollPosition(position);
};
useEffect(() => {
window.addEventListener("scroll", handleScroll);
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, []);
return (
<div className={styles.parent}>
<div className={styles.heroBox} />
<div className={styles.blackOverlay} />
<img className={scrollPosition < 63 ? styles.logo : styles.logoSticky}
src="/MegaManLogo.png"
alt="Mega Man Logo"
/>
<div className={styles.headerBox}>
<h1 className={styles.h1}>
A Brief History of the <span className={styles.blueBomber}> Blue Bomber</span>
</h1>
</div>
<button
className={styles.gamesButton}
>
See the Games
</button>
</div>
)
}
export default Hero
hero.module.css:
.parent {
position: relative;
}
.blackOverlay {
box-sizing: border-box;
height: 100vh;
width: 100vw;
margin: 0;
margin-top: -100vh;
padding: 0;
background: rgba(0, 0, 0, 0.705);
max-width: 100%;
overflow: hidden;
z-index: 10;
}
.heroBox {
box-sizing: border-box;
overflow-y: hidden;
height: 100vh;
width: 100vw;
margin: 0;
padding: 0;
background-image: url("../images/hero-background.jpg");
background-size: 100vw auto;
background-repeat: no-repeat;
max-width: 100%;
overflow: hidden;
z-index: 1;
}
.logo {
position: absolute;
width: 40vw;
height: auto;
margin-left: 30vw;
margin-top: -85vh;
z-index: 30;
transition-duration: 0.5s;
}
.logoSticky {
margin-left: 44vw;
margin-top: -100vh;
position: fixed;
width: 10vw;
height: auto;
z-index: 30;
transition-duration: 0.5s;
}
.headerBox {
position: absolute;
margin: 0px;
margin-top: 8vh;
margin-left: 30vw;
margin-bottom: 0px;
padding-bottom: 5px;
top: 45vh;
border-bottom: 2px solid rgb(122, 112, 255);
z-index: 20;
}
.h1 {
color: white;
font-family: Arial Narrow, sans-serif;
font-weight: 100;
font-size: 1.7em;
}
.blueBomber {
color: rgb(122, 112, 255);
font-family: Arial Narrow, sans-serif;
font-weight: 400;
float: right;
margin-left: 6px;
}
.gamesButton {
position: absolute;
top: 67vh;
left: 43.5vw;
padding: 7px;
font-family: Arial Narrow, sans-serif;
font-weight: 100;
font-size: 16px;
color: rgb(122, 112, 255);
background: transparent;
border: 2px solid rgb(122, 112, 255);
border-radius: 200px;
cursor: pointer;
transition-duration: 0.3s;
z-index: 20;
}
.gamesButton:hover {
background: rgb(122, 112, 255);
border: 3px solid rgb(122, 112, 255);
color: white;
}
.listItem {
display: inline-block;
margin-top: 5px;
font-size: 14px;
border-bottom: 2px solid transparent;
transition-duration: 0.5s;
text-align: center;
}
.listItem:hover {
color:rgb(122, 112, 255);
border-bottom: 2px solid rgb(122, 112, 255);
}
App.js:
import './App.css';
import Hero from './components/hero.js'
import NavBar from './components/navbar.js'
import styles from './component-css/App.module.css'
function App() {
return (
<div className="App">
<div className={styles.nav}><NavBar logo={false} /></div>
<div className={styles.body}><Hero /></div>
<div style={{height: "100vh"}} />
</div>
);
}
export default App;
App.module.css:
.nav {
position: absolute;
width: 100%;
z-index: 10;
}
.body {
z-index: 1;
}
Something on my website is bothering me.
The last menu item of my Navbar is outside it's container's background:
Here is how my CSS looks like:
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
height: 280px;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
align-items: space-evenly;
}
.nav-menu.active {
display: flex;
flex-direction: column;
align-content: flex-start;
background: #242222;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav-links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav-links:hover {
background-color: #fff;
color: #242424;
border-radius: 0;
}
.navbar-logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%);
}
.menu-icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav-links-mobile {
display: block;
text-align: center;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
text-decoration: none;
font-size: 1.5rem;
background-color: transparent;
color: #fff;
padding: 14px 20px;
border: 1px solid #fff;
transition: all 0.3s ease-out;
}
.nav-links-mobile:hover {
background: #fff;
color: #242424;
transition: 250ms;
}
}
and HTML generated by a React component looks like this:
<>
<nav className='navbar'>
<div className='navbar-container'>
<Link to='/' className='navbar-logo' onClick={closeMobileMenu}>
LOGO
</Link>
<div className='menu-icon' onClick={handleClick}>
<i className={click ? 'fas fa-times' : 'fas fa-bars'} />
</div>
<ul className={click ? 'nav-menu active' : 'nav-menu'}>
<li className='nav-item'>
<Link to='/' className='nav-links' onClick={closeMobileMenu}>
Home
</Link>
</li>
<li className='nav-item'>
<HashLink to='/#wherewefly' onClick={closeMobileMenu} className='nav-links'>
Where we fly
</HashLink>
</li>
<li className='nav-item'>
<Link
to='/services'
className='nav-links'
onClick={closeMobileMenu}
>
Promotions
</Link>
</li>
<li className='nav-item'>
<Link
to='/products'
className='nav-links'
onClick={closeMobileMenu}
>
Contact
</Link>
</li>
</ul>
{button && <Button buttonStyle='btn--outline'>LOGIN</Button>}
</div>
</nav>
</>
How can I make the last menu item (Contact) be covered by the black background too?
I have tried changing the height but the menu items "follow" and are aligned to the bottom. I can't find out how to change it though.
Edited: Picture has been updated
Remove height: 280px;.
You are using flex-box so you shouldn't be setting the height or width attributes. The idea is to allow the box to be flexible to it's contents.
If you want a fixed height you should be using flex-basis attribute.
EXAMPLE
I have had to convert your react to html to demonstrate but as you can see the code you provided works as expected. This would indicate the issue lies elsewhere in code you have not provided.
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
align-items: space-evenly;
}
.nav-menu.active {
display: flex;
flex-direction: column;
align-content: flex-start;
background: #242222;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav-links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav-links:hover {
background-color: #fff;
color: #242424;
border-radius: 0;
}
.navbar-logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%);
}
.menu-icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav-links-mobile {
display: block;
text-align: center;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
text-decoration: none;
font-size: 1.5rem;
background-color: transparent;
color: #fff;
padding: 14px 20px;
border: 1px solid #fff;
transition: all 0.3s ease-out;
}
.nav-links-mobile:hover {
background: #fff;
color: #242424;
transition: 250ms;
}
<nav class='navbar'>
<div clas='navbar-container'>
<a class='navbar-logo'> LOGO
</a>
<div class='menu-icon'>
<i class='fas fa-times' />
</div>
<ul class='nav-menu active'>
<li class='nav-item'>
<a class='nav-links'> Home
</a>
</li>
<li class='nav-item'>
<a class='nav-links'>
Where we fly
</a>
</li>
<li class='nav-item'>
<a class='nav-links'> Promotions
</a>
</li>
<li class='nav-item'>
<a class='nav-links'> Contact
</a>
</li>
</ul>
<Button class='btn--outline'>LOGIN</Button>
</div>
</nav>
Well, the height is fixed in the CSS, if you want it to take the height the content has you need to use "fit-content".
Blockquote
The issue could be the height of the nav-menu. please use min-hight instead. This may fix your issue
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
min-height: 280px;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
align-items: space-evenly;
}
I have created a navbar. This navbar should change color when scrolling. I have only managed so far that the background color changes. However, I also want the text colors to change as well as the icons. I would make now with each className the query, whether active or not. But this is very redundant. Is there an option that covers everything? By saying if the user scrolls and the navbar changes color, then the textcolor and everything else should also change? Would it make sense to say if navbar changes then call another new css? Is that possible? If so how do I do that?
Is there an option that makes this easier?
Navbar.js
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import { Button } from '../buttons/Button';
import './Navbar.css';
function Navbar() {
const [click, setClick] = useState(false);
const [button, setButton] = useState(true);
const [navbar, setNavbar ] = useState(false);
const handleClick = () => setClick(!click);
const closeMobileMenu = () => setClick(false);
const showButton = () => {
if(window.innerWidth <= 960) {
setButton(false);
}
else {
setButton(true);
}
};
useEffect(() => {
showButton();
}, []);
window.addEventListener('resize', showButton);
const changeBackground = () => {
if(window.scrollY >= 80) {
setNavbar(true);
}
else {
setNavbar(false);
}
};
window.addEventListener('scroll', changeBackground);
return (
<>
<nav className={navbar ? 'navbar active' : 'navbar'}>
<div className="navbar-container">
<Link to="/" className="navbar-logo" onClick={closeMobileMenu}>
APPNAME<i className="fab fa-typo3"></i>
</Link>
<div className="menu-icon" onClick={handleClick}>
<i className={click ? 'fas fa-times': 'fas fa-bars'} />
</div>
<ul className={click ? 'nav-menu active' : 'nav-menu'}>
<li className='nav-item'>
<Link to="/" className='nav-links' onClick={closeMobileMenu}>
Home
</Link>
</li>
<li className='nav-item'>
<Link to="/services" className='nav-links' onClick={closeMobileMenu}>
Services
</Link>
</li>
<li className='nav-item'>
<Link to="/products" className='nav-links' onClick={closeMobileMenu}>
Products
</Link>
</li>
<li className='nav-item'>
<Link to="/sign-up" className='nav-links-mobile' onClick={closeMobileMenu}>
Sign up
</Link>
</li>
</ul>
{button && <Button buttonStyle='btn--outline'>GET STARTED</Button>}
</div>
</nav>
</>
)
}
export default Navbar
Navbar.css
.navbar {
background: #2b41cb;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
position: sticky;
top: 0;
z-index: 999;
}
/* AS YOU CAN SEE HERE IS THE ACTIVE PART */
.navbar.active {
/*background: linear-gradient(90deg, rgb(66, 2, 194) 0%, rgb(0, 78, 194) 100%)*/
background: #fff;
}
.navbar-container {
display: flex;
justify-content: center;
align-items: center;
height: 80px;
max-width: 1500px;
}
.navbar-logo {
color: #fff;
justify-self: start;
margin-left: 20px;
cursor: pointer;
text-decoration: none;
font-size: 2rem;
display: flex;
align-items: center;
}
.navbar-logo.active {
color: #232323;
}
.fa-typo3 {
margin-left: 0.5rem;
font-size: 1.8rem;
}
.nav-menu {
display: grid;
grid-template-columns: repeat(4, auto);
grid-gap: 10px;
list-style: none;
text-align: center;
width: 60vw;
justify-content: end;
margin-right: 2rem;
}
.nav-item {
height: 80px;
}
.nav-links {
color: #fff;
display: flex;
align-items: center;
text-decoration: none;
padding: 0.5rem 1rem;
height: 100%;
}
.nav-links:hover {
border-bottom: 4px solid #fff;
transition: all 0.2s ease-out;
}
.fa-bars {
color: #fff;
}
.nav-links-mobile {
display: none;
}
.menu-icon {
display: none;
}
#media screen and (max-width: 960px) {
.NavbarItems {
position: relative;
}
.nav-menu {
display: flex;
flex-direction: column;
width: 100%;
height: 90vh;
position: absolute;
top: 80px;
left: -100%;
opacity: 1;
transition: all 0.5s ease;
}
.nav-menu.active {
background: #242222;
left: 0;
opacity: 1;
transition: all 0.5s ease;
z-index: 1;
}
.nav-links {
text-align: center;
padding: 2rem;
width: 100%;
display: table;
}
.nav-links:hover {
background-color: #fff;
color: #242424;
border-radius: 0;
}
.navbar-logo {
position: absolute;
top: 0;
left: 0;
transform: translate(25%, 50%);
}
.menu-icon {
display: block;
position: absolute;
top: 0;
right: 0;
transform: translate(-100%, 60%);
font-size: 1.8rem;
cursor: pointer;
}
.fa-times {
color: #fff;
font-size: 2rem;
}
.nav-links-mobile {
display: block;
text-align: center;
margin: 2rem auto;
border-radius: 4px;
width: 80%;
text-decoration: none;
font-size: 1.5rem;
background-color: transparent;
color: #fff;
padding: 14px 20px;
border: 1px solid #fff;
transition: all 0.3s ease-out;
}
.nav-links-mobile:hover {
background: #fff;
color: #242424;
transition: 250ms;
}
}
What I would like is for the entire navbar to be in white and the text and icons to be in black when scrolled. If not it should stay as it is, blue background and white font color.
But this is very redundant. Is there an option that covers everything?
Yes, just add a new class name for the element state and then use that class to style everything including the child elements. So in your case you can do:
.navbar {
background: #2b41cb;
height: 80px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2rem;
position: sticky;
top: 0;
z-index: 999;
}
.navbar-container {
display: flex;
justify-content: center;
align-items: center;
height: 80px;
max-width: 1500px;
}
.navbar-logo {
color: #fff;
justify-self: start;
margin-left: 20px;
cursor: pointer;
text-decoration: none;
font-size: 2rem;
display: flex;
align-items: center;
}
/* Active style overrides */
.navbar.active {
background: #fff;
}
.navbar.active .navbar-logo {
color: #232323;
}
Would it make sense to say if navbar changes then call another new css? Is that possible? If so how do I do that?
I think that is also possible, you can use JS to do that but I think adding a state class name is simpler and easier to reason about.