Tips for efficiently scaling elements using CSS media queries

Is there a way to create a responsive animation with CSS using VW units without compromising the quality of the animation?
Seeking assistance from knowledgeable individuals in this matter.

HTML:

<div class="slidebar" id="slide" >
        <div class="hamburger" id="hamburger-1" onclick="hamburger()">
          <span class="line"></span>
          <span class="line"></span>
          <span class="line"></span>
        </div>
</div>

CSS:

.slidebar{
    position: absolute;
    top: 17px;
    right: 25px;
    z-index: 100;
    transition: 0.4s ease-in-out;
}
.hamburger .line{
    width: 50px;
    height: 5px;
    background-color: black;
    display: block;
    margin: 8px auto;
    -webkit-transition: all 0.3s ease-in-out;
    -o-transition: all 0.3s ease-in-out;
    transition: all 0.3s ease-in-out;
    
}
#hamburger-1.is-active .line:nth-child(2){
    opacity: 0;
  }
.hamburger:hover{
    cursor: pointer;
}
  
#hamburger-1.is-active .line:nth-child(2){
    opacity: 0;
}
  
#hamburger-1.is-active .line:nth-child(1){
    -webkit-transform: translateY(13px) rotate(45deg);
    -ms-transform: translateY(13px) rotate(45deg);
    -o-transform: translateY(13px) rotate(45deg);
    transform: translateY(13px) rotate(45deg);
}
#hamburger-1.is-active .line:nth-child(3){
    -webkit-transform: translateY(-13px) rotate(-45deg);
    -ms-transform: translateY(-13px) rotate(-45deg);
    -o-transform: translateY(-13px) rotate(-45deg);
    transform: translateY(-13px) rotate(-45deg);
}

JS:

function hamburger(){
    var hamburg = document.getElementById('hamburger-1');
    hamburg.classList.toggle("is-active");
    var slidebar = document.getElementById("slide");
    slidebar.classList.toggle("is-active");
}

A JSFiddle has been created for easy access: JSFiddle

Looking for solutions using only JS, CSS, and HTML.

Answer №1

From my understanding of the question, it seems like you are looking to adjust the size of the burger button dynamically. You can achieve this by including the following CSS properties within the .hamburger .line class:

width: 10vw;
max-width: 50px;

Answer №2

To make your website more responsive, I suggest converting all px values to em using this helpful conversion tool. By doing so, the sizes will be relative to the parent element's font-size (e.g., 2em is twice the size of the parent's font).

For further information on CSS units, you can refer here.

.hamburger .line {
    width: 3.125em;
    height: 0.3125em;
    background-color: black;
    display: block;
    margin: 0.5em auto;
    transition: all 0.3s ease-in-out;
}

#hamburger-1.is-active .line:nth-child(2) {
    opacity: 0;
}

.hamburger:hover {
    cursor: pointer;
}

/* More CSS styles for hamburger menu icons */

@media screen and (min-width: 600px) {
    .hamburger {
        font-size: 50px;
    }
}

If desired, you could set the .hamburgers font-size in vw units to avoid using multiple media queries.

https://codesandbox.io/s/sleepy-joliot-ycf99?fontsize=14&hidenavigation=1&theme=dark

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

Tips for storing the output of a script URL in a JavaScript variable

I am currently facing an issue with running a script in the 'head' element of a webpage. The script makes an API call to a specific URL and retrieves results in the form of a JavaScript array. However, I am encountering difficulty because the API ...

Encountering a 404 Error in React Native when using Axios - The request was unsuccessful with status code 404

I am facing an issue while trying to retrieve data from an API in Brazil. The API is returning the following error: Object { "_link": "/v1/campeonatos/2/fases/56", "decisivo": false, "eliminatorio": true, "fase_id": 56, "ida_e_volta": false, " ...

What is the best way to merge two JSON arrays using Axios in a React project?

I am currently working on getting data using axios React from Laravel, but I am facing some challenges in combining two arrays by their respective Ids. In my case, I am trying to retrieve product data and images from the Laravel Controller. Can anyone prov ...

transferring information between two html pages using javascript

Although this question has been raised multiple times, I have gone through the answers and attempted various solutions, however, my code is still not functioning correctly. Below are my working files : my_app -index.html -task1 -index.html I ...

Parameterized query causing JavaScript error

As I struggle with this issue for more than a day now, a scenario unfolds where a user clicks on a link of a book name triggering me to read that book's name. Subsequently, an Ajax request is made to a Jersey resource within which a method in a POJO c ...

The 'authorization' property is not available on the 'Request' object

Here is a code snippet to consider: setContext(async (req, { headers }) => { const token = await getToken(config.resources.gatewayApi.scopes) const completeHeader = { headers: { ...headers, authorization: token ...

Can you specify separate paths in your NPM package for use in the browser versus the server (NodeJS)?

Can you specify separate directories within your NPM package for the browser and NodeJS server? While my code is largely isomorphic, it’s minified and combined for browsers. ...

Switching CSS styles - seeking a smoother integration

I have successfully implemented a JS CSS switcher, which works brilliantly. However, I would like it to function more seamlessly. When opening a new page on the site, the default CSS style sometimes flickers briefly before the selected CSS style is reappli ...

Upon clicking the "show more information" button on the page, JavaScript executes the function to display additional details

<a href="#" class="button">Read More</a><br> I'm currently struggling with programming a button on my website. Specifically, I need the Read More button to display all information related to the CDs I have for sale when clicked. A ...

The pseudo right arrow is failing to display in the vertical center

My pseudo right arrow is not displaying vertically centered within the .nav-link class. Can anyone suggest how I can achieve vertical alignment for my right arrow in an <a> tag? I have already attempted using top:50% but it did not work. Here is th ...

What is the best way to retrieve a document from a collection in a Meteor application?

My experience with mongodb is very limited, so I need help on how to retrieve a document from a meteor collection. I am trying to check if a document exists for the user and update it with an object. if (Saves.find({_id: Meteor.userId()}).fetc ...

What could be causing the jQuery slidedown to malfunction within my table?

My slider is not working when I include a table on my website. The slider works fine without a table, but I need to display members in a table format. Can anyone help me identify the issue with my code? The information needs to be displayed below the nam ...

Sending an array of numbers from JavaScript (using Ajax) to C# - a step-by-step guide

Normally, my typical AJAX call to pass an integer ID to the server side in C# would look like this: $.ajax({ url: '@Url.Action("PlantHistoryContent", "PlantStatus")', data: {id: 1}, async: false, ...

Is there a way for me to immediately send data after receiving it?

When I try to perform onPress={() => kakaoLosing() I am attempting to retrieve data (profile) from getProfile using async await and immediately dispatch that data to KAKAOLOG_IN_REQUEST, This is my current code snippet: import { ...

Setting up a custom PrimeNG theme to match our unique style is a great way to

I am currently using the most recent version of "primeng": "^12.2.0", and I am looking to implement my own custom theme for primeng. Despite searching through numerous blogs, I have yet to find a solution. In an attempt to create my cu ...

Encountering an issue while trying to import the D3.js library into my React.js application

I'm having trouble with properly importing the d3.js libraries in my react.js app. I am using import * as d3 from 'd3' to import everything and store it in the d3 namespace, but I keep getting an error that says - Cannot read property ' ...

Having trouble loading a JSON object into a Datatable using Jquery?

I am currently utilizing DataTables in combination with Jquery. I have a data source in the form of an JSON object that I intend to retrieve via Ajax and showcase within the table. The JSON data is retrieved from the /live/log url and has the following fo ...

Is it possible to create a CSS design where alternating table rows have two different heights?

Looking to design a table where the height of every other row is approximately one-third of the preceding row. I've managed to style this with an inline method, but I'm curious if it's possible to create separate tags for different tr styles ...

Is your angularjs localstorage giving you trouble?

I am encountering an issue with local storage on my webpage. Upon initial visit, I am seeing an outdated value from local storage. However, upon refreshing the page, I am able to access the current value. How can I prevent this error and ensure that I only ...

IntelliSense in VS Code is unable to recognize CSS files

I'm currently in the process of creating a React application, however, I've encountered an issue where importing a .css file doesn't prompt vscode to display the file in the intellisense feature. https://i.sstatic.net/nxhyV.png It's u ...