A guide on how to align Hamburger CSS in the center, center text, and include a

For a while now, I've been attempting to achieve three specific things with a Hamburger menu, but so far, I've had no luck. This Hamburger menu is responsive, transitioning into an X symbol upon clicking and moving to the right of the page with a smooth transition (although the shortened code doesn't work properly in the Fiddle).

1) My first goal is to vertically center the Hamburger menu within the Nav Bar, with the Menu text positioned next to it, while also ensuring that the text is vertically centered (appearing halfway up the Hamburger). I can manage to vertically center the Hamburger menu, but as soon as I add text, it always aligns with the bottom of the Hamburger lines.

2) Additionally, I'd like to have a border around the entire Hamburger itself, including padding, and then have the text beside it, outside of this border. Despite my efforts, the border seems to only wrap around the actual Hamburger lines, or the entire Hamburger without any padding.

3) Lastly, I'm looking for a way to change the color of both the Hamburger and the text upon hover and click events. So far, I've only managed to change the color of the text but not the Hamburger itself. I've searched high and low but haven't found a solution that works.

Fiddle

jQuery(document).ready(function () {
    jQuery('#toggle-nav').click(function (e) {
        jQuery(this).toggleClass('active');
        jQuery('.menu ul').toggleClass('active');
        jQuery('.hamburgerWrapper').toggleClass('active');

        e.preventDefault();
    });
});
.hamburgerWrapper {
  display: flex;
  flex-grow: 1;
  justify-content: flex-start;
  left: 0;
  position: absolute;
  width: 100%;
  background-color: #3462bc;
  border-bottom: 2px solid #b3c4e6;
  transition: all 600ms ease;
}
.hamburgerWrapper.active {
  left: 60%;
  transition: all 300ms ease;
}
.hamburger {
  list-style-type: none;
}
.hamburger li a {
  text-align: center;
  display: block;
  color: #fff;
  padding: 24px 16px 12px 16px;
  cursor: pointer;
}
#toggle-nav:after {
  padding-left: 50px;
  content: 'Menu';
}
#toggle-nav span,
#toggle-nav span:before,
#toggle-nav span:after {
  cursor: pointer;
  border-radius: 1px;
  height: 5px;
  width: 35px;
  background: white;
  position: absolute;
  display: block;
  content: '';
}
#toggle-nav span:before {
  top: -10px;
}
#toggle-nav span:after {
  bottom: -10px;
}
#toggle-nav span,
#toggle-nav span:before,
#toggle-nav span:after {
  transition: all 500ms ease-in-out;
}
#toggle-nav.active span {
  background-color: transparent;
}
#toggle-nav.active span:before,
#toggle-nav.active span:after {
  top: 0;
}
#toggle-nav.active span:before {
  transform: rotate(45deg);
}
#toggle-nav.active span:after {
  transform: rotate(-45deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hamburgerWrapper">
  <ul class="hamburger">
    <li><a id="toggle-nav" href="#"><span></span></a>
    </li>
  </ul>
</div>

Answer №1

If you're looking to style a hamburger menu icon, one approach is to use a combination of vertical-align: middle; and display: inline-block; for the menu text and icon. When positioning the additional bars of the hamburger menu with absolute positioning, consider using either absolute or relative for the hamburger icon. Another option is to utilize box-shadow to create the hamburger menu.

Here's an example using CSS:

.element {
  width: 100px;
  height: 50px;
  background: green;
  display: inline-block;
  vertical-align: middle;
}
.menu {
  display: inline-block;
  vertical-align: middle;
}
<div class="element"></div>
<div class="menu">Menu</div>

For a different approach, you can wrap the icon in a parent element with padding to adjust its size:

.wrapper {
  padding: 8px 2px;
  border: 1px solid black;
  display: inline-block;
}
.element {
  background: black;
  width: 20px;
  height: 3px;
  box-shadow: 0 -5px 0 0 black, 0 5px 0 0 black;
}
<div class="wrapper">
  <div class="element"></div>
</div>

For more advanced interactions like click effects, consider using JavaScript for added functionality beyond simple styling. However, CSS can also be used to create hover effects:

.wrapper {
  transition: 0.5s ease all;
}
.wrapper:hover {
  background-color: #ccc;
}
.wrapper:hover .hamburger-wrapper {
  border-color: red;
}
.wrapper:hover .hamburger {
  background: green;
  box-shadow: 0 -5px 0 0 green, 0 5px 0 0 green;
}
.wrapper:hover .menu {
  background: yellow;
  color: orange;
}
.menu {
  transition: 0.3s ease all;
  display: inline-block;
  vertical-align: middle;
}
.hamburger-wrapper {
  transition: 0.3s ease all;
  padding: 8px 2px;
  border: 1px solid black;
  display: inline-block;
  vertical-align: middle;
}
.hamburger {
  transition: 0.3s ease all;
  background: black;
  width: 20px;
  height: 3px;
  box-shadow: 0 -5px 0 0 black, 0 5px 0 0 black;
}
<div class="wrapper">
  <div class="hamburger-wrapper">
    <div class="hamburger"></div>
  </div>
  <div class="menu">Menu</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

Assistance needed with selecting a CSS rule

I have a question regarding CSS that seems pretty straightforward. Here is the code in question: <!doctype html> <html amp lang="en-US"> <head> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto+Slab: ...

The Bootstrap 4 column is not floating properly to the right as it is leaving a gap on the

My right sidebar is not floating to the right and has space from the right side. You can view it at this link: <div class="col-md-2 col-lg-2 col-sm-2" id="right-sidebar" style="background-color:orange; float: right; right: 0!important; width: 100%;"& ...

Adding an HTML tag attribute to a Bootstrap 5 popover using the setAttribute() method: A Step-by

Trying to include HTML tags in a popover setAttribute() function within Bootstrap 5, but the tags are displayed as content rather than being applied as attributes. <button type="button" class="btn btn-secondary mx-2" data-bs-containe ...

Iterating through the Bootstrap grid

https://i.sstatic.net/XMIzT.jpg I'm attempting to create a layout similar to the image provided. Currently, I am utilizing WordPress and WooCommerce and aiming to showcase products in this manner. The following HTML code is what I have been working ...

Is there a way to make the Sweetalert2 alert appear just one time?

Here's my question - can sweetalert2 be set to only appear once per page? So that it remembers if it has already shown the alert. Swal.fire({ title: 'Do you want to save the changes?', showDenyButton: true, showCancelButton: true, ...

The concept of CSS inheritance within div elements

I've been researching extensively about the concept of CSS inheritance, but I have come across a puzzling question that remains unanswered. Consider the code snippet below: <!DOCTYPE HTML> <html> <head> <style type="text/css"> ...

Embed a unique custom design sheet within a Facebook iframe

I'm looking to customize the design of my Facebook feed by adding a custom style sheet inside a Facebook iframe. Here's what I have: <iframe width="1000px" height="1000px" frameborder="0" name="f1e348592ed856c" allowtransparency="true" allo ...

Tips on changing the background image when hovering over a list item and a link with CSS

I have been attempting to adjust an image that is nested within a link which is nested inside a list. My initial idea was to use the image as a background picture. The issue arises when I try to position the background picture correctly with respect to th ...

Is it possible to change the inner html to null during an active ajax request?

My goal is to have two separate data.html files inserted into two different HTML files without needing a click event. I want the structure of my data.html files to remain consistent, while allowing the template of my website to change dynamically by callin ...

The Ajax post is only activated on one occasion

Hello there, I am encountering a problem with an ajax function. My goal is to post data to a database and then update a table that displays the database information. Strangely, the process works fine the first time - data is posted and the table is refresh ...

Is the Ajax call being triggered unexpectedly?

I've been working on an Ecommerce website and everything was going smoothly until the update_cart (quantity) code started submitting my form instead of updating it. I've tried making changes and even moving it outside the form, but the issue rema ...

How to Save Information to MySQL Database Using an HTML Form with Checkboxes

I recently set up an HTML form that includes checkboxes and successfully managed to store the values using an array in my database. After adding a name field to the form along with a corresponding column in my MySQL table, I hit a roadblock. The new name ...

ASP TextChanged event is not triggering unless the user presses the Enter key or tabs out

My code is functioning correctly, however, the TextChanged event is triggered when I press enter or tab, causing my code to execute. I would like to be able to search for a record without having to press enter or tab. ASP: asp:TextBox ID="txtNameSearch ...

Ajax activates a slider

One element I have is a menu div, along with a content div Initially, when the slider (which is a jquery plugin) is placed in the index.php, everything appears to be working well, as shown in the image below However, my objective is to have each page&apo ...

How can I position text next to an input within a <td> element in a vertical arrangement?

Below is the HTML code: <fieldset><legend>Callout Report</legend> <table> <tr><td>Start Time</td><td> <input type="text" id="startTimeUI" autocomplete="off" /> </td></tr> <tr><td& ...

Exploring the DOM in JavaScript: Searching for the final ancestor containing a specific attribute

Check out this example of HTML code: <div id="main1" data-attribute="main"> <div id="section2" data-attribute="subsection"> <div id="nested3" data-attribute="sub-subsection"> </div> </div> </div> <div id= ...

React Images: Carousel display issue with images not centered

Is there a way to center the selected image instead of it appearing on the left-hand side? Here is the current behavior: https://i.stack.imgur.com/SUWOK.jpg I am using the following packages in a sandbox environment with Next.js 11 and TailwindCSS 2.2.4: ...

What could be the reason for this JavaScript malfunctioning in both Chrome and Safari browsers?

Why is it that the javascript works fine in Firefox for the gallery on the page below, but doesn't seem to be functioning properly in Chrome and Safari? In Chrome, the big image isn't displaying but the thumbnails are, and in Safari nothing show ...

Simple JavaScript numeric input field

Hey there, I'm a beginner learning JavaScript. I'm currently working on creating a program that adds numbers input through text fields. If you want to check out the HTML code, visit this link on JS Fiddle: http://jsfiddle.net/fCXMt/ My questio ...

The contents of the div do not display when the button is pressed except in jsfiddle

I have written a script that triggers the appearance of a div where users can adjust the time settings for a timer. The functionality works perfectly on JSFiddle, with the div displaying as intended. However, when tested on other online IDEs such as Coding ...