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

Attempting to emulate the grid showcase using flexbox styling

Creating this layout using CSS Grid was a breeze. However, I wonder if it can be achieved with Flexbox. What do you think? .Message { display: inline-grid; grid-template-areas: ". name""date text"; } .Date { align-items: ...

Ways to conceal a text-filled div behind an image using 3D transformation

I've been working on creating three images with a fun 'flipcard' effect for the past day and a half, and I believe I'm getting closer to achieving my goal. However, there is one issue remaining, as you can see in the codepen. When the ...

Navigate through the contents of a table featuring intricate colspan and rowspan elements, while keeping both headers and left columns in

I am facing a challenge with a dynamically generated table that has complex structure including colspans and rowspans. My goal is to fix the headers and, if possible, even lock the first few rows on the left side while allowing the content of the table to ...

How can I prevent clearQueue() from removing future queues?

In my code, I have a button that triggers the showing of a div in 500ms when clicked. After the div is shown, a shake class is added to it after another 500ms. The shake class is then removed after 2 seconds using the delay function. However, if the user c ...

Loading an empty CSS file in Node.js

Just starting out with node.js, and I could really use some help with a small css and image issue that I'm facing. I've streamlined my html and .js for clarity. Despite trying everything, the css and image just won't load. My form and server ...

MySQL unable to access information entered into form

After creating a new log in / register template using CSS3 and HTML, I now have a more visually appealing form compared to the basic one I had before. To achieve this look, I referenced the following tutorial: http://www.script-tutorials.com/css 3-modal-po ...

Building adaptable divs using bootstrap technology

I'm playing around with bootstrap and here is the code I came up with: <!doctype html> <html lang="en"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet ...

Twitter Bootstrap navbar-collapse can be seen overlapping navbar-header section, causing a

I seem to be having a problem with the Twitter Bootstrap navbar-collapse element overlapping the navbar-header logo, 'the alpha', and causing the link to be unclickable. As shown in the image below, the alpha logo has an href link but is not cli ...

What is the best way to add several icons at once?

Is there a way to insert multiple star icons directly below the review text? I attempted to use pseudo elements to add the icons, but I could only insert one icon when I actually need to include multiple icons. Here is what I have tried so far: .review:: ...

I am currently facing an issue with validating forms in JavaScript Document Object Model (DOM)

Whenever I click on a field and move to another one, the span tag turns red. Then, after pressing the submit button, an alert message pops up. However, if I turn the span red, fill in the field, and press the submit button, it shows success even if other f ...

Utilizing jQuery to implement a CSS style with a fading effect, such as FadeIn()

I have a jQuery script that applies a CSS style to an HTML table row when the user clicks on a row link: $(this).closest('tr').css("background-color", "silver"); Is there a way to soften this color change effect, such as by gradually fading in ...

Any tips for creating a website with non-redirecting tabs?

I'm currently working on a website project and I would like to incorporate tabs on the side of my site for easy navigation. I envision an interactive tab system where users can click on their desired category and be directed there instantly, rather th ...

Alter the color of the span text without affecting the :before or :after elements

Can you change the text color of a span without affecting its :before and :after colors? Is it possible to modify the color of the main text in a span element without altering the colors of the elements that come before and after it? Here is an example o ...

After the automation is finished, I am interested in outputting the IMDB rating of a movie or series to the terminal

I prefer using Google search to find the element because I find it easier to navigate compared to IMDB. import selenium.webdriver as webdriver print("This script is designed to retrieve the IMDb rating of a movie or TV series!!!") def get_results(search_ ...

Combining the value of $(this) to create an identifier name

I am attempting to create a hover effect on an h1 element that triggers the glowing effect on a span element with an id that corresponds to the value of the h1. While I have successfully set up a glowing effect for a sentence, I am struggling to replicate ...

Is it possible to utilize props within a computed property in order to apply a CSS class binding?

Can props be utilized within a computed property in an alternative manner? Upon attempting this, it seems to add the class 'foo' (the props name) instead of the intended 'fa-foo' (or a different value if props were configured). If I d ...

Guide to dynamically setting SCSS $variables in JavaScript after retrieving them from local storage in a React application

In my current situation, I am retrieving color combinations in hash values from the database through an API call and then saving them in localStorage for future use. However, I am facing a challenge when trying to access this data from localStorage and uti ...

What is the best method for creating a fade effect on a div background?

I am trying to animate a div with the id #test from a 0 opacity background to an opacity of 0.7 using CSS rgba values, but for some reason, the .animate function is not working as expected. Here is my CSS: #test { background-color: rgba(0, 0, 0, 0); ...

Why is a div nested inside an overflow: hidden; div not expanding to the full width of its content?

.container { white-space: nowrap; overflow: hidden; } .inner { width: 100%; } .list-item { display: 'inline-block', boxSizing: 'border-box', border: '3px solid black' } import React, { Component } from 're ...

Icon-enhanced Bootstrap dropdown selection

I have a unique select dropdown using Bootstrap where I want to display icons like the example below: https://i.sstatic.net/dZmTS.png <!DOCTYPE html> <html> <head> <script src="/scripts/snippet-javascript-console.min.js?v=1"& ...