CSS Selectors failing to deliver desired results

Hey, I have a question regarding CSS selectors in relation to my design.

I've been experimenting with manipulating checkboxes using CSS and trying to use multiple selectors like "+", "~", etc. to trigger events when the checkbox is checked or unchecked.

I'm a bit puzzled as to why this approach isn't working, especially when using the "~" selector. My understanding is that "+" selects elements directly after the first element, while "~" selects all elements that come after the previous one. This is my first attempt at this, so I'm still learning and trying to grasp the concept. Does ":checked" not serve as a reference point when chaining multiple selectors? I've included two attempts on the jsfiddle for reference.

Although this will eventually be a navigation menu, for now, I've left it as is to focus on understanding selectors.

HTML

<div id="container">
     <header>
          <input type="checkbox" id="navigation_drawer">
              <label for="navigation_drawer" id="drawer_open"></label>
              <label for="navigation_drawer" id="drawer_close"></label>
          <nav id="navigation">
              <ul>
                  <li id="home_page"><a href="#">Home</a></li>
                  <li id="about_page"><a href="#">About</a></li>                         
              </ul>
          </nav>
     </header>
</div>

CSS

label#drawer_open{
   position: absolute;
   top: 2;
   right: 0;
   margin-right: 1rem;
   background-image: url('http://i.imgur.com/Nxafddw.png');
   background-image: no-repeat;
   width: 35px;
   height: 23px;
}

#navigation{
  background-color:blue;
}

#navigation_drawer:checked + #container #navigation{
  background-color:red;
}

#navigation_drawer:checked + container #navigation{
  background-color:red;
}

Thanks!

Edit: It seems there were some mistakes made while copying and pasting the code here. Here's an updated fiddle of the original problem for your reference. I appreciate the feedback and help provided. https://jsfiddle.net/y9x8accc/6/

Answer №1

It seems the issue with your code is that the + (Adjacent Sibling Selector) only selects the element immediately following it, which in this case is your #drawer_open label and not the nav.

Adjacent Sibling Selector: Also known as an adjacent or next-sibling selector, this selects only the specified element right after the previously defined element.

Your labels might have incorrect indentation, leading you to believe that the nav is a direct sibling. To address this, you should utilize the General Sibling Selector (~).

To fix this issue, use the following code:

#navigation_drawer:checked ~ #navigation{
  background-color: red;
}

View updated fiddle

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

Inject EJS Template String into Express CSS File to Customize Background Image of an HTML Element

I have successfully configured a css file for my Express ejs web app. All the styles are displaying correctly, paths are accurate, and everything is working perfectly. Within one of my ejs files, the structure looks like this: <%- include ("partials/h ...

Troubleshooting spacing and padding problems in Bootstrap 5.2.3's Scrollspy feature

Hello, I'm currently working on developing a new portfolio template that features a list on the left side to guide users through different sections of the page. Utilizing Scrollspy in Bootstrap 5.2.3 has been helpful so far, but I've noticed that ...

choose a div with additional padding at the top

I am looking to create a dropdown menu with a custom arrow design. I have attempted to achieve this by wrapping my select element in a div with an arrow image background. The issue I am facing is that the select element seems to have 3-4 pixels of top p ...

Steps to include a border around text and center align it:

As a beginner in Html and CSS, I am trying to create a heading with the text "Women safety" and wrap it with a border. However, when I apply the border to the text, it covers the entire width, extending both left and right. I only want the border to be a ...

A guide to sending a GET request with custom headers and adding parameters to the request

I recently came across information on the Google OAuth documentation about sending an OAuthGetRequestToken request using the Authorization header of a GET or POST request. The instructions are to include "Authorization: OAuth" in the header, with all param ...

Navigational link behavior in the React component with the <tr> element

I am in the process of developing a React application with a component that shares a resemblance to the following: class TableRow extends React.Component { constructor(props) { super(props) } render() { const handleClick = () ...

The dynamic combination of jCarousel, jQuery Accordion, and fade in effects

Take a look at this link www.aboud-creative.com/demos/mckinley3. You'll find a jQuery Accordion with jCarousel inside the "Developments" section. I have implemented a standard fadeIn function for the logo, accordion, and a stag on the bottom right to ...

The issue in AngularJS 1.4 where the select element value is not binding due to a type mismatch

My ng-model has a value assigned to it, but it is not binding with the selected element. <select data-ng-model="myval"> <option value="? number:2 ?"></option> <option value="2" class="ng-binding">Value 1</option> <op ...

What is the best way to move the Grid upward when the above content is not visible?

Let me demonstrate what I have been working on. Currently, I am creating a weather application to explore the functionalities of https://material-ui.com/. I am attempting to prototype an animation inspired by Google Flights, which can be seen here: https: ...

Tips for changing the background color of a Qtextedit?

After experimenting with HTML, I discovered that using type bgcolor="#ffd814" will change the background color in textedit. But how can I achieve the same result using QAction and QColorDialog? This is the method I tried: void MainWindow::on_actionBackgr ...

Display Bootstrap radios horizontally when the screen size is small or larger, and vertically when the screen

After reviewing the Bootstrap documentation for styling checkboxes and radios, I am searching for a solution to have my radios displayed horizontally on small screens or larger, and vertically on extra small screens. The approach described in the Bootstr ...

Angular Flot Chart Resizing: A Guide to Dynamic Chart S

I have successfully integrated a Flot chart into my HTML using an Angular directive. Here is how it looks: <flot id="placeholder" dataset="dataset" options="options" height="300px" width="100%" style="width:100%;" ng-disabled="graphLoading" ng-class="{ ...

What is the process for creating a div and its children without inheriting the styles of its parent?

Imagine having the following structure: <div class="building"> <p>...</p> <div class="room"> <p>...</p> </div> </div> <div class="building"> <p>...</p> <div class="room ba ...

Show a picture on top of another picture

Is there a way to overlay an image and text on top of another image, similar to what is done on this website ? ...

"Step-by-step guide to layering an HTML element on top of another with CSS

I am facing an issue with positioning a small icon over a slider using HTML and CSS. The problem arises because both the slider and the icon have a position absolute, causing the slider to hide the icon. Here is the CSS code for the icon: #nav { po ...

Finding the correct closing tag in HTML can be accomplished using an algorithm

Currently working on my custom HTML parser in JAVA and have completed the lexer. Now diving into coding the parser to create a DOM tree and ensure the proper construction of my HTML. For example, understanding that an img tag is considered a void tag as p ...

Need help with styling for disabled autocomplete? Check out the attached image

I'm currently working with material-ui and react to create a basic form. Everything is functioning properly except for a small UI issue that I can't seem to figure out how to resolve. When I utilize the browser's auto-complete feature, the i ...

Tips on inserting table data into a textarea input using a button

Is there a way to display the text from the table in a textarea box when clicking the "add" button, without removing the data once it's added? I simply want the text "Add this text to textarea" to show up in the textarea box. https://jsfiddle.net/bj ...

Tips for resolving a top bar on the left side Grid with CSS without it covering the adjacent right side Grid element

I am currently working with material-ui and react, and facing an issue with the positioning of a top bar within a Grid element. The top bar is meant to be fixed in its position, but not at the very top of the page as it is inside a parent Grid element. I ...

What is the best way to trigger the selection options in a dropdown menu with several buttons?

Is it possible to display the options from a select-option tag using a different button? I have either a div or another button. I want to be able to show the list of options from my select tag by clicking this button. Here is my select tag: <select&g ...