Creating a dynamic navigation bar in CSS from complete A to Z

As a novice in web development, I came across a Figma design that required me to create a menu similar to this = menu image (shown in the first image below), which then transitions into an X cross menu image (displayed as the second image below):

https://i.sstatic.net/Q6PLkVnZ.png

https://i.sstatic.net/zCfpdJ5n.png

Below is the CSS code that I have implemented. While it successfully transforms the menu to an X shape, the alignment is not quite right. Here are images of my equal menu and cross menu for reference: equal menu image & cross menu image.

header {
  background-color: #000000;
  width: auto;
  height: 114px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  /* font-size: calc(10px + 2vmin); */
  padding: 0px 40px;
}

.logo {
  width: auto;
  height: auto;
}


/* .hamburger {
  width: auto;
}

.line {
  width: 44px;
  border: 2px solid #ffffff;
  border-radius: 10px;
} */

.hamburger {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  /* Adjust gap for better spacing */
  width: 44px;
  height: 44px;
  text-align: center;
}

.line {
  width: 100%;
  height: 4px;
  /* Thickness of the lines */
  text-align: center;
  background-color: #ffffff;
  /* Color of the lines */
  border-radius: 10px;
  transition: all 0.3s ease;
  /* Smooth transition */
  position: relative;
  /* Position relative for absolute positioning of lines */
}

.hamburger.active .line1 {
  transform: rotate(45deg) translate(5px, 5px);
  /* Rotate and move the first line */
}

.hamburger.active .line2 {
  transform: rotate(-45deg) translate(5px, -5px);
  /* Rotate and move the second line */
}
<header>
  <div className="logo">
    <img src={LogoSvg} alt="stan+vision logo" width={ "165px"} height={ "24px"} />
  </div>
  <div role="button" className={`hamburger ${isActive ? "active" : ""}`} onClick={toggleMenu}>
    <div className="line line1"></div>
    <div className="line line2"></div>
  </div>
  <div>
    <button>Theme</button>
  </div>
</header>

Answer №1

If you want to make the rotation occur based on a specific pivot point for your X shape, you can specify the transform origin like this:

.line {
  transform-origin: 9px center;
}

Just remember that in this case, the value of 9px is calculated by adding half of the gap between the lines (14px / 2) with half of their height (4px / 2).

For proper alignment after rotating without any translation, you can define the rotation angles as follows:

.hamburger.active .line1 {
  transform: rotate(45deg);
}

.hamburger.active .line2 {
  transform: rotate(-45deg);
}

(I've converted your ReactJS code into plain HTML for demonstration purposes)

.hamburger {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  /* Adjust gap for better spacing */
  width: 44px;
  height: 44px;
  text-align: center;
}

.line {
  width: 100%;
  height: 4px;
  /* Thickness of the lines */
  text-align: center;
  background-color: #000;
  /* Color of the lines */
  border-radius: 10px;
  transition: all 0.3s ease;
  /* Smooth transition */
  position: relative;
  /* Position relative for absolute positioning of lines */
  
  transform-origin: 9px center;
}

.hamburger:hover .line1 {
  transform: rotate(45deg);
}

.hamburger:hover .line2 {
  transform: rotate(-45deg);
}
<div role="button" class="hamburger">
  <div class="line line1"></div>
  <div class="line line2"></div>
</div>

If you need to shift the entire X shape slightly to the right before applying the rotation, you can include a translateX transformation prior to the rotate operation:

.hamburger.active .line1 {
  transform: translateX(5px) rotate(45deg);
}

.hamburger.active .line2 {
  transform: translateX(5px) rotate(-45deg);
}

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

Resize a group of images to match the parent's width and height dimensions

I am working with a div that contains variously-sized images and is nested inside a parent container. <div id="parentContainer"> <div id="boxToScale"> <img src="http://placehold.it/350x150" /> <img src="http://placehold.it/150 ...

React component for Google reCAPTCHA fails to display after updating state

I'm encountering an issue with my Next.js app that uses react-google-recaptcha. The problem arises in a multi-part form where the view changes as the user progresses through the form. If the user navigates to the view containing the ReCAPTCHA and then ...

Exploring Typescript for Efficient Data Fetching

My main objective is to develop an application that can retrieve relevant data from a mySQL database, parse it properly, and display it on the page. To achieve this, I am leveraging Typescript and React. Here is a breakdown of the issue with the code: I h ...

The variable set in useEffect on a global scope cannot be accessed in other functions

Recently, I've encountered an issue with this code snippet that sends data to my Firebase database upon button click. Strangely, when I try posting without entering any content, everything works fine. However, as soon as I input some data and click th ...

How should a menu be properly utilized in Material UI when combined with an AppBar?

I could use some help with this particular issue. The example in the documentation isn't working for me: https://github.com/mui-org/material-ui/blob/master/docs/src/pages/components/app-bar/MenuAppBar.tsx https://material-ui.com/components/app-bar/# ...

Toggling forms with HTML <select> elements: A step-by-step guide

In the process of creating a web application, I am faced with the task of registering users based on their specific category. To accomplish this, I have incorporated a combo-box where users can indicate their user type. My objective is to display an appro ...

What is the best way to deactivate the onclick event after it has been triggered?

There was an image that triggered the add_cart() JavaScript function when clicked using onclick() <img src="images/add.png" onclick="add_cart()"> The goal is to limit the user to clicking the image only once. Upon the first click, the add_cart func ...

I'm curious, what height would be most suitable for a first impression?

I'm in the process of building a website and I want to ensure that all elements are visible within the div without requiring users to scroll. However, I know that screen resolutions vary between computers. What can I do to address this issue? Is ther ...

How do @material-ui/core and @types/material-ui interact with each other?

Exploring a sample project that utilizes material-ui. Upon inspecting the package.json file, I noticed the inclusion of the following packages: { ... "dependencies": { "@material-ui/core": "^1.4.1", ... }, "devDependencies": { "@types ...

Is it possible to customize the deep elements of ExpansionPanelSummary using styled-components in React?

After digging into the documentation and examples on how to customize Material UI styling with styled-components, I successfully applied styling to the root and "deeper elements" within an ExpansionPanel and ExpansionPanelDetails. However, when attempting ...

The overlay div is not displaying over my main div as intended

Having trouble with my overlay design - I've set up a button that should display an overlay div on top of my main div when clicked, but for some reason it's not appearing. Despite watching tutorials and looking for similar issues, I'm unable ...

Why is it not possible to establish a minimum height of 100% in html?

HTML Code: <!DOCTYPE html> <html> <head></head> <body> <link rel="stylesheet" type="text/css" href="style.css"> <div class="sheet" style="width:80%;max-width:1024px;height:400px;"></div> ...

How can I create an image link with a hover effect using HTML and CSS?

I've streamlined my code to focus on the issue at hand - I have an image that changes when hovered over, but how can I make it so that clicking on the image redirects you to a specific website? http://pastebin.com/h83yi3e3 Here is the code snippet: ...

Positioning the .aspx buttons in a coordinated manner

In my update panel, I have a label, a dropDownList, and two buttons: <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional"> <ContentTemplate> <div id="dropDownList" style="position:relative;" runat=" ...

What is the best way to split an array into four columns and allocate 10 <li> items from the array to each column?

If I have a dynamic array with 40 elements that changes on every render, how can I loop through the array and return 4 groups of elements, each containing 10 items without any repetition? I want to style these objects in the array using a parent flex con ...

Is it feasible to set the width as automatic in this scenario?

Example 1 demonstrates my goal, but it's challenging to set a fixed width for .sub due to the addition of new items. The width needs to adjust accordingly. However, when no width is specified, Example 2 displays the result. Are there any methods to ...

Having trouble incorporating custom CSS into my Rails/Bootstrap project

I’m having trouble figuring out what I’m doing wrong. I’ve added custom files and tried to override the existing ones. application.css.scss @import 'bootstrap-sprockets'; @import 'bootstrap'; /* * This manifest file will be co ...

"Enhance Your Website with Various Hover Effects Using CSS across Multiple Elements

Has anyone experienced trouble with making the hover effect work in my onHover class and applying a display effect to span.box? Any ideas on how to fix this? .onHover { display: block; width: 200px; height: 20px; background: green; } .onHover:ho ...

What strategies can be implemented to decrease the initial loading duration of a YouTube iframe video?

What are some techniques we can use to decrease iframe loading time? Is it possible to implement lazy loading for YouTube iframe videos? I experimented with initially hiding the iframe and displaying an image instead. However, when the user clicks, it req ...

How can I export the styling from vite library mode in a React application?

My Vite React TypeScript application features JSX components, module.scss, and global CSS files. Although when I build it in Library Mode, I end up with separate .js, .d.ts, and .css files. However, once I install it in another application, the styling d ...