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%;
}
}
Related
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;
}
}
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 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>
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.
After adding the pop-up menu, the content inside "main" disappear and the overflow-x:hidden of the body does not work. Does anyone know why?
const slide = () => {
const burger = document.querySelector(".burger");
const menu = document.querySelector(".menu");
const links = document.querySelectorAll(".menu div");
//Toggle Menu
burger.addEventListener("click", () => {
menu.classList.toggle("menupop");
})
}
slide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: calc(16px + 0.25vw);
overflow: scroll;
font-family: 'Antic Slab', serif;
}
body {
min-height: 100vh;
overflow-x: hidden;
}
main {
display: grid;
width: 100vw;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50% 50% 50%;
}
header {
grid-column: 1/3;
grid-row: 1/2;
background-color: pink;
}
#rose {
grid-column: 1/2;
grid-row: 2/3;
background-color: hotpink;
}
#rose-img {
grid-column: 2/3;
grid-row: 2/3;
background-color: rgb(134, 184, 204);
}
#tree {
grid-column: 1/3;
grid-row: 3/4;
background-color: lawngreen;
}
#about-us {
grid-column: 1/2;
grid-row: 4/5;
background-color: lightcoral;
}
#contact-us {
grid-column: 2/3;
grid-row: 4/5;
background-color: orange;
}
.burger {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: space-around;
width: 50px;
height: 50px;
cursor: pointer;
display: none;
}
.line1,
.line2,
.line3 {
flex: 1 1 1;
width: 80%;
height: 5%;
background: black;
border-radius: 5px;
box-shadow: 1px 1px grey;
transition: all .5s ease-in-out;
}
.a {
transform: translateY(2px);
}
.b {
transform: translateY(-2px);
}
nav {
display: flex;
justify-content: flex-end;
align-items: center;
height: 50px;
background-color: rgb(245, 249, 250, 0.5);
padding: 5px 10px;
position: sticky;
top: 0;
}
ul {
display: flex;
list-style: none;
justify-content: space-around;
width: 40%
}
li a {
text-decoration: none;
color: grey;
}
.logo {
font-family: 'Italianno', cursive;
margin-right: auto;
font-size: 30px;
font-weight: bold;
}
.menu {
display: flex;
flex-direction: column;
justify-content: flex-start;
background-color: rgba(91, 126, 172, 0.5);
height: 100vh;
width: 50vw;
position: absolute;
top: 50px;
right: 0;
transform: translateX(100%);
transition: transform 0.5s ease-in;
}
.menupop {
transform: translateX(0%);
}
.menupop div {
animation-name: menuFade;
animation-duration: 1s;
animation-delay: 2s;
animation-timing-function: ease;
animation-fill-mode: forwards;
}
.menu div {
width: 100%;
height: 20%;
background-color: rgb(245, 249, 250, 0.6);
margin-bottom: 1px;
display: flex;
justify-content: center;
align-items: center;
transform: translateX(100%);
}
.menu div a {
text-decoration: none;
}
#keyframes menuFade {
from {
transform: translateX(100%);
}
to {
transform: translateX(0px);
}
}
#media only screen and (max-width: 600px) {
ul {
width: 60%;
}
}
#media only screen and (max-width: 430px) {
.burger {
display: flex;
}
ul {
display: none;
}
}
<body>
<nav>
<div class="logo">Logo</div>
<ul>
<li>Flower</li>
<li>Tree</li>
<li>About us</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div class="menu">
<div class="first">Flower</div>
<div class="second">Tree</div>
<div class="third">About us</div>
</div>
<main>
<header id="jumbotron">
</header>
<section id="rose">
</section>
<section id="rose-img">
</section>
<section id="tree">
<div class="privacy">
</div>
<div class="hedge">
</div>
</section>
<section id="about-us">
</section>
<section id="contact-us">
</section>
</main>
</body>
In your CSS, remove overflow: scroll;.
This should prevent your side menu from staying outside of your viewport width, because with overflow: scroll;, you're allowing the browser to have your side menu out of view, making it only reachable via scrolling.
I'd also like to add that your side menu transition is too long.
If you think about it from a user experience, a user might think that your site is slow.
I've shortened the transition to 200ms and removed the animation-duration and animation-delay. It'll seem more responsive to users now.
EDIT: Also, is anyone else finding it weird how when you use "Run Snippet" on Stackoverflow, use Full Page view, then try to shrink it down to mobile width, the media queries aren't working properly? It works fine when I run the code in VSCode and launch a Live Server, but for some reason, Stackoverflow's Run Snippet is being buggy...
EDIT 2: I've added content to each of your div tags and section tags. So the reason why you couldn't see any content under the main tag was because the height of all of your containers (div, section, main, and header tags) is, by default, auto. If height is auto, then the container will shrink or grow based on the content inside of the container. In your case, you had NO content in ANY of your containers, hence why all of your containers weren't visible, they were essentially at 0px height.
Now that I've added content, you can see your containers.
const slide = () => {
const burger = document.querySelector(".burger");
const menu = document.querySelector(".menu");
const links = document.querySelectorAll(".menu div");
//Toggle Menu
burger.addEventListener("click", () => {
menu.classList.toggle("menupop");
})
}
slide();
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: calc(16px + 0.25vw);
font-family: 'Antic Slab', serif;
}
body {
min-height: 100vh;
overflow-x: hidden;
}
main {
display: grid;
width: 100vw;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50% 50% 50%;
}
header {
grid-column: 1/3;
grid-row: 1/2;
background-color: pink;
}
#rose {
grid-column: 1/2;
grid-row: 2/3;
background-color: hotpink;
}
#rose-img {
grid-column: 2/3;
grid-row: 2/3;
background-color: rgb(134, 184, 204);
}
#tree {
grid-column: 1/3;
grid-row: 3/4;
background-color: lawngreen;
}
#about-us {
grid-column: 1/2;
grid-row: 4/5;
background-color: lightcoral;
}
#contact-us {
grid-column: 2/3;
grid-row: 4/5;
background-color: orange;
}
.burger {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-content: space-around;
width: 50px;
height: 50px;
cursor: pointer;
display: none;
}
.line1,
.line2,
.line3 {
flex: 1 1 1;
width: 80%;
height: 5%;
background: black;
border-radius: 5px;
box-shadow: 1px 1px grey;
transition: all .5s ease-in-out;
}
.a {
transform: translateY(2px);
}
.b {
transform: translateY(-2px);
}
nav {
display: flex;
justify-content: flex-end;
align-items: center;
height: 50px;
background-color: rgb(245, 249, 250, 0.5);
padding: 5px 10px;
position: sticky;
top: 0;
}
ul {
display: flex;
list-style: none;
justify-content: space-around;
width: 40%
}
li a {
text-decoration: none;
color: grey;
}
.logo {
font-family: 'Italianno', cursive;
margin-right: auto;
font-size: 30px;
font-weight: bold;
}
.menu {
display: flex;
flex-direction: column;
justify-content: flex-start;
background-color: rgba(91, 126, 172, 0.5);
height: 100vh;
width: 50vw;
position: absolute;
top: 50px;
right: 0;
transform: translateX(100%);
transition: transform 200ms ease-in;
}
.menupop {
transform: translateX(0%);
}
.menupop div {
animation-name: menuFade;
animation-timing-function: ease;
animation-fill-mode: forwards;
}
.menu div {
width: 100%;
height: 20%;
background-color: rgb(245, 249, 250, 0.6);
margin-bottom: 1px;
display: flex;
justify-content: center;
align-items: center;
transform: translateX(100%);
}
.menu div a {
text-decoration: none;
}
#keyframes menuFade {
from {
transform: translateX(100%);
}
to {
transform: translateX(0px);
}
}
#media only screen and (max-width: 600px) {
ul {
width: 60%;
}
}
#media only screen and (max-width: 430px) {
.burger {
display: flex;
}
ul {
display: none;
}
}
<body>
<nav>
<div class="logo">Logo</div>
<ul>
<li>Flower</li>
<li>Tree</li>
<li>About us</li>
</ul>
<div class="burger">
<div class="line1"></div>
<div class="line2"></div>
<div class="line3"></div>
</div>
</nav>
<div class="menu">
<div class="first">Flower</div>
<div class="second">Tree</div>
<div class="third">About us</div>
</div>
<main>
<header id="jumbotron">
This is a header with ID "jumbotron".
</header>
<section id="rose">
This is a section with ID "rose".
</section>
<section id="rose-img">
this is a section with ID "rose-img".
</section>
<section id="tree">
<div class="privacy">
This is a div with class "privacy".
</div>
<div class="hedge">
This is a div with class "hedge".
</div>
</section>
<section id="about-us">
This is a section with ID "about-us".
</section>
<section id="contact-us">
This is a section with ID "contact-us".
</section>
</main>
</body>