Ways to make content react to opening or closing of the sidebar

Hey there! I understand that this question has been asked before, but my situation is a bit different. Here's the deal: on my website, I have a sidebar that consists of two overlapping divs which are toggled by a button. Currently, the content of the page sits on top because it was easier to work with. My question is, how can I adjust the page content to fit the new window size when the sidebar is open and then revert back when it's closed?

#A,
#B {
  position: absolute;
  transition: all 800ms;
  -webkit-transition: all 800ms;
  -moz-transition: all 800ms;
  -ms-transition: all 800ms;
  -o-transition: all 800ms;
  margin-top: 20px;
}

#A {
  top: 0px;
  width: 14em;
  bottom: 0px;
  background: #939393;
  border: solid #000000 2px;
}

#B {
  top: 0px;
  left: 14em;
  right: 0;
  bottom: 0px;
  background: #3b3f45;
}

/*Hide the checkbox*/
#toggle {
  display: none;
}
.toggle label:hover, .toggle label:active, .toggle input:hover+label, .toggle input:active+label {
  border: 1px solid #000000;
}

.toggle label {
  position: fixed;
  border-radius: 4px;
  /*Set the left position to the same as the sidebar */
  left: 14em;     
  margin: 8em 1.5% 5px;
  z-index: 2;
  transition: all 800ms;
  -webkit-transition: all 800ms;
  -moz-transition: all 800ms;
  -ms-transition: all 800ms;
  -o-transition: all 800ms;
  background: #FFFFFF;
  padding: 4px 6px;
  background: #75787c;
  cursor: pointer;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: bold;
  text-align: center;
}

/* Initial sidebar state */
#toggle ~ #A {
  left: 0;
}

/*move both containers on toggle*/
#toggle:checked ~ #A,
#toggle:checked ~ #B {
  left: -14em;
}

/*move label to follow sidebar animation*/
#toggle:checked,
#toggle:checked ~ label {
  left: 0;
  background: #FFFFFF;
}
<div class="toggle">
  <div id="A">
    </div>
  <input id="toggle" type="checkbox" checked>
  <label for="toggle">Menu</label>
  <div id="B">
  </div>
</div>

Answer №1

Your utilization of the selector ~ is incorrect

The element1 ~ element2 selector targets instances of element2 that come after element1.

Both elements must share the same parent, but element2 doesn't necessarily have to be directly following element1.

For instance

Apply a background color to all <ul> elements following a <p> element with the same parent:

p ~ ul {
    background: #ff0000;
}

In your situation, #toggle ~ #A and #toggle:checked ~ #A aren't taking effect because #A isn't preceded by #toggle.

To rectify this, adjust the placement of #A in the DOM like so:

#A, #B {
    position: absolute;
    transition: all 800ms;
    -webkit-transition: all 800ms;
    -moz-transition: all 800ms;
    -ms-transition: all 800ms;
    -o-transition: all 800ms;
    margin-top: 20px;
    box-sizing: border-box; /*The width and height properties (and min/max properties) includes content, padding and border, but not the margin*/
}
#A {
    top: 0px;
    width: 14em;
    bottom: 0px;
    background: #939393;
    border: solid #000000 2px;
}
#B {
    top: 0px;
    left: 14em;
    right: 0;
    bottom: 0px;
    background: #3b3f45;
}
/*Hide the checkbox*/
 #toggle {
    display: none;
}
.toggle label:hover, .toggle label:active, .toggle input:hover+label, .toggle input:active+label {
    border: 1px solid #000000;
}
.toggle label {
    position: fixed;
    border-radius: 4px;
    /*Set the left position to the same as the sidebar */
    left: 14em;
    margin: 8em 1.5% 5px;
    z-index: 2;
    transition: all 800ms;
    -webkit-transition: all 800ms;
    -moz-transition: all 800ms;
    -ms-transition: all 800ms;
    -o-transition: all 800ms;
    background: #FFFFFF;
    padding: 4px 6px;
    background: #75787c;
    cursor: pointer;
    font-family: Arial, Helvetica, sans-serif;
    font-weight: bold;
    text-align: center;
}
/* Initial sidebar state */
 #toggle ~ #A {
    left: 0;
}
/*move both containers on toggle*/
 #toggle:checked ~ #A, #toggle:checked ~ #B {
    left: -14em;
}
/*move label to follow sidebar animation*/
 #toggle:checked, #toggle:checked ~ label {
    left: 0;
    background: #FFFFFF;
}
<div class="toggle">
    <input id="toggle" type="checkbox">
    <label for="toggle">Menu</label>
    <div id="A"></div>
    <div id="B"></div>
</div>

Note that I also applied box-sizing: border-box to #A, #B to factor in the borders when calculating the width(14em). If you remove it, you'll notice how the border-right of #A isn't visible when toggled.

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

What could be the reason for the selection box in my form not changing the items when togg

I'm having an issue with my form selection box code that used to work but is now not functioning properly. Could someone please help me identify where the error lies? Essentially, I have a form with the ID #main and a select box named #chart-type. Th ...

Footer not adhering to the bottom of specific pages

I have been using the code snippet below to ensure that most of my pages stick to the bottom. However, I've noticed that the ones that don't are sub-menu items that contain a contact form with captcha. I'm not sure what's causing this i ...

Adding text to the end of a web address through a submission form

Here is an example of a form: <form method="post"> {% csrf_token %} <input class="search" type="text" name="q" placeholder="Search Engine"> <input type="submit"> I am looking t ...

Sticky positioning is not maintaining its position at the top

I've noticed a strange behavior where, when I scroll down, the yellow box goes on top of the blue box. I have set both boxes to have a position sticky so that they should stay in place and not overlap. However, I want only the orange box to be scrolla ...

Firefox displays text smaller than Chrome

Hey there! I'm facing a little issue with my code. It renders correctly on Chrome (to the right), but on Firefox (to the left) it's a bit off. Any suggestions on how I can fix this? It usually corrects itself after a refresh (zoom in/out), but al ...

What is the best way to implement a front-end CSS style for text instead of a background style?

I have HTML text I want to style with a color like rgba(0,0,0,0.1), but I want the color to appear above or on top of the text, not below or behind it. Issue: How can I accomplish this? (CSS or JavaScript solutions are welcome). Thank you in advance for ...

Ensure the left column extends all the way down

I've been struggling with this issue for almost three days now. After reading numerous articles on creating a three-column layout and experimenting with absolute and relative positioning, I'm still not able to achieve the desired result. What I& ...

scroll both horizontally and vertically

I came across a tutorial on creating a scrolling page using jQuery and I followed it successfully for horizontal scrolling. However, I'm now wondering if it's possible to implement both horizontal and vertical scrolling on the same page. For in ...

Height of Nav-Links in Bootstrap 4

Is there a way to modify the height of the navigation links in Bootstrap? To set a specific height, such as for the navbar, you may encounter issues where it doesn't occupy the entire space. CSS CODE: .navLogo { width: 15%; height: 100%; } ...

In Vue, it is not accurate to measure overflow

I am working on creating an overflow effect with tagging that fades out at the beginning to provide a subtle hint to users that there is more content. Here is what it currently looks like: https://i.stack.imgur.com/fXGBR.png To achieve this, I added a fa ...

What is the best way to ensure uniform header height on cards or similar elements using flexbox?

Avoid using hacks Do not hard code the solution. The goal is to create a solution that works for dynamic content and responsive screen sizes, rather than just one specific case. The issue lies in the fact that the headers are not direct children of the e ...

Teaching sessions along with the implementation of functions

I've created a set of input fields with the class replaceInput. The idea is to have a simple function that clears the value when the user focuses on the field, and then returns it to 'X' if the field is empty on focus out. My query is, coul ...

Learn how to utilize the getElementByClassName method in JavaScript to insert checkboxes into two lists that share the same class name

I have a situation where I have two lists sharing the same class name. I need to dynamically create checkboxes in both of these lists based on XML data. My current dilemma lies in how to properly utilize getElementByClassName in JavaScript to add checkbox ...

Unexpected outcomes when generating jQuery pages using CSS

I have created a piece of JavaScript code that populates 3 divs with icons in even rows. However, I am facing an issue where my first two rows align as expected, but the third row starts aligned with .col-5. Can you help me troubleshoot this? function row ...

Disable the dragging of a draggable div when another div is dropped inside

In my view, I have displayed reservations and tables as div elements. The tables are draggable and droppable, while the reservations are only draggable. My goal is to be able to drag a table to a desired position and then place a reservation on it. Once th ...

What is the best way to alter the state of a button when it is clicked in React, and then return it to its original state when

I'm working on a button that I've defined like this: <button className={class} onClick={() => changeClassProperty()}> {classText} </button> My goal is to update both the CSS styling class and the text of the button A when ...

Having trouble locating the code for adding a hover effect to the FontAwesome icon

In regards to my portfolio located here: My Portfolio When hovering over the "Marketing Automation" section, a small fa fa-tablet icon appears to the left which turns blue when hovered over. I would like for this icon to remain white, rather than changing ...

Why does my design become chaotic when switching from four columns to two columns in responsive mode?

My layout is currently set up like this: But when I try to resize it into two columns, I encounter a new issue: I don't want to add height to the columns. How can I resolve this problem? Thank you. ...

Is the latest version of three.js not compatible with Blender models?

In the past, I would export blender models to use them with three.js, starting from version r47. However, after updating to the latest release, r49, it seems that the same code that used to display the model in the browser no longer works. Should I rever ...

Could LXML potentially determine the value of a class in HTML?

For instance, consider the following scenario: <button class="oW_lN oF4XW sqdOP yWX7d _8A5w5 ">MyTextIsHere</button> The "MyTextIsHere" part of the code can contain only two specific values: "Yes" and "No". Is it feasible to determine wh ...