The hamburger menu is malfunctioning and spilling over

// App.js

import './App.css';

function App() {
  return (
    <div className='navbar'>
    <label className="hamburger-menu">
        <input type="checkbox" />
    </label>
    <div className="logo">Purohit Store</div>
    <div className="nav-lists">
        <ul>
            <li>home</li>
            <li>products</li>
            <li>contact</li>
            <li>about</li>
        </ul>
        <div>
          <button>login</button>
          <button>cart</button>
        </div>
    </div>
</div>
  );
}

export default App;


// App.css

*, *::before, *::after{
  box-sizing: border-box;
}

body{
  margin: 0;
  background: #eee;
  font-family: sans-serif;
  width: 100%;
  overflow-x: hidden;
}

.navbar,
.nav-lists,
.navbar ul,
.nav-lists div{
  display: flex;
  align-items: center;
}

.navbar{
  position: sticky;
  top: 0;
  left: 0;
  justify-content: space-between;
  padding: 10px 20px;
  background-color: #fff;
  width: 100%;
  z-index: 100;
}

.nav-lists{
  gap: 50px;
}
.navbar ul,
.nav-lists div{
  gap: 20px;
  list-style: none;
}


@media screen and (min-width: 701px) {
.hamburger-menu{
  display: none;
}
}

@media screen and (max-width: 700px) {

.nav-lists{
  position: absolute;
  top: 0;
  right: 0;
  flex-direction: column;
  background-color: #333;
  color: white;
  margin-top: 3rem;
  padding: 2rem;
  transition: translate 200ms ease;
  translate: 100%;
  max-width: 30rem;
  min-height: 100vh;
}

.nav-lists ul,
.nav-lists div{
  flex-direction: column;
  padding: 0;
}

.hamburger-menu {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 110;
}

.hamburger-menu:has(input:checked) + .nav-lists {
  translate: 0;
}

}

[enter image description here][1]

when i check the checkbox nav-lists should be translate to 0 and when i uncheck the checkbox it should translate to 100% but it does not working and nav-lists is overflowing so in body i set overflow is hidden but still it is overflowing. when i do the same thing with navbar it's working but the problem is that app logo and hamburger both included in navbar so i do this with nav-lists but in nav-lists it doesn't work. when i exclude logo and hamburger from navbar and paste it to top of the jsx and include all jsx in one fragment than its working but when i include all this jsx in div instead of jsx fragment it does not work

Answer №1

The problem lies with this specific selector:

.hamburger-menu:has(input:checked) + .nav-lists

With the + symbol, it is trying to find the element directly after the hamburger menu (which in this scenario, is not nav-lists, but rather logo)

To target any subsequent element, you can utilize the general sibling combinator ~:

.hamburger-menu:has(input:checked) ~ .nav-lists

Here is a small demonstration:

.hamburger-menu:has(input:checked) ~ .nav-lists {
  color: #f00;
}
<label class="hamburger-menu">
  <input type="checkbox" />
</label>
<div class="anotherElement">Another element</div>
<div class="nav-lists">Nav list</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Disabling the ripple effect in the Tab Component of React's Material UI library: A step-by-step guide

Seeking to eliminate the default ripple effect present in the Tab component of the Material UI library, I referenced a solution on How to disable ripple in Material Design React and attempted the following: const CustomTab = withStyles({ ... MuiBu ...

Shifting divs to different positions upon clicking

I am currently working on a project where I have three divs in a container positioned next to each other. My goal is to make them change their positions when clicked. For example, clicking on the left div should move it to the center position. Here is my p ...

What is the reason behind the modification in flex item size when align-items property is applied?

When creating the responsive design, I utilized flexbox. Surprisingly, without changing the size of each flex-item, applying align-items:center caused the size of the first flex item (PICTURE 2) to change when displayed in a vertical view. On the other han ...

Getting data from a list in an SPFX solution: A step-by-step guide

I have 3 files - RequestSHP.tsx, WebPart.ts, Exekucie.tsx. I'm attempting to populate a dropdown with data from a list, but encountering a "404 (Not Found)" error. Here is my RequestSHP.tsx -> import {sp} from '@pnp/sp/presets/all'; exp ...

Troubleshooting Next.js server actions with ESLint error detection

I encountered eslint errors while developing a basic server component with server action: // /app/search/page.tsx export default function Search() { async function updateResults(formData: FormData) { "use server"; await new Promise((r ...

Guide on how to send files to an API using a form in React with MUI

I am struggling with sending an image through the front-end to my back-end application that is set up to accept data. Here is the code I have: import { useState } from 'react'; import axios from '../../config'; import { useNavigate } fr ...

Can a props be retrieved and passed as an argument to a function?

My goal is to retrieve a prop from MapsStateToProps using react-redux's connect and then pass it to a child component. This prop serves as an argument for a function, which in turn returns something that becomes the state of the child component. Alth ...

Issues with hydrating React local storage hook in custom implementation within NextJS

Currently facing an issue while implementing the localstorage hook in NextJS. The error message I am encountering is: Error: Hydration failed because the initial UI does not match what was rendered on the server.. Any suggestions on what might be causing ...

Showcasing a Collection of Dropdown Options for All Angular Material Icons in Angular Versions 2 and Above

I'm currently developing an Angular 10 application that utilizes Angular Material Icons. I am in search of a method to dynamically showcase a dropdown menu containing all the available Material Icons. My attempt to programmatically retrieve the icon ...

Tips on adjusting font color without activating the :visited state

Currently, I have set up the following HTML and CSS: <div id = "navi"> <ul> <li><a id = "topLinks" class = "currentPage" href = "index.html"> Home </a></li> <li><a id ...

Encase a group of child elements within a parent container using

Currently, I am able to wrap an entire li-element in an ordered list with a link: $(e).wrap('<a href="#" onclick="window.open(\'/xyz/\');return false;"></a>'); This is the HTML construct: <li class=""> ...

Extension Overlay for Chrome

I'm currently working on developing a Chrome extension, but I'm encountering some confusion along the way. Despite researching online, I keep receiving conflicting advice and haven't been able to successfully implement any solutions. That&ap ...

Learn how to display two rows of div elements within a single div container

In my program, I am using a for loop to display multiple div elements in one row. The first div is showing up correctly, but I am having trouble inserting a new div that looks the same as the first one. How can I achieve this? Thank you in advance. "first ...

Which online guide offers the most comprehensive tutorial for mastering react hooks?

Having recently finished my introduction to React course, covering the basics and fundamentals of the framework, I am now eager to delve into learning about React hooks. Unfortunately, I am experiencing some difficulty in finding materials specifically r ...

Error encountered when attempting to reference multiple Select MenuItems in Material UI

Recently, I've been encountering a perplexing error when attempting to open a multiple Select in React+Next.js using Material UI: Error: Argument appears to not be a ReactComponent. Keys: retry This issue seems to be related to a ref. It occurs with ...

Appsync GraphQL query mysteriously failing despite receiving a positive 200 response

I am currently managing one React Amplify app with two environments - one for my wife's blog (www.riahraineart.com) and the other for my own blog (www.joshmk.com). Both websites are operating from the same repository, but I am configuring them differe ...

Utilizing ReactJS to dynamically display various content based on onclick event

Is it possible to dynamically display different content based on button clicks? I want to create a schedule where clicking on specific days like Monday will reveal exercises and timings only for that day. The same goes for Thursday and other days. You ca ...

How can we rearrange the positions of three items in an array?

I am dealing with a function that returns an array of objects structured like this const allGreen = _.filter( sidebarLinks, side => !isServicePage(side.slug.current) ); https://i.stack.imgur.com/dR8FL.png I am attempting to rearrange the positions of ...

Pause until the array is populated before displaying the components

Currently, I am using firebase storage to fetch a list of directories. Once this fetching process is complete, I want to return a list of Project components that will be rendered with the retrieved directory names. How can I wait for the fetching to finish ...

Utilizing numerous CSS rule names in conjunction with the styled API within Material UI

Utilizing Material UI, I aim to customize a component by applying multiple rule names through the styled API. For instance, let's say I want to set the color of the FormLabel Component to blue and the asterisk (required) to red. If I were using the ...