-2

I want to create an uneven hamburger button like this:

enter image description here

and transform/animate it into an X button, on click:

enter image description here

Of course, with CSS only. How to do it? (maybe a trivial question but I am far from a CSS expert)

Huqe Dato
  • 235
  • 1
  • 16
  • Try and do it yourself first. Look into SVG shapes and CSS Keyframes. – Zach Jensz Sep 28 '22 at 11:11
  • So how did you create the "uneven hamburger," is it an ``, ``, a series of elements...? Please share your (relevant) "*[mcve]*" code, we're unlikely to write all the code for you, but if you show your starting point and your best attempt(s), we may be able to help iron out your mistakes, or show you better approaches. – David Thomas Sep 28 '22 at 11:19
  • Excuse but I have nothing to share as I don't know how to do this. As already said: I am not an css expert. I need a full example to start from. – Huqe Dato Sep 28 '22 at 11:27

1 Answers1

2

Try this:

body {
  margin: 0;
  font-family: Helvetica, sans-serif;
  background-color: #f4f4f4;
}

a {
  color: #000;
}

/* header */

.header {
  background-color: #fff;
  box-shadow: 1px 1px 4px 0 rgba(0,0,0,.1);
  position: fixed;
  width: 100%;
  z-index: 3;
}

.header ul {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow: hidden;
  background-color: #fff;
}

.header li a {
  display: block;
  padding: 20px 20px;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}

.header li a:hover,
.header .menu-btn:hover {
  background-color: #f4f4f4;
}

.header .logo {
  display: block;
  float: left;
  font-size: 2em;
  padding: 10px 20px;
  text-decoration: none;
}

/* menu */

.header .menu {
  clear: both;
  max-height: 0;
  transition: max-height .2s ease-out;
}

/* menu icon */

.header .menu-icon {
  cursor: pointer;
  display: inline-block;
  float: right;
  padding: 28px 20px;
  position: relative;
  user-select: none;
}

.header .menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.header .menu-icon .navicon:before,
.header .menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.header .menu-icon .navicon:before {
  top: 5px;
  width: 9px;
}

.header .menu-icon .navicon:after {
  top: -5px;
  width: 12px;
}

/* menu btn */

.header .menu-btn {
  display: none;
}

.header .menu-btn:checked ~ .menu {
  max-height: 240px;
}

.header .menu-btn:checked ~ .menu-icon .navicon {
  background: transparent;
  width: 18px;
}

.header .menu-btn:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
  width: 18px;
}

.header .menu-btn:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
  width: 18px;
}

.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:before,
.header .menu-btn:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}

/* 48em = 768px */

@media (min-width: 48em) {
  .header li {
    float: left;
  }
  .header li a {
    padding: 20px 30px;
  }
  .header .menu {
    clear: none;
    float: right;
    max-height: none;
  }
  .header .menu-icon {
    display: none;
  }
}
<header class="header">
  <a href="" class="logo">CSS Nav</a>
  <input class="menu-btn" type="checkbox" id="menu-btn" />
  <label class="menu-icon" for="menu-btn"><span class="navicon"></span></label>
  <ul class="menu">
    <li><a href="#home">Home</a></li>
    <li><a href="#about">About</a></li>
    <li><a href="#careers">Careers</a></li>
    <li><a href="#contact">Contact</a></li>
  </ul>
</header>
Rizeen
  • 1,296
  • 1
  • 6
  • 17