CSS transformation on the go

Can anyone help me? I am trying to create an animation for a hamburger menu when checked and unchecked. I have managed to animate the menu, but I'm struggling with animating the left menu when transforming it to 0.

&__menu {
   transform: translateX(-100%);
   transition: 1s ease-in-out;
// .c-hamburger--icon ~ &{
//     transition: all 300ms;
// }
   .c-hamburger--icon:checked ~ &{
     height: 100vh;
     background: #000;
     width: 270px;
     transform: translateX(0);

     top: 0;
     opacity: .7;
     position: fixed;
   }
}

You can view the demo on CodePen.

Answer №1

Implementing the height and width properties in the default state of the element is crucial rather than applying them only after checking the menu. The default values for height and width are both set as auto, and transitioning from or to auto directly is not supported, resulting in an abrupt hiding instead of a smooth transition.

Even when utilizing transform: translateX(-100%) to initially conceal the menu, setting default height and width will not affect the layout due to how CSS rules cascade. Below is a demo showcasing the compiled CSS code.

&__menu {
   position: fixed;
   top: 0;
   height: 100vh;
   width: 270px;
   transform: translateX(-100%);
   transition: 1s ease-in-out;
   .c-hamburger--icon:checked ~ &{
     background: #000;
     transform: translateX(0);
     opacity: .7;
   }
}

Another important consideration is that the position property should be defined within the default state itself, as it cannot be transitioned over time. This helps maintain consistency in the positioning of elements throughout interaction states.

@import url("https://fonts.googleapis.com/css?family=Titillium+Web");
 * {
  padding: 0;
  margin: 0;
  font-size: medium;
  font-family: 'Titillium Web', sans-serif;
}
.l-app__menu {
  position: fixed;
  top: 0;
  height: 100vh;
  width: 270px;
  transform: translateX(-100%);
  transition: 1s ease-in-out;
}
.c-hamburger--icon:checked ~ .l-app__menu {
  transform: translateX(0);
  background: #000;
  opacity: .7;
}
.l-app__left {
  float: left;
  position: fixed;
  background: #185a9d;
  width: 50%;
  height: 100%;
  overflow: hidden;
}
@media (max-width: 1200px) {
  .l-app__left {
    position: static;
    width: 100%;
    height: 100vh;
    background: #fff;
  }
}
.l-app__right {
  float: right;
  background: #fff;
  width: 50%;
  height: 100vh;
}
@media (max-width: 1200px) {
  .l-app__right {
    position: static;
    width: 100%;
    background: #bfbfbf;
  }
}
.l-app__right--inner {
  padding: 50px;
}
.l-app__hamburger {
  position: fixed;
  z-index: 1;
}
.c-bike__image {
  background: url(http://bikeshopgirl.com/wp-content/uploads/2011/10/15038.jpg) no-repeat;
  background-size: contain;
  min-height: 100%;
  opacity: .9;
  top: 0;
  position: relative;
}
@media (max-width: 1200px) {
  .c-bike__image {
    position: static;
    width: auto;
    opacity: 1;
  }
}
.c-bike__header {
  font-size: 150%;
  padding: 15px;
}
@media (max-width: 1200px) {
  .c-bike__header {
    padding: 0;
  }
}
.c-bike__article {
  letter-spacing: .3px;
  padding: 10px;
}
.c-bike__article.c-bike__header {
  font-size: 120%;
  padding: 0;
}
.c-hamburger {
  background: transparent;
  display: block;
  position: relative;
  overflow: hidden;
  margin: 0;
  padding: 0;
  width: 96px;
  height: 96px;
  font-size: 0;
  text-indent: -9999px;
  appearance: none;
  box-shadow: none;
  border-radius: none;
  border: none;
  cursor: pointer;
  transition: background .3s;
}
.c-hamburger:focus {
  outline: none;
}
.c-hamburger--icon {
  display: none;
}
.c-hamburger--icon ~ .c-hamburger--htx {
  transition: transform 1s;
}
.c-hamburger--icon:checked ~ .c-hamburger--htx {
  transform: translate(250px, 0) rotate(90deg);
  transition: 1s ease-in-out;
}
.c-hamburger--icon:checked ~ .c-hamburger--htx .c-hamburger__span {
  background: none;
}
.c-hamburger--icon:checked ~ .c-hamburger--htx .c-hamburger__span::before {
  top: 0;
  transform: rotate(45deg);
}
.c-hamburger--icon:checked ~ .c-hamburger--htx .c-hamburger__span::after {
  bottom: 0;
  transform: rotate(-45deg);
}
.c-hamburger--htx__span {
  transition: transform .5s;
}
.c-hamburger--htx__span::before {
  transition-property: top, transform;
}
.c-hamburger--htx__span:after {
  transition-property: bottom, transform;
}
.c-hamburger__span {
  display: block;
  position: absolute;
  top: 44px;
  left: 18px;
  right: 18px;
  height: 8px;
  background: #930202;
  border-radius: 5px;
}
.c-hamburger__span::before,
.c-hamburger__span::after {
  position: absolute;
  display: block;
  left: 0;
  width: 100%;
  height: 8px;
  background: #930202;
  border-radius: 5px;
  content: "";
}
.c-hamburger__span::before {
  top: -20px;
}
.c-hamburger__span::after {
  bottom: -20px;
}
<div class="l-app">
  <div class="l-app__hamburger">
    <input id="c-hamburger--icon" type="checkbox" name="c-hamburger" class="c-hamburger--icon" />
    <label for="c-hamburger--icon" class="c-hamburger c-hamburger--htx">
      <span class="c-hamburger__span"></span>
    </label>
    <nav class="l-app__menu"></nav>
  </div>
  <div class="l-app__left">
    <div class="l-app__left--inner c-bike__image"></div>
  </div>
  <div class="l-app__right">
    <div class="l-app__right--inner">
      <section class="c-bike">
        <header>
          <h3 class="c-bike__header">Bike name</h3>
        </header>
        <article class="c-bike__article">
          <header>
            <h4 class="c-bike__article c-bike__header">Secion name</h4>
          </header>
          <p class="c-bike__article c-bike__paragraph">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce quis diam egestas, dictum augue ut, dignissim lorem. Integer eros felis, sodales at viverra et, ultricies malesuada lacus. Nullam ex orci, vulputate vitae porta eu, gravida vel arcu. Curabitur
            mattis quis dolor sit amet dapibus. Etiam mattis diam id rhoncus tempor. Phasellus tristique auctor luctus. Nulla ullamcorper tempus porttitor. Sed et tincidunt sem. Morbi dictum ut orci sit amet pretium. Integer feugiat enim vitae neque commodo,
            ac malesuada lacus scelerisque. Donec quis odio in ipsum facilisis auctor. Vestibulum turpis turpis, blandit sit amet tempus sed, facilisis nec tellus. Morbi elementum vulputate sem a rhoncus. Vivamus bibendum congue velit, ac hendrerit est
            suscipit ac. Nunc a molestie libero.
          </p>
        </article>
      </section>
    </div>
  </div>
</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

The bold horizontal line in HTML tables is a bit unusual and is caused by the border-collapse property

Edit 1: The strange issue appears to be related to the border-collapse property in CSS, as it can be resolved by using border-spacing: 0px. However, the question remains, why does border-collapse result in this behavior? It seems to be connected to scaling ...

How can you implement CSS styling for Next.js HTML during server-side rendering?

Explore Next.js and observe the page request in the network tab. The preview displays not only the HTML but a fully pre-styled page as well. https://i.stack.imgur.com/deJLY.png When working with Styled-Components and Material-UI, they offer the ServerSty ...

What is the default margin for Autocomplete in Material UI?

Having just started with material-ui, I'm having trouble figuring out the margins of the Autocomplete. My form includes both Autocomplete and TextInput elements, but their positioning seems off to me. Is there some predefined margin box around Autocom ...

The presence of concealed cells within an HTML table is leading to an increase in the

I am currently working on an HTML Table that serves as a summary table for displaying a list of items. The table has 4 visible columns out of approximately 20, with the remaining columns set to display: none;. However, the hidden cells are causing the rows ...

Generate a dynamic vertical line that glides smoothly over the webpage, gradually shifting from one end to the other within a set duration using javascript

I have designed a calendar timeline using html. My goal is to implement a vertical line that moves from left to right as time progresses, overlaying all other html elements. This functionality is similar to Google Calendar's way of indicating the curr ...

What are the dimensions for maximum picture width and height in the Bootstrap 4 grid class col-4? Additionally, can you provide some tips on minimizing image bl

Hey there! I am trying to arrange 3 screenshots in 3 separate columns in one row, resembling steps 1, 2, and 3. I want them all to have the same size. Can you advise on the maximum width and height I should use before they start getting stretched or comp ...

What methods can I use to make sure the right side of my React form is properly aligned for a polished appearance?

Trying to create a React component with multiple input field tables, the challenge is aligning the right side of the table correctly. The issue lies in inconsistent alignment of content within the cells leading to disruption in overall layout. Experimente ...

Tips for Preventing Ant Design from Overriding Existing CSS Styles

Currently, I am working on a ReactJS project. Initially, the entire project was designed using pure CSS. However, I decided to incorporate a small Antd component into my project. Following the provided instructions, I imported the component like this in on ...

Having trouble with this code// Does anyone know how to make the div slide in line with other divs when hovering over it?

After spending countless hours analyzing and trying various solutions, I have come to the realization that I need to seek help with my code. The task at hand is proving to be incredibly frustrating, and I have exhausted all other options before resorting t ...

Simultaneously scrolling left and fading in with jQuery

I've come across this code that works well in scrolling my divs to the left. However, I'm looking to add a fading effect while they are scrolling. Is there a way to achieve this? $('div#line1').delay(1000).show("slide", { direction: "r ...

Updating the CSS link href in ASP.NET using code behind

I'm struggling with changing the CSS href in the code-behind of my .ASPX page. I've tried various methods but haven't found a solution that works as intended. HTML Markup: <link id="linkCSS" runat="server" href='/css/1.css' re ...

Tips for Aligning a Collection of Relative Positioned Divs within a Parent Relative Positioned Div

Struggling to dynamically center a group of relative positioned divs within a parent div that is also relative positioned. The parent div has a width of 620px, while the child divs are each 200px wide. There could be 1 to 3 child divs per line, which is w ...

Utilize CSS block display for ::before and ::after pseudo elements within a div container

I have a CSS file that contains the following code snippet: .loader { width: 250px; height: 50px; line-height: 50px; text-align: center; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-fam ...

initiate changes to the application's style from a component nested within the app

I'm looking to update the background color of the entire page, including the app-nav-bar, when I'm inside a child component. When I navigate away from that component, I want the default style to be restored. Any suggestions on how to achieve this ...

Guide to resizing images with next.js

I have an issue with resizing an image within a Bootstrap card using next/image. I am trying to decrease the size of the image to about 60% of its original size and prevent it from filling the entire top portion of the card. I attempted wrapping the imag ...

Is your React application struggling to display large images when bundled with Webpack?

I am facing an issue while trying to display an image from my image folder in a React project using Webpack. I have observed that smaller photos with physically smaller dimensions and file sizes load properly, but larger photos do not render on the screen, ...

Is there a way to implement a css/javascript property specifically for a div that is selected in the url?

Take for instance the scenario where I possess the URL example.com/#foo. In this case, CSS styling will be directed to the div with the id foo. If achieving this solely in CSS is not feasible, what methods in JavaScript or jQuery can be used efficiently ...

Enhance the collapsible feature in Vue.js by integrating Bootstrap and anim

In the process of creating a side bar menu with collapse show/hide functionality, I am encountering some issues. The current CSS implementation is making the collapse action appear abrupt and unnatural. I am looking to achieve a smooth sliding transition ...

Slick.js integrated with 3D flip is automatically flipping after the initial rotation

I'm encountering an issue with my CSS3 carousel and 3D flipping. Whenever I navigate through the carousel and flip to the next slide, the first slide seems to automatically flip/flop after completing the rotation. You can see a visual demonstration o ...

Attempting to conceal image previews while incorporating pagination in Jquery

I'm working on implementing pagination at the bottom of a gallery page, allowing users to navigate between groups of thumbnail images. On this page, users can click on thumbnails on the left to view corresponding slideshows on the right. HTML <di ...