Issue with toggle button not functioning properly when used with htmlFor in NextJS

I'm currently working on implementing a toggler that, when clicked, will hide certain elements. Specifically, I want it to hide the sidebar once it's functioning correctly. However, I've hit a roadblock and can't seem to figure out how to hide these elements.

The technology I'm using for this project is NextJS.

<input type="checkbox" id="sidebarToggle" className={styles.sidebarToggle} />
<div className={styles.dashboard}>
  <div className={styles.sidebar>
    <div className={styles.sidebarHeader}>
      <h3 class={styles.brand}>
        <span><img src="/unlink.svg" alt="unlink"/></span>
        <span>easyTechServ</span>
      </h3> 
      <label htmlFor="sidebarToggle"><img src="/menu-alt.svg" alt="menu-alt"/></label>
    </div>
  </div>
</div>

Below you'll find the corresponding CSS:

.sidebarToggle:checked ~ .sidebar .sidebarHeader h3 span:last-child,
.sidebarToggle:checked ~ .sidebar li span:last-child {    
  display: none;
}

Answer №1

Your current CSS selector is not correct as you are trying to use the sibling selector (~). However, .sidebar and .sidebarToggle are not siblings, so the intended hiding effect will not take place.

I would recommend avoiding over-specifying your CSS selectors and removing any unnecessary specificity for better code readability.

Please review the HTML+CSS snippet below:

.sidebarToggle:checked ~ .dashboard .brand span:last-child,
.sidebarToggle:checked ~ .dashboard li span:last-child {
  display: none;
}

.brand span:last-child {
  color: crimson;
}
<input type="checkbox" id="sidebarToggle" class="sidebarToggle" />
<div class="dashboard">
  <div class="sidebar">
    <div class="sidebarHeader">
      <h3 class="brand">
        <span><img src="/unlink.svg" alt="unlink"/></span>
        <span>easyTechServ</span>
      </h3>
      <label for="sidebarToggle">
        <img src="/menu-alt.svg" alt="menu-alt"/>
      </label>
    </div>
  </div>
</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

The error message "global.HermesInternal - Property 'HermesInternal' is not found in the type 'Global & typeof globalThis'" appeared

I ran the auto-generated code below: $ react-native init RepeatAloud App.tsx /** * Sample React Native App * https://github.com/facebook/react-native * * @format * @flow strict-local * ...

Issue with Firefox: Calc function in CSS3 not functioning as expected

When using the calc() function to set scale values in my less class as a function, I encountered an issue where it works on Chrome but not Firefox. Below is the code snippet for my class: .zoom(@value) { transform: scaleX(@value); -moz-transform ...

Misalignment of Font-awesome icons in the footer section

I've implemented a footer on my website. My goal is to have the icons centered both vertically and horizontally, with the colored area: Always positioned at the bottom of the screen No taller than the icons themselves Here's the code snippet: ...

Stylish Material Design Icons

Our website utilizes CSS classes to display icons across various pages. .alert { /* used for "alert" type messages */ background: url(images/alerts.png) no-repeat left top; padding: 5px 0 15px 35px; margin: 0; } Use: <p id="ctl00_ctl00_ContentMessagePa ...

Leveraging an intersection type that encompasses a portion of the union

Question: I am currently facing an issue with my function prop that accepts a parameter of type TypeA | TypeB. The problem arises when I try to pass in a function that takes a parameter of type Type C & Type D, where the intersection should include al ...

What is the best way to make a flexbox stretch to fill the entire screen

Can someone guide me on how to ensure my flexbox expands to fill the entire screen? I am working on a website and I want to eliminate any empty spaces from it. >> https://i.sstatic.net/uAooe.png I have attempted setting margin and padding to zero b ...

Using the ternary operator in React to implement inline styles

Within my React/Typescript project, I aim to dynamically exhibit a color based on the presence or absence of a value in payload[1]. In the code snippet below, note the usage of an inline style tag. <li className="recharts-tooltip-item" style={ ...

The API has been triggered twice

I am currently working on a React component where I have implemented an API call using the useEffect hook with an empty dependency array. However, I noticed that the API is being called twice and I can't seem to find the reason behind it. import { use ...

Looking to troubleshoot the "404 page not found" error in React JS?

This code snippet includes the navigation bar component that is currently functioning without any issues. navBar.jsx import React, { useEffect, useState } from 'react'; import './navbar.css' import { Link, useLoaderData, useNavigate } f ...

Utilizing TypeScript path aliases in a Create React App project with TypeScript and ESLint: A step-by-step guide

I utilized a template project found at https://github.com/kristijorgji/cra-ts-storybook-styled-components and made some enhancements. package.json has been updated as follows: { "name": "test", "version": "0.1.0" ...

Three images intersecting one another

I am facing an issue with the transparency of one specific image. The images on the left and center look fine, but the one on the right is not hidden behind the center image. I have tried using overflow: hidden and z-index, but haven't had any success ...

Having trouble retrieving the pathname of a nested route within middleware.js in next js version 14

I am currently referring to the official App Router documentation for Authentication on this page My goal is to extract the pathname from the next URL export function middleware(request) { console.log('now we are in middleware'); const { ...

Align buttons to the center within a row using Bootstrap

I am having trouble centering all buttons in each col-md-4: <div class="row position-relative mt-2"> <div class="col-md-4 center-block"> <div class="center-block align-to-bottom"> <div class="btn-group-vertical"> ...

Combining two tubes in React threejs to form a tee-piece is proving challenging with CSG functionality not yielding desired results

My current project involves creating a tee-piece fitting in the plumbing realm, which consists of merging two tubes with three openings. The visual representation can be seen in this image. In my development process using threejs, I've written code t ...

What is the best way to align my text to the bottom of a table header in a vertical manner?

I am working with a series of nested tables and this is the basic structure: <table> <tr> <td> <table> <thead> <tr> <th></th> <th ...

Styling GTKSharp with CSS is a great way to customize

I am in the process of converting an existing C/GTK+ GUI application to C# using GTKSharp in Visual Studio 2017. To facilitate this, I have installed the following package https://www.nuget.org/packages/GtkSharp/3.1.3 via NuGet. Below is how I am loading ...

Date Selector Tool for Input Field

Does anyone know how to change the color of the date picker that appears in certain browsers but is unsure about the selector's name? HTML: <form action="pledge.html" method="post"> <input type="date" id="birthday" name="user_bda ...

How to customize the page background color in Next JS

I am currently working on a project using Next Js and have encountered an issue. I have a global.css file set up in the project, but I need to change the background color of a specific page without affecting the rest of the project. Although I have tried u ...

Ways to customize the appearance of a react-chartist component

I recently created a React JS component that utilizes the amazing react ChartistGraph component. However, I've run into an issue where the default CSS of ChartistGraph seems to be conflicting with my custom styling. While there is plenty of documentat ...

Turn off all the styles associated with a specific CSS selector

Looking for a solution to override CSS rules for the .lp-caption selector? .lp-caption { -moz-border-bottom-colors: none; -moz-border-left-colors: none; -moz-border-right-colors: none; -moz-border-top-colors: none; backgr ...