Sibling selector beside not functional

Trying to create a sliding sidebar without using JavaScript has presented me with a challenge involving an adjacent sibling selector that just won't function as expected.

Here is the code snippet causing the issue:

.sidebar {
  position: fixed;
  top: 100px;
  right: 0px;
  z-index: 0;
  width: 120px;
  height: 100px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.00);
  border-bottom: solid 1px #181918;
  border-left: solid 1px #181918;
}
.sidebar ul li {
  font-family: 'Zona Pro';
  font-size: 18px;
  margin-bottom: 16px;
  -webkit-font-smoothing: antialiased;
  color: rgba(24, 25, 24, 0.78);
  cursor: pointer;
}
#sidebarToggler {
  display: none;
}
#sidebartoggler + .sidebar {
  top: 500px;
}
<div class="pageWrap">
  <input type="checkbox" id="sidebarToggler" />
  <div class="sidebar">
    <ul>
      <li>Settings</li>
      <li>Log out</li>
    </ul>
  </div>
  <div class="userNameDiv">
    <label for="sidebarToggler">
      Fale1994
    </label>
  </div>
  <div class="pageContent">
    @RenderBody()
  </div>
</div>

The CSS seems to be working flawlessly except for one last line... I've tried multiple approaches but can't seem to make it work.

Upon clicking on the label, the status of the checkbox toggles between checked and unchecked. The aim is for the CSS to then adjust the 'top' attribute of the sidebar to 500px when checked. I even experimented with changing the background-color, all to no avail.

Answer №1

Two key points you need to address:

  • Make sure to include the :checked pseudo-class for the checkbox
  • Remember that CSS is case-sensitive, so use #sidebarToggler instead of #sidebartoggler

Snippet

.sidebar {
  position: fixed;
  top: 100px;
  right: 0px;
  z-index: 0;
  width: 120px;
  height: 100px;
  padding: 30px;
  background-color: rgba(255, 255, 255, 0.00);
  border-bottom: solid 1px #181918;
  border-left: solid 1px #181918;
}
.sidebar ul li {
  font-family: 'Zona Pro';
  font-size: 18px;
  margin-bottom: 16px;
  -webkit-font-smoothing: antialiased;
  color: rgba(24, 25, 24, 0.78);
  cursor: pointer;
}
#sidebarToggler {
  display: none;
}
#sidebarToggler:checked + .sidebar {
  top: 500px;
}
<div class="pageWrap">
  <input type="checkbox" id="sidebarToggler" />
  <div class="sidebar">
    <ul>
      <li>Settings</li>
      <li>Log out</li>
    </ul>
  </div>
  <div class="userNameDiv">
    <label for="sidebarToggler">
      Fale1994
    </label>
  </div>
  <div class="pageContent">
    @RenderBody()
  </div>
</div>

Answer №2

#sidebartoggler:checked + .sidebar

Make sure you include the :checked pseudo-class in your CSS selector.

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

Customizing HTML Select Elements

Can the HTML Select attribute be customized to have a flatter appearance? Although I can set the border to be 1px solid, the dropdown part still appears 3D and unattractive. ...

Place an IconButton component next to each Material UI TableRow

I am trying to include an icon next to the material UI table row component. Similar to the hint icon shown in the screenshot below Here is my attempt so far, but it's not functioning as expected: Check out the code on CodeSandbox https://i.stack.i ...

What is causing the floated element to overlap the element below instead of vice versa?

div { background-color: #00FFFF; } p { width: 50px; height: 50px; border: 1px solid black; margin: 0px; } #p1 { background-color: red; float: left; } #p2 { background-color: green; } #p3 { background-color: orange; } #p4 { backgr ...

Creating a progress bar that includes hyperlinks: A step-by-step guide

Currently, I am in the process of developing an application form that consists of 9 pages of questions. Initially, I had implemented a progress bar which indicated completion by filling up 1/9 for each page completed. However, I have been informed that thi ...

Exploring the Features of PrimeNG Table Component in Angular 8

After attempting to implement p-table (PrimeNG table) in my Angular project and importing all necessary dependencies and modules using the CLI, I encountered the following error: ERROR: The target entry-point "primeng/table" has missing dependencies: - @ ...

What is the complete pathway in CSS?

Consider this scenario: within my CSS file, there is a line of code used to display an image - specifically, a label icon located in the sidebar. background-image:url(../assets/images/icons/arrow_state_grey_expanded.png); However, due to its relative pat ...

Adding a button to the right of a text-field input in Vuetify app

I need advice on the best way to use Vuetify in order to display a button to the right of a text-field (attached). I've checked the main site for information but couldn't find anything related to this specific scenario. <v-row clas ...

Using a web browser control to choose a radio button and then submit the form

Currently, I am attempting to use my WebBrowser control in order to select a radio button and also click on a submit button that doesn't have any id or name attached. Below is the snippet of HTML code from the webpage: <form method="post> & ...

Incorporate an external hyperlink into a tabPanel or navbarMenu within an R Shiny application

I am struggling with adding external hyperlinks to the tabs and dropdowns in a navbar setup using Shiny (implemented with bootstrapPage). While I have come across solutions for linking to other tabs within a Shiny app, my goal is to link to an external web ...

Positioning MDL cards in HTML documents

Seeking guidance on positioning MDL cards alongside existing text. I've attempted various methods without success so far, and the desired outcome has not been achieved. The goal is to have text aligned to the left (which is currently satisfactory) wi ...

I am curious if there is a wysiwyg web editor extension specifically designed for VS2010 available?

In my experience, I have been working with C#, HTML coding using VS2010 and MVC. Utilizing VS2010 has proven to be an invaluable tool for me in this process. Currently, I find myself needing to create some straightforward static web pages. I am wondering ...

A newline within the source code that does not display as a space when rendered

Written in Chinese, I have a lengthy paragraph that lacks spaces as the language's punctuation automatically creates spacing. The issue arises when inputting this paragraph into VSCode - there's no seamless way to incorporate line breaks in the s ...

Javascript data table pagination and custom attributes resulting in unexpected truncation of strings

Currently, I have implemented an Ajax functionality in my template to receive a JSON response and dynamically populate a datatable with the elements from that JSON: $('.bicam_tests').click(function(){ $.ajax({ type: 'GET', ...

I am interested in incorporating a vertical scrollbar into a gridview in ASP.NET

I've successfully bound data to a grid view in my ASP.NET application. I've implemented pagination, but instead of that, I'd like to incorporate a scroll bar to prevent lengthy paging. Below is the code for my grid view: <div class="box ...

What is the best way to evenly distribute input elements horizontally within the parent container?

I am working on a layout with five select inputs that I want to arrange horizontally within the parent div. However, the current code setup is not achieving the desired layout: <div id="divtable"> <select class="abc"></select> ...

Show the input from one HTML form on a different HTML form

Seeking guidance on utilizing local storage in HTML5. How can I extract information from one form and present it on another HTML form page? Picture this: two forms at play here. Upon populating form 1 and hitting the submit button, the content should be v ...

What is the best way to conceal the broken image icon using PHP?

UPLOAD PICTURES IN DATABASE: if (!isset($_FILES['gambarid']['tmp_name'])) { echo ""; }else{ $rand5=rand(); $image_name5= addslashes($_FILES['gambarid']['name']); $fileext5=explode('.&ap ...

Is there a method to exempt a particular input field from form validation?

Is there a way to prevent a form validation error message from displaying in other input fields when using an input url field within a rich text editor component (RTE) placed inside a form element? <input type="url" v-model="state.url ...

Align text to the right and do not include any images

I am attempting to align the images back to the left under the title, while only floating the description text to the right. I am struggling with clearing the image floats. HTML: <div class="title">System Displays</div> <div class="descrip ...

Aligning images in a table grid of 300px by 300px dimensions

I've included my code below and I am searching for the most effective method to use CSS to center an image both horizontally and vertically within a 300 by 300 pixel square. If the image is larger, it should be resized to fit within the square, while ...