Closing menu smoothly with CSS or JavaScript transition

I've set up a sidebar div and a content div to function as two CSS grid columns. The sidebar itself consists of two additional columns, but here's the basic structure:

<div class='site-wrap'>
    <div class='sidebar'>
        <div class='navbar'></div>
        <div class='menu'>
            <ul>
                <li>menu item</li>
            </ul>
        </div>
    </div>
    <div class='content>
        <h1>Hello world</h1></div>
    </div>
</div>

The specific CSS details are not crucial, but this is how the sidebar functions:

.site-wrap {
    display: grid;
    grid-template-columns: 320px 1fr;
}

.sidebar {
    display: grid;
    grid-template-columns: 64px 1fr;
    height: 100vh
}

You can see a working version in this JS Fiddle link: https://jsfiddle.net/z4mLtwoy/

Currently, when I close the menu, I adjust the css using grid-template-columns: 72px 1fr on the site-wrap. It works fine, but I would like to incorporate a transition effect. Since CSS grid doesn't support transitions yet, do you know of a CSS (possibly flexbox) or JS method that could provide a transition?

Answer №1

To create a smooth animation effect for the width or max-width property of the menu element, you can leverage CSS transitions. Instead of using grids, consider utilizing a flexbox layout to arrange elements in columns.

body {
  margin: 0;
  padding: 0;
}

.site-wrap {
  display: flex;
  height: 100vh;
}

.sidebar {
  display: flex;
}

.site-wrap .menu {
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  box-sizing: border-box;
  height: 100vh; 
  margin: 0;
  overflow: hidden;
  padding: 0;
  transition: max-width 0.5s ease;
  max-width: 100px;
}

.site-wrap .navbar {
  background: green;
  height: 100vh;
  width: 64px;
}

.menu li {
  padding: 16px;
  white-space: nowrap;
}

.content {
  background: #EFEFEF;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  flex-grow: 1;
  padding: 16px;
}

.banner {
  background: lightblue;
  box-sizing: border-box;
  padding: 16px;
  height: 64px;
  width: 100%;
}

#trigger {
  display: none;
  position: fixed;  
}

.button {
  background: blue;
  color: white;
  cursor: pointer;
  margin: 8px;
  padding: 8px 16px;  
}

#trigger:checked ~ .site-wrap .menu {
  max-width: 0px;
}
<input id='trigger' type='checkbox'>
<div class='banner'>
  <label for='trigger' class='button'>Click me</label>
</div>
<div class='site-wrap'>
  <div class='sidebar'>
    <div class='navbar'></div>
    <ul class='menu'>
      <li>Item 1</li>
      <li>Item 2</li>
      <li>Item 3</li>
      <li>Item 4</li>
    </ul>
  </div>
  <div class='content'>
    <h2>Hello world</h2>
  </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

AngularJS grid designed to emulate the functionalities of a spreadsheet

I have been facing challenges with Angular while attempting to recreate a spreadsheet layout in HTML using ng-repeat. Despite extensive research, I have not found a solution. My goal is to display data in a table format as shown below: <table> & ...

Is there a way to manually trigger a re-render of all React components on a page generated using array.map?

Within my parent component (Game), I am rendering child components (Card) from an array. Additionally, there is a Menu component that triggers a callback to Game in order to change its state. When switching levels (via a button click on the Menu), I want a ...

the function does not wait for the DOM content to finish loading

As I execute the code, an error occurs stating that setting innerText of Null is not allowed. I understand that this is because my function runs before the DOM is completely loaded, but I am unsure of how to resolve this issue. Attempts have been made usi ...

Using 'this' in arrow functions within a class in JSX is not allowed

Error Code import React, {Component} from "react"; import ReactDOM from "react-dom"; class App extends Component { constructor(props) { super(props); this.DemoRenderer = function () { return <div>DemoRenderer</ ...

Configuring the start_url and scope within the manifest.json file for a React Progressive Web App

Can you explain the distinction in manifest.json between using start_url as "." versus "./", and likewise, setting scope as "." versus "./"? I am currently migrating a React app behind a reverse proxy server accessed through https://www.example.com/scorer ...

What is the best way to access a PHP variable from JavaScript in this scenario?

In this scenario, I need my script to run post.php successfully. This is working fine. However, while running the post.php file, a variable should be created in it with the content "1". Afterwards, I would like to retrieve this variable in my JavaScript co ...

Reposition labels below input fields on HubSpot Embedded Form to enhance form layout

In the process of modifying the DOM using jQuery in the HubSpot form embed code, I am attempting to relocate the labels below the inputs in order to achieve the desired CSS styles. <script> hbspt.forms.create({ portalId: "xxxxx" ...

Is the logo stretching when adding an image in a bootstrap row?

Attempting to utilize Bootstrap 4 for website development, I encountered an issue when adding a logo image and text to the header. The image became stretched, as seen below: https://i.sstatic.net/3qRnE.png Below is the HTML code used: <header> &l ...

Is it possible to integrate a GWT library into Dart programming?

Considering migrating to Dart, but unsure of its maturity. Can a library originally written in GWT be used in Dart? ...

What is the process for including a checkbox with a label in Card Media?

I attempted to create this feature using code. However, I encountered an issue where the CardMedia and the checkbox would not align properly, resulting in a lack of responsiveness. <Card> <CardMedia ...

Current selection in a dynamic menu within an Express universe

In my Express.js app, I am working on creating a simple menu. My goal is to add the "active" class to the menu list item of the page that is currently being visited. Here is the code snippet from my Jade view file: li(class = path === "/id/admin" ? "activ ...

Numerous identifiers and a distinct title

As a beginner in Ruby and JavaScript, I am unsure of my actions. I have a dropdown box that I want to display by unique name. Using JavaScript (CoffeeScript), I retrieve results with JSON. I have implemented a method to display by unique name, which is f ...

Choosing the primary camera on a web application with multiple rear cameras using WebRTC

Having a bit of trouble developing a web app that can capture images from the browser's back camera. The challenge lies in identifying which camera is the main one in a multi-camera setup. The issue we're running into is that each manufacturer u ...

Tips on handling a redirection request following a fetch post request

When communicating with my node server (Express) using fetch to build a Single Page Application (SPA), I have encountered an issue. Upon each request to the server, I validate the session and redirect to a login page if it is not valid. However, I noticed ...

Syntax Error Unearthed: Identifier Surprise Discovered in (Javascript, ASP.NET MVC, CSHTML)

I encountered an error when attempting to remove a dynamically created HTML element by clicking the corresponding remove button. The goal is to invoke the remove method at the end of the script and pass along certain arguments. However, I'm facing iss ...

The SVG rendering issue persists in Safari following the recent update

Currently, I am working on a React project that involves implementing a gradient generator for icons. The issue I am facing is specific to Safari browser - while everything works perfectly fine on Chrome, Safari renders the icon normally only initially. Fo ...

It is unable to obtain a video feed from a USB input device through WebRTC (readyState transitions to ended)

I am attempting to utilize WebRTC in order to showcase a video input as a live feed on the screen. My intention is not to engage in any peer-to-peer communication, but solely to display a video feed. The code I have implemented works smoothly with my lapt ...

Fetching image files from Angular JS in a servlet

On my website, users have the ability to upload images. I am using AngularJS to post the data to a specific URL via a POST request. My question is, how can I retrieve this data in a Java servlet? My goal is to save the uploaded image in a database. Is this ...

What is the best method for adding items to a state array using React hooks?

Currently, I have a state initialized as const [filteredProducts, setFilteredProducts] = useState([]); My goal is to append new items to the end of this state. I am attempting to achieve this by iterating through a list of products, like so: products.forE ...

Angular - Error: Cannot read property 'publishLast' of undefined

My goal is to prevent multiple requests from being created when using the async pipe. I am facing an issue with a request to fetch a user from the API: getUser() { this._user = this.http.get<User>(environment.baseAPIUrl + 'user') ...