What is the most straightforward method for implementing CSS transitions with fit-content for width?

Can you help me figure out how to smoothly transition the width of an element with the style attributes transition: 0.5s and width: fit-content? Specifically, I want to apply this transition to the entire element, not just the padding. Here is the Codepen for reference.

I would appreciate a simple and straightforward solution!

window.onload = function() {

    function openMenu() {
        document.getElementById("sideMenuID").style.width = "50vw";
    }

    function closeMenu() {
        document.getElementById("sideMenuID").style.width = "0";
    }
}
body {
  font-family: "Helvetica", sans-serif;
}

.sideMenu {
  white-space: nowrap;
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #000000;
  overflow-x: hidden;
  padding-top: 60px;
  transition: 0.5s;
}

.sideMenu a {
  padding: 8px 8px 16px 36px;
  text-decoration: none;
  font-weight: bold;
  color: #ffffff;
  display: block;
  transition: 0.3s;
}

.sideMenu button {
  border: none;
  padding: 0px 0px 0px 0px;
  text-decoration: none;
  font-size: 18pt;
  font-weight: bold;
  color: #ffffff;
  background-color: inherit;
  display: block;
  transition: 0.3s;
  cursor: pointer;
}

.sideMenu a:hover button:hover {
  color: #b8b8b8;
}

.sideMenu .closebtn {
  float: right;
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

.openSideMenuIcon {
    position: fixed;
    z-index: 2;
    color: #b8b8b8;
}

@media screen and (max-height: 450px) {
  .sideMenu {padding-top: 15px;}
  .sideMenu a {font-size: 18px;}
}
<html>
  <head>
    <title>Summer Vacation Countdown!</title>
    <meta name="description" content="A summer vacation countdown made by Austin Charbonneau, a freshman." />
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="style.css"></link>
  </head>
  <body id="body">
    <script>
        function openMenu() {
                document.getElementById("sideMenuID").style.width = "fit-content";
            document.getElementById("sideMenuID").style.paddingRight = "7vw";
        }

        function closeMenu() {
            document.getElementById("sideMenuID").style.width = "0";
            document.getElementById("sideMenuID").style.paddingRight = "0px";
        }
    </script>
    <div id="sideMenuID" class="sideMenu">
        <a href="javascript:void(0)" class="closebtn" onclick="closeMenu()">&times;</a>
        <a><button onclick="openTimeSetMenu()" id="setTimeMenuOpenButton">Set Closing Time</button></a>
        <a><button onclick="" id="contactSuggestions">Contact/Suggestions</button></a>
    </div>
    <span style="position: absolute;font-size:50px;cursor:pointer;color: #b8b8b8;z-index: 0;" onclick="openMenu()">&#9776;</span>
  </body>
</html>

Answer №1

To create a smooth animation effect, you need to adjust the width of #sideMenuID. Here's a functional demonstration:

window.onload = function() {

    function openMenu() {
        document.getElementById("sideMenuID").style.width = "50vw";
    }

    function closeMenu() {
        document.getElementById("sideMenuID").style.width = "1px";
    }
}
body {
  font-family: "Helvetica", sans-serif;
}

.sideMenu {
  white-space: nowrap;
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #000000;
  overflow-x: hidden;
  padding-top: 60px;
  transition: 0.5s;
}

.sideMenu a {
  padding: 8px 8px 16px 36px;
  text-decoration: none;
  font-weight: bold;
  color: #ffffff;
  display: block;
  transition: all .3s;
}

#sideMenuID {
  transition: all 1s;
}

.sideMenu button {
  border: none;
  padding: 0px 0px 0px 0px;
  text-decoration: none;
  font-size: 18pt;
  font-weight: bold;
  color: #ffffff;
  background-color: inherit;
  display: block;
  transition: 0.3s;
  cursor: pointer;
}

.sideMenu a:hover button:hover {
  color: #b8b8b8;
}

.sideMenu .closebtn {
  float: right;
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

.openSideMenuIcon {
    position: fixed;
    z-index: 2;
    color: #b8b8b8;
}

@media screen and (max-height: 450px) {
  .sideMenu {padding-top: 15px;}
  .sideMenu a {font-size: 18px;}
}
<html>
  <head>
    <title>Summer Vacation Countdown!</title>
    <meta name="description" content="A summer vacation countdown made by Austin Charbonneau, a freshman." />
    <script type="text/javascript" src="script.js"></script>
    <link rel="stylesheet" href="style.css"></link>
  </head>
  <body id="body">
    <script>
        function openMenu() {
                document.getElementById("sideMenuID").style.width = "400px";
            document.getElementById("sideMenuID").style.paddingRight = "7vw";
        }

        function closeMenu() {
            document.getElementById("sideMenuID").style.width = "0px";
            document.getElementById("sideMenuID").style.paddingRight = "0px";
        }
    </script>
    <div id="sideMenuID" class="sideMenu">
        <a href="javascript:void(0)" class="closebtn" onclick="closeMenu()">&times;</a>
        <a><button onclick="openTimeSetMenu()" id="setTimeMenuOpenButton">Set Closing Time</button></a>
        <a><button onclick="" id="contactSuggestions">Contact/Suggestions</button></a>
    </div>
    <span style="position: absolute;font-size:50px;cursor:pointer;color: #b8b8b8;z-index: 0;" onclick="openMenu()">&#9776;</span>
  </body>
</html>

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 mobile site is extending beyond the width of the device screen

After creating this single web page and testing it on my mobile device, I noticed an issue. When I swipe to the left, the website's width shifts slightly, as if the edges are not fully contained within the screen. This problem only occurs on Safari, n ...

Establish the minimum and maximum values for the text field depending on the selection made in the previous dropdown menu

For my project, I need to create a dropdown menu and a text box positioned next to it. I want the text box to have specific character limits based on the option selected from the dropdown. For example, if "Option 1" is chosen, then the textbox should only ...

What is the best way to relocate a form to the top of the page

I am creating a website to enhance my HTML skills, and I encountered an issue with the sign-in form. Whenever I try to place an image next to the form, it automatically moves to the bottom. I have attempted using padding-top and padding-bottom but to no av ...

What precautions should I take to safeguard my HTML/CSS/JS projects when sharing a link with my employer?

Picture this scenario: you need to share a link for your work, but you want to protect it from being easily copied or developed further by someone else. However, since it's a document, any browser can still view it. Can anyone recommend a tool that wi ...

"Encountering a Javascript issue while trying to apply a CSS class to a

Encountering issues in Safari desktop / mobile and Internet Explorer. The error message states: In Safari: TypeError: Attempted to assign to readonly property. IE Edge: Assignment to read-only properties is not allowed in strict mode The problem arises ...

Is there a way to successfully submit multiple locations, each separated by commas, through the multipart form?

Here is my HTML form: <form method="POST" enctype="multipart/form-data" v-on:submit.prevent="handelSubmit($event);"> <div class="clear"> <div class="col-md-3"></div> <div class="col-md-6"> <div class="form ...

Give GetElementsByClassName a shot

Hey, have you tried using js ref_doc_getelementsbyClassName? "I keep getting the error message 'Uncaught TypeError: Cannot set property 'value' of null' " Check out this HTML code snippet: <input type="text" cla ...

Discovering the exact distance between a 'li' and a 'div' element

Currently, I am in the process of converting HTML to JSON. Here is an example of the HTML I am working with: <div class="treeprofit" id="divTreeViewIncomeDetails" style="height: auto;"> <li><span class="fa fa-folder-open highlight" ...

Pictures on aspx websites utilizing a Master Page

On my master page, I have two images - a logo at the top and a Facebook link at the bottom. There is also an admin folder containing .aspx pages specifically for admin logins. For all non-admin pages, the images are accessed using the src of image/logo.jp ...

Optimal method for arranging Vue components

When deciding where to position a child element, should it be done within its own CSS scope or from the parent? In the scenario provided below, would it be more effective to use margin-left: auto; on the parent element or the child element (both options a ...

Utilizing CSS3 Columns to control the flow of elements and enforce breaks

Struggling to articulate my question, but I'll give it a shot :D Let's talk about the images: I'm having trouble explaining, I want to align the elements like in the first image, but all I'm getting is the second and third pictures. C ...

the table image does not resize correctly in mobile view

I need to resize an image inside a table's <td> when the screen size is in mobile mode. Although I have already added a viewport, my mobile query works correctly for other purposes except for the image. Here is the HTML code: <table class="t ...

What is the best way to ensure a div occupies the full height within a grid layout?

https://i.sstatic.net/awwxE.png Looking at the image provided, I am seeking a way to make the top left image occupy the entire height just like the neighboring div in the first row of the grid. Is there a way to achieve this? The desired outcome should b ...

Is your URL getting cut off in jQuery?

How can I properly display a URL in an HTML table without it getting truncated? I'm attempting to show it within a table using jQuery, but it seems to be cutting off the URL. Here's a snippet of my code. Any suggestions on how to fix this? <! ...

Ways to trigger Bootstrap modal through a PHP GET call

I have been searching for a solution to create a modal similar to this one, but I haven't found anything yet. My requirement is that when we pass true in a GET request, the modal should be displayed. Otherwise, if there is no value specified in the $ ...

Get a Blob as a PNG File

Hope you had a wonderful Christmas holiday! Just to clarify, I am new to JS and may make some unconventional attempts in trying to download my Blob in PNG format. I am facing an issue with exporting all the visual content of a DIV in either PDF or image ...

Filter dates within a range using two input fields in AngularJS

I am trying to use two input fields to display data within a specific range of dates using the AngularJS date filter. (Range filter for AngularJS dates using two input fields) Check out my code below: var app = angular.module('app',[]).control ...

Subscribe on Footer triggers automatic scrolling to the bottom of the page

Whenever I fill out the form in the footer and hit submit, it directs me to a different page while automatically scrolling back down to the footer. I'm looking for a solution that prevents this automatic scrolling. I've attempted using window.sc ...

Restrict User File Uploads in PHP

I have a system set up that enables users to upload files under 200 MB. Once the file is downloaded once, it will be automatically deleted. Additionally, all files are deleted from the server after 24 hours. I am looking for a way to limit the number of up ...

React, Axios, and the PokeAPI are like a team of explorers navigating an

Trying to enhance my React abilities, I decided to follow this tutorial on creating a list of names using PokeAPI. However, I hit a roadblock at 11.35 into the tutorial while implementing the provided code snippets in both App.js and PokemonList.js: fu ...