employing + in conjunction with the > selector

As I delve into learning about responsive menus, I came across the hamburger checkbox hack through a simple Google search. My goal is to only display direct descendants when clicking on the hamburger icon and hide the sub menus.

#toggle-menu {
  cursor: pointer;
}

#primary-nav,
#menu-toggle,
#primary-nav>ul {
  display: none;
}

#menu-toggle:checked+#primary-nav {
  display: block;
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet" />
<div class="menu">
  <a href="#">
    <h1>Company</h1>
  </a>
  <label for="menu-toggle" id="toggle-menu"><i class="far fa-bars"></i></label>
  <input type="checkbox" id="menu-toggle">
  <ul id="primary-nav">
    <li>home</li>
    <li>dropdown
      <ul>
        <li>sub1</li>
        <li>sub2</li>
      </ul>
    </li>
  </ul>
</div>

If you have any insights or suggestions, they would be greatly appreciated!

Answer №1

If you want to display the ul only after the input, first hide all uls, then reveal the ul immediately following a checked input

You can assign a class to all inputs and apply the same action to the submenu.

#toggle-menu {
  cursor: pointer;
}

.toggler,         /*checkboxes with this class will be hidden */
ul {              /* hide all menus (consider assigning them a common class) */
  display: none;
}

.toggler:checked+ul {    /* show the ul only if it directly follows a checked toggler input */
  display: block;
}
<link href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" rel="stylesheet" />
<div class="menu">
  <a href="#">
    <h1>Company</h1>
  </a>
  <label for="menu-toggle" id="toggle-menu"><i class="far fa-bars"></i></label>
  <input type="checkbox" id="menu-toggle" class="toggler">
  <ul id="primary-nav">
    <li>home</li>
    <li>
      <label for="sub-menu1">dropdown</label>                <!-- use a label and target the checkbox below -->
      <input type="checkbox" class="toggler" id="sub-menu1"> <!-- add this -->
      <ul>
        <li>sub1</li>
        <li>sub2</li>
      </ul>
    </li>
  </ul>
</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

Using HTML input checkboxes in conjunction with a JavaScript function

After creating a basic payment form using HTML/CSS/JS, I wanted to implement checks on user inputs using HTML patterns. In addition, I aimed to display a pop-up alert using JS to confirm the form submission only after all necessary inputs are correctly fil ...

Adjust the size of three panels (left, middle, right) using Javascript, ensuring that the middle panel remains unchanged in size

I am a newcomer to JavaScript and currently working with HTML, CSS, and JS files. I have three panels - left, center, and right - that I want to resize. The left panel resizes correctly when dragging the column border. https://i.stack.imgur.com/LxhUX.png ...

Arrange the contents within a div with Bootstrap 4 for proper alignment

Just stepping into the world of front end development and my question might come off as quite basic. Currently, I am delving into ReactJS. https://i.sstatic.net/o9tDo.png My current challenge is related to the following code snippet: <div className=" ...

When an element is transferred to a different <div>, it does not automatically inherit its new CSS styles

There's an element with existing behavior that needs to be moved to meet new requirements without losing its original styles. The challenge is applying new styles to the element after it's been moved. For example: <div id="existingStructure" ...

How can I centrally align my navigation bar using CSS?

I've been working on a navigation bar for my website for a few days now. However, I'm having trouble centering it and using margin auto doesn't seem to be working. Here's the HTML code I have: <!DOCTYPE html> <html> < ...

The use of a custom padding on a text input with the Bootstrap 3 form-control class can cause the text to be hidden

It seems like the code is working fine in Opera and Chrome, but in Firefox and IE11, the text is getting hidden, which is not very enjoyable. I don't have a Mac, so I can't test it on Safari. You can see the issue in action on this Plunker. I&a ...

Buttons surrounding the image are exhibiting varied responsiveness

This is my initial inquiry. In the application I'm working on with rails and bootstrap, I've noticed that when the screen size changes to small, the elements around the image do not adjust properly like they do on a medium or large screen. Here ...

Having trouble with displaying the CSS background image?

I've been attempting to configure background images using CSS, but for some reason, I'm not able to get the images to show up correctly. Below is the CSS code that I'm working with: a.fb { background-image: url('img/Facebook.png&a ...

Enhancing divs with border using CSS Transforms

I'm currently in the process of constructing an isometric grid made up of cubes using CSS, but I've encountered a problem with outlines. Here's my desired outcome: cube design However, this is what I have managed to achieve so far: html cu ...

Arrange Bootstrap grid elements so that the majority of the content is aligned at the top, while keeping the button positioned

I have a layout using Bootstrap 4 grid to showcase some elements. I want the titles and textual content to be aligned vertically at the top, which is working fine. However, there is a <button> at the end of each grid element that I want to be vertica ...

Rails: How come my personalized stylesheet isn't taking priority over the font family?

I have included the Skeleton boilerplate css in my application from this source. It can be found within the directory app/assets/stylesheets. app/assets/stylesheets ├── application.css ├── custom.scss ├── normalize.css └── skeleton ...

Update picture for mobile and tablet view (Semplice template)

I am currently using the Semplice Theme for my portfolio website and I have come across a little issue. I want to figure out how to change an image specifically on smaller devices like mobile phones and tablets. However, I'm still quite new to coding. ...

The navigation area is too small for the social media buttons to fit

Hi everyone, I'm encountering an issue with the social media buttons on my website. They're not staying in the navigation header area despite trying to use the float attribute. Here are some images illustrating the problem: https://i.stack.imgur ...

Evenly distribute top and bottom padding within a fixed-height border using CSS

I'm facing a challenge with centering the content vertically on an online certificate that users can print out. The main issue lies in aligning the inner wrapper (#wrapper) inside the outer border (#outerBorder), which has a fixed height to ensure pro ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

Flipping a combination of CSS 3 and JavaScript cards will cause them to flip all together in unison

Hello! I came across a really cool card flip code that is IE compatible. I made some modifications to it and got it working, but there's one problem - instead of flipping the selected card, it flips all the cards. I tried modifying the JavaScript code ...

Does anyone know if it's achievable to include a background position within the img /asp:image tag?

Currently, I am endeavoring to enhance the loading speed of my webpage. The initial approach I decided to pursue is base64 conversion. On my homepage, there are a total of 18 small images that need to be loaded. Since base64 encoding increases image size ...

A guide to traversing a class and pinpointing each element that contains a particular classlist property

Here is my code snippet that generates 4 spans within a class. When a user clicks on a span, it changes color to indicate that it has been clicked. <head> <style> .tagger1010 span { padding: 6px 10px; ...

Set the background image to always cover the entire page

My goal: When the site has a widescreen aspect ratio, I want the background image to scale to 100% horizontally, cropping the top and bottom. This can be achieved easily using background-size: cover; However, if the site has a vertical aspect ratio (such ...

What is your approach to setting up this navigation system with HTML and CSS?

Navigation Nav w/ Hover State Can you create a navigation menu using HTML and CSS? The white box serves as a placeholder for a logo. Note: The "Products" link has a drop-down menu feature. See my current implementation below, although it's not ye ...