What is the best way to create a seamless transition effect for two floating divs as they move in sync with each other

I'm in need of some assistance with CSS coding. I am currently working on creating a sidebar that slides in, similar to mobile pages, but instead of using absolute positioning, I have opted to float two divs inside a wrapper.

My ability to explain things is not the best, so I'll provide a simple example to illustrate my issue.

The HTML:

<!-- Used for toggling the sidebar -->
<input type="checkbox" id="toggle" hidden="hidden">

<!-- The wrapper -->
<div class="wrapper">

  <!-- The content -->
  <div class="content">
    <label for="toggle">Show</label>
    <!-- Content goes here -->
  </div>

  <!-- The sidebar -->
  <aside class="sidebar">
    <label for="toggle">Hide</label>
    <!-- Sidebar content goes here -->
  </aside>

</div>

The CSS:

/* General settings */
* { margin: 0; padding: 0; }

html, body {
  width: 100%;
  height: 100%;
}

/* Toggle effect */
#toggle:checked + .wrapper > .content {
  margin-left: -25%;
  transition: all ease 1s;
}

/* Wrapper styling */
.wrapper {
  width: 100%;
  height: 100%;
  overflow: hidden;
  transition: all ease 1s;
}

/* Content styling */
.wrapper > .content {
  width: 100%;
  height: 100%;
  overflow: auto;
  float: left;
  background-color: #2ecc71;
  transition: all ease 1s;
}

/* Sidebar styling */
.wrapper > .sidebar {
  width: 25%;
  height: 100%;
  overflow: auto;
  float: left;
  background-color: #27ae60;
  transition: all ease 1s;
}

When you click "Show" or "Hide," you'll notice that the sidebar does not slide in or out smoothly. Is there a way to achieve a smoother transition?

Answer №1

To simplify, toggling a class on the body tag is an effective method. In this case, let's use .sidebar-open:

.container,
.sidebar {
  transform: translateX(0);
  transition: all .5s ease-in-out;
}    

.sidebar-open .container,
.sidebar-open .sidebar {
  transform: translateX(-200px);
}

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

Is there a way to resolve the issue of this div sticking to the top of the

I am currently working on building a portfolio website. One issue I'm facing is that my first div (for the "About Me" section) appears below my second div on the webpage. My goal is to have the first div displayed in full screen on both mobile and des ...

What is the best way to restrict the length and number of lines in a textarea based on pixel dimensions?

I am looking to create a textarea where people can select different fonts, such as "sans-serif" and "serif", to type in text. I plan to have buttons labeled "sans-serif" and "serif" that will change the font of the textarea when clicked. However, there ar ...

Enabling the `dbc.Select()` function to require a dropdown selection in DASH does not automatically highlight an empty dropdown

While working on my plotly-Dash form, I utilized bootstraps dbc.Select() to generate a dropdown. I hoped to have it outlined in red, similar to my dbc.Input() boxes, when no option is selected. However, the red border is not showing up. I set required = ...

Adjust the vertical alignment in Material UI Grid to allow for proper tab navigation while maintaining the original order

When using Material-UI's standard grid system, the alignment of a new grid item after all 12 "space units" are utilized is based on the longest element within those 12 "space units". Is there a way to disrupt this type of alignment? If you take a look ...

Looking to display a div with both a plus and minus icon? Having trouble getting a div to show with left margin? Need assistance hiding or showing div text

Can someone please review this source code? Here is the demo link: http://jsfiddle.net/bala2024/nvR2S/40/ $('.expand').click(function(){ $(this).stop().animate({ width:'73%', height:'130px' }); $( ...

The art of positioning headers using CSS

Just a quick question: I'm working on formatting my HTML and CSS, and I have the following code: <div class="container"> <div id="header"> <div id="navbar"> </div> </div> </div> This is how my CSS looks ...

Why is it possible to apply column width to a div element, but not to a button?

This code functions properly and sets the button to a size 4: <div class="col-xs-4"> <button class="btn btn-block btn-primary">Like</button> </div> However, the following code does not work as expected: <div> <bu ...

Creating a responsive menu that integrates seamlessly with WordPress

Currently, I am in the process of redesigning a theme that is based on the Educampus theme: Educampus I've encountered an issue where the responsive menu goes to two lines at a certain size: col-lg or col-md: https://i.sstatic.net/CbhZV.png col-sm ...

Troubleshooting CSS and Bootstrap display problems and bugs

Hey, I'm having some trouble with the PHP code for a Star Wars webpage that I'm working on for a university project. I'm encountering issues that I can't seem to fix. I've set up templates and have a basic design ready to connect t ...

The size of HTML select input varies among IOS, Android, and Windows devices

Does anyone know of a CSS property that can be used to tailor the styling specifically for IOS devices in order to achieve a similar look to that of windows and android?https://i.sstatic.net/VMSXb.jpg I attempted using -webkit, but unfortunately it did no ...

Looking for a way to make text wrap neatly inside a div?

I am working on a piece of code that includes 3 images with captions below them. I want to ensure that the width of the text does not exceed the width of the image, even though the image widths can vary. If the text extends beyond the image width, it shoul ...

Personalized jQuery Slider, Go back to initial slide

Working on enhancing my jQuery skills by constructing a basic slider with 3 slides. The slider wrapper, with a fixed width of 1400px and a height of 350px serves as the outer container. Within this wrapper lies an unordered list where its items are floate ...

Utilizing selected Bootstrap dropdown option for extracting value

I'm currently learning about development and am trying to create a bootstrap dropdown with 3 different background image options. My goal is to change the background based on the selected option from the dropdown. However, I am struggling to figure out ...

How do I adjust the ul size to be proportionate to the header?

I have a list with corresponding hyperlinks in my code, and I am trying to make these elements the same size as the header. I attempted adding auto padding and height auto but it did not produce the desired result. Here is the HTML snippet: <header id ...

Preventing right-aligned images from overflowing inside a div on Google Chrome

I am currently utilizing the jQuery cycle plugin to display a slideshow of images that floats to the right within an outer div element. The CSS I am using is as follows: .slideshow { float:right; margin-left: 20px; } The image alignment works per ...

Numerous divs positioned above a myriad of images

Is there a way to create multiple images with a tag name overlaying each one as a link? I've been able to add a tag name on top of one image, but not on others. Every time I try to duplicate the div containing the image and tag, the tags do not show u ...

The "in" class for Bootstrap collapse is not functioning as expected

I am currently experimenting with Bootstrap's collapse class to create a toggle effect for an element when a checkbox is clicked. The initial state of the page should have the checkbox unchecked and the element displayed. Upon clicking the checkbox, t ...

Using jQuery to replace the content of a div with a delay?

I am attempting to utilize jQuery to create a fade effect on an element, replace its innerHTML content, and then fade it back in once the new content has been added. I have successfully managed to replace the element's content using the .html() method ...

Find out if the visible area contains an open dropdown menu and take action if it is not

I am currently working on a way to determine if an open dropdown menu is within the user's view or has moved out of sight. I've been experimenting with the getBoundingClientRect() method to compare coordinates and viewport dimensions, but it seem ...

Displaying an HTML/CSS image layered on top of another image with a fixed navigation bar

I've run into an issue with my webpage layout. I have a fixed navigation bar and an image displayed, but now I want to add another image on top of the existing one. However, when I use "position: relative" and position:absolute", the two images end up ...