How to change the style of the navbar on scrolling? - css

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.

Related

NextJs 13 Link styling

I'm new in nextJs and for a projet, I try to create a menu with a hover effect with this code:
import Link from 'next/link';
const MenuItem = ({ href, label }) => (
//This Link has a className that doesn't get styled.
<Link href={href} className="menu-item">
{label}
</Link>
);
function MainNavigation() {
return (
<>
<style jsx>{`
.header {
height: 5rem;
display: flex;
align-items: center;
justify-content: space-between;
background-color: #ffffff;
padding: 0 10%;
font-family: system-ui, sans;
font-size: 2.5rem;
font-weight: 800;
}
.logo {
font-size: 2rem;
color: white;
font-weight: bold;
}
.header ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
align-items: baseline;
}
.header li {
margin-left: 3rem;
}
.menu-item {
color: #000;
position: relative;
text-decoration: none;
}
.menu-item::before {
background: hsl(45 100% 70%);
content: "";
inset: 0;
position: absolute;
transform: scaleX(0);
transform-origin: right;
transition: transform 0.5s ease-in-out;
z-index: -1;
}
.menu-item:hover::before {
transform: scaleX(1);
transform-origin: left;
}
`}</style>
<header className='header'>
<div className='logo'>React Meetups</div>
<nav>
<ul>
<li>
<MenuItem href='/' label='menu item 1' />
</li>
<li>
<MenuItem href='/' label='menu item 2' />
</li>
</ul>
</nav>
</header>
</>
);
}
export default MainNavigation;
According nextJs 13 documentation, we don't need anymore to put in the Link the tag but I wonder how I can do the hover effect on the menu item? Currently the style is not apply on my menu-item.
Someone could explain what's wrong in this piece of code?
Thanks
try this in your style
.header > nav > ul > li > a:hover{
/*put your CSS here*/
}

How can I fix the logo/image position to be positioned on the top left instead of in the middle on media (min-width: 40rem) specific?

<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;
}
}

CSS selector when placeholder shown in grid element

In a (React/TS) form i need to move the label for the inputfield when;
The input has the focus
When the placeholder is not shown
This works fine for the first objective (with .grid__item--fixedrowcolone), but i am unable to to realise this for the second. The input and the label are grid elements so i can place the input and label on top and vertically center both.
What i unsuccessfully tried is (multiple variants) of the snippet below.
.grid__item--fixedrowcolone {
&:not(~ .form__inputfield:placeholder-shown) {
color: green;
}
}
react form
<div className={styles.form__inputwrapper} data-testid='form-input'>
<div className={styles.form__inputsymbol} data-testid='form-input-icon'>
<img src={lockSvg} alt='lock' width='24' height='24' />
</div>
<div className={styles.grid__container} data-testid='form-input-field'>
<div className={styles['grid__item--fixedrowcolone']}>
<input
className={styles.form__inputfield}
type='text'
pattern='^.*{8,}'
id='pwd1'
name='pwd1'
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder=' '
required
data-testid='forminput-input'
/>
</div>
<div className={styles['grid__item--fixedrowcoltwo']}>
<label
className={styles.form__inputlabel}
htmlFor='pwd1'
data-testid='forminput-inputlabel'
>
wachtwoord
<span className={styles.form__inputlabelvalmsg}>
+312234
</span>
</label>
</div>
</div>
</div>
css module.scss
.grid {
&__container {
display: grid;
justify-items: stretch;
}
&__item {
&--fixedrowcolone, &--fixedrowcoltwo {
display: flex;
grid-column: 1;
grid-row: 1;
}
}
}
.form {
&__inputwrapper {
display: flex;
margin-bottom: 10px;
position: relative;
background: var(--theme_form_field_bg_color);
border-radius: 4px;
}
&__inputsymbol {
display: flex;
align-self: center;
//align-items: center;
padding-left: 10px;
pointer-events: none;
color: var(--theme_fg_color);
font-size: 1.5em;
transition: all 0.4s;
filter: var(--theme_fg_color_filter)
}
&__inputfield {
align-self: center;
//line-height: 1.2;
font-size: 1.5em;
height: 50px;
color: var(--theme_fg_color);
background: none;
outline: none;
border: none;
max-width: 280px;
min-width: 250px;
overflow: visible;
touch-action: manipulation;
}
&__inputlabel {
align-self: center;
//line-height: 1.2;
font-size: 1.5em;
transition: .3s all ease;
}
&__inputlabelvalmsg {
display: none;
}
}
.grid__item--fixedrowcolone {
&:focus-within ~ .grid__item--fixedrowcoltwo {
top: 5px;
font-size: .5em;
align-self: start;
}
}
Unfortunately it is yet not possible to select elements based on presence of child elements or child element pseudo selectors. So therefore i reverted back to absolute positioning to overlay two elements.
react form
<div className={styles.form__inputwrapper} data-testid='form-input'>
<div className={styles.form__inputsymbol} data-testid='form-input-icon'>
<img src={lockSvg} alt='lock' width='24' height='24' />
</div>
<div
className={styles.form__inputfieldwrapper}
data-testid='form-input-field'
>
<input
className={styles.form__inputfield}
type='text'
pattern='^.*{8,}'
id='pwd1'
name='pwd1'
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
placeholder=' '
required
data-testid='forminput-input'
/>
<label
className={styles.form__inputlabel}
htmlFor='pwd1'
data-testid='forminput-inputlabel'
>
wachtwoord
<span className={styles.form__inputlabelvalmsg}>
+31652693 of 0652786518
</span>
</label>
</div>
</div>
css module.scss
#mixin inputlabel-defaults() {
font-size: 1.5em;
align-self: center;
}
.form {
&__inputwrapper {
display: flex;
margin-bottom: 10px;
position: relative;
background: var(--theme_form_field_bg_color);
border-radius: 4px;
}
&__inputfieldwrapper {
display: flex;
}
&__inputsymbol {
display: flex;
align-self: center;
//align-items: center;
padding-left: 10px;
pointer-events: none;
color: var(--theme_fg_color);
font-size: 1.5em;
transition: all 0.4s;
filter: var(--theme_fg_color_filter)
}
&__inputlabel {
position: absolute;
transition: .3s all ease;
#include inputlabel-defaults();
}
&__inputlabelvalmsg {
display: none;
}
&__inputfield {
align-self: center;
//line-height: 1.2;
font-size: 1.5em;
height: 50px;
color: var(--theme_fg_color);
background: none;
outline: none;
border: none;
max-width: 280px;
min-width: 250px;
overflow: visible;
touch-action: manipulation;
&:focus-within ~ .form__inputlabel {
top: 5px;
font-size: .5em;
align-self: start;
}
&:not(:placeholder-shown) ~ .form__inputlabel {
color: red;
top: 5px;
font-size: .5em;
align-self: start;
}
&:placeholder-shown ~ .form__inputlabel {
top: unset;
#include inputlabel-defaults();
}
}
}

NAV Hamburger links show at the bottom of the screen

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;
}
}

Animation on scaling down to mobile

I have made a responsive navbar and have added an animation to be played when ".nav-toggle" is clicked for the mobile size of the navbar.
When I am scaling down my browser window, for a split second at screen size 768px you can see the animation playing in reverse. The animation I am asking about is the "transform: scale(1, 0);".
How do I get the animation to not play when the user is scaling down the window size from desktop to mobile? Again, the animation in question occurs when you get to 768px, it animates in reverse for like a second then it works normally.
Thank you for your help!!
const mainNav = document.querySelector('.main-nav');
const navToggle = document.querySelector('.nav-toggle');
const navLinks = document.querySelectorAll('.main-nav li');
navToggle.addEventListener('click', function () {
mainNav.classList.toggle('main-nav-active');
navLinks.forEach((links, index) => {
if (links.style.animation) {
links.style.animation = '';
} else {
links.style.animation = `navLinksFade .3s ease forwards ${index / 10 + .2}s`
};
});
});
:root {
--clr-primary-200: #f3eed9;
--clr-primary-400: #824936;
--clr-neutral-100: #FFF;
--clr-neutral-900: #222c2a;
--ff-primary: 'Roboto', sans-serif;
--ff-accent: 'Playfair Display SC', sans-serif;
}
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: var(--ff-primary);
font-size: 1.3175;
}
.navbar {
background-color: var(--clr-neutral-900);
display: flex;
justify-content: space-around;
align-items: center;
min-height: 10vh;
}
.logo {
color: var(--clr-neutral-100);
text-decoration: none;
font-size: 2rem;
text-transform: uppercase;
letter-spacing: 3px;
}
.main-nav {
display: flex;
width: 60%;
justify-content: space-around;
}
.main-nav li {
list-style: none;
}
.nav-links {
text-decoration: none;
color: var(--clr-neutral-100);
font-size: 1.4rem;
}
.nav-toggle {
color: var(--clr-neutral-100);
font-size: 2rem;
display: none;
cursor: pointer;
}
#media (max-width: 768px) {
.main-nav {
position: absolute;
background-color: var(--clr-neutral-900);
top: 10vh;
width: 100%;
height: 40%;
display: flex;
flex-wrap: nowrap;
flex-direction: column;
align-items: center;
transform: scale(1, 0);
transform-origin: top;
transition: all 200ms ease-in;
}
.main-nav li {
opacity: 0;
}
.logo {
margin-right: auto;
margin-left: 2rem;
}
.nav-toggle {
display: block;
margin-right: 2rem;
}
.main-nav-active {
transform: scale(1, 1);
}
}
#keyframes navLinksFade {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
<nav class="navbar">
Logo
<ul class="main-nav">
<li>
Home
</li>
<li>
Products
</li>
<li>
Contact Us
</li>
<li>
Blog
</li>
</ul>
<div class="nav-toggle">
<div>+</div>
</div>
</nav>

Resources