What is the best way to position two flexbox children in the center of a parent div with relative positioning?

I need help creating an animated hamburger menu icon with three lines that form into an X shape when clicked. Currently, I'm struggling to align the first horizontal line and the third one properly.

Does anyone know how to make two flexbox children overlap each other at the center of the parent div?

This is what I have so far:

[].forEach.call(document.querySelectorAll('#hSandwich'), function(el) {
  el.addEventListener('click', function() {
  var ct = document.querySelector('#hSandwich');
    var l1 = document.querySelector('.hSandwich-01');
    var l2 = document.querySelector('.hSandwich-02');
    var l3 = document.querySelector('.hSandwich-03');
l1.classList.toggle('hSandwich-01-x');
    l2.classList.toggle('hSandwich-02-x');
    l3.classList.toggle('hSandwich-03-x');
    ct.classList.toggle('hSandwich-x');
    
  })
})
#hSandwich {
  width:40px;
  height:40px;
  border:1px solid black;
  display:flex;
  align-items:center;
  justify-content:center;
  flex-direction:column;
}

   .hSandwichItem {
    width:60%;
    height:10%;
    border-radius:10px;
    background:grey;
    margin:2px 0 2px 0;
    transition: all 0.2s ease-out;
  }

  .hSandwich-01-x {
    position:relative;
    margin-top:-50%;
    -moz-transform: rotate(45deg);
    -webkit-transform: rotate(45deg);
    -o-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
    transform: rotate(45deg);
  }
  
  .hSandwich-02-x {
    opacity:0;
  }
  
  .hSandwich-03-x {
    position:relative;
    margin-top:-50%;
    -moz-transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
    -o-transform: rotate(-45deg);
    -ms-transform: rotate(-45deg);
    transform: rotate(-45deg);
  }
<div id="hSandwich">
  <div class="hSandwich-01 hSandwichItem"></div>
  <div class="hSandwich-02 hSandwichItem"></div>
  <div class="hSandwich-03 hSandwichItem"></div>
</div>

Thank you for your assistance.

Answer №1

Do you anticipate something like this:

rotate(45deg) will rotate the first box upwards and rotate(-45deg) will rotate the third box downwards, making the menu icon look like this: >. Then simply add a margin-top to the third box to achieve an X shape

[].forEach.call(document.querySelectorAll('#hSandwich'), function(el) {
  el.addEventListener('click', function() {
    var ct = document.querySelector('#hSandwich');
    var l1 = document.querySelector('.hSandwich-01');
    var l2 = document.querySelector('.hSandwich-02');
    var l3 = document.querySelector('.hSandwich-03');
    
    l1.classList.toggle('hSandwich-01-x');
    l2.classList.toggle('hSandwich-02-x');
    l3.classList.toggle('hSandwich-03-x');
    ct.classList.toggle('hSandwich-x');
    
  })
})
#hSandwich {
  width:40px;
  height:40px;
  border:1px solid black;
  display:flex;
  align-items:center;
  justify-content:center;
  flex-direction:column;
}

.hSandwichItem {
  width:60%;
  height:10%;
  border-radius:10px;
  background:grey;
  margin:2px 0 2px 0;
  transition: all 0.2s ease-out;
}

.hSandwich-01-x {
  position: relative;
  -moz-transform: rotate(45deg);
  -webkit-transform: rotate(45deg);
  -o-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
}

.hSandwich-02-x {
  opacity:0;
}

.hSandwich-03-x {
  position: relative;
  margin-top: -34%;
  -moz-transform: rotate(-45deg);
  -webkit-transform: rotate(-45deg);
  -o-transform: rotate(-45deg);
  -ms-transform: rotate(-45deg);
  transform: rotate(-45deg);
}
<div id="hSandwich">
  <div class="hSandwich-01 hSandwichItem"></div>
  <div class="hSandwich-02 hSandwichItem"></div>
  <div class="hSandwich-03 hSandwichItem"></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

Identifying shifts in offsetTop

In my design, I have a page where scroll bars are not allowed, meaning all content must be visible at once. The image below shows the basic layout of the screen to give you an idea. The layout consists of two blocks - Block 1 and Block 2. Block 1 is expan ...

Is there a more efficient method for implementing server side rendering in a Next.js application?

Currently, I am implementing server-side rendering in my Next.js application. This specific component is responsible for returning HTML content. I'm wondering if there are more efficient methods available? import Feature from 'components/home/Fea ...

Saving modified input data for submission using Vue

Objective: The main goal is to have a form interface loaded with an object's current data for editing. This allows the user to open a modal with the form already filled out with the existing information, giving them the option to either edit it or lea ...

Centering images of varying sizes on Bootstrap cards

My task involves incorporating a list of organization logos and links inside cards using HTML code. However, every time I add a logo, the card size changes in accordance with the logo's size, and the image is not centered within the card. I attempte ...

Interactive menu backdrop determined by div element

I am working on a website with a menu structured like the following: Home -- Work -- Pricing -- Contact Us -- About This website uses a one-page layout and has enabled Scrollspy on Bootstrap. I need to make the active menu background dynamic depending o ...

Implementing Bootstrap Tags Input for a fresh start!

Currently, I am using Bootstrap Tags Input. When I press "ENTER," it automatically converts the text into a tag. However, I would like to be able to combine free text with tags without the "ENTER" key converting the text. Is there a way to achieve this? & ...

"Encountering issues when trying to POST data from an Ember component to an

When attempting to post from a component in my Ember app to my Express API, I am encountering the following error: SyntaxError: Unexpected token V in JSON at position 0 Upon inspecting the response in the network inspector on Chrome, I found this error m ...

javascript how to retrieve the custom attribute value of an HTML style

I am dealing with an HTML element that has some inline styles as well as a custom CSS property. I am able to access every style attribute except for my custom style property. Here is the code I am working with: <img id="myimg" class="ImgClass" style=" ...

Encountering spacing problems on IE9 and FF11 while using OS X

After conducting some testing on a design I coded, I found that it functions well in FF 13, Chrome 21, IE 7/8, and Opera 11.62 for Windows, as well as Safari 5.1 for OS X. Unfortunately, since I only have WinXP, I had to utilize Adobe Browserlab to test f ...

Unable to modify the focus outline for .custom-control-input within Bootstrap 4

After spending countless hours attempting to change the focus outline color of the custom bootstrap controls, I have hit a roadblock. Changing the background is a breeze with: .custom-control-input:checked~.custom-control-indicator { background-color: re ...

Build a Search Suggestions feature with Node Package Manager (NPM) and Model-View-Controller (M

Stepping into the exciting world of MVC core and harnessing NPM for JavaScript packages has been a learning curve. However, I've encountered an issue that requires some deliberation on the best course of action for resolution. To provide context, I ha ...

Utilizing the powerful combination of AngularJS and the jQuery Uniform plugin

Utilizing an AngularJS directive to incorporate the Uniform jQuery plugin with angularjs: myApp.directive('pluginUniform', function() { return { restrict: 'A', link: function(scope, element, attrs) { ele ...

How can we call a function in HTML from an external JavaScript file?

I am attempting to utilize a function that I have defined in my .js file within my HTML document. js/newsalerts.js function decodeHTML(html) { //https://stackoverflow.com/a/2989105/4650297 var tempDiv = document.createElement("DIV"); tempDiv. ...

The `col-md-4` element from Bootstrap 4 is not functioning properly within my ejs file

<div class="container"> <div class="row card-deck"> <% for(var i=0;i<campgrounds.length;i++){ %> <div class="card col-12 col-md-4"> <img class="card-image-top" src="<%= campgrounds[i].image %>" ...

Tips for applying a CSS class with a smooth fade-out effect

In my Angular project, I am working on creating a custom notification component. Here is the template code I have so far: <aside *ngFor="let message of messages "> <div class="notification" style="position:fixed"> {{message.conten ...

Attempting to showcase the data stored within MongoDB documents on my website's user interface

I am facing difficulties in showing the data stored in my database on the front end of my grocery list app, which is built using the MERN stack. I have a form that successfully sends data to MongoDB and saves it upon submission. In order to retrieve the d ...

Align two text elements of varying heights at the center of a container

I need to style a fixed-height container with a width of 100% that is set to display:block. Inside this container, there are two text elements styled differently in terms of font-size, and floated left and right respectively. Here's the HTML structu ...

What is the best way to design a dynamic gallery that showcases three columns for large screens, but switches to two columns for mobile devices?

My goal is to create an image gallery using Bootstrap, with 3 columns on large screens and 2 columns on mobile. I want to maintain the aspect ratio of each image while fixing the width so they align within the columns. I was able to achieve this on large s ...

Viewing the scroll overflow element without needing to scroll it into view

I am encountering an issue with a child element styled using overflow-y: auto. Inside this element, there is a list of other objects. Occasionally, I need to scroll this list to a specific position and have been utilizing item.scrollIntoView() for this pur ...

What is the best way to conceal a Material UI button with CSS?

For my TODO react app, I am using a Material UI button. My goal is to create a form with an input field and a submit button, but I want the submit button to be invisible so that users think the form submits when they press the "Return" key. import { Tex ...