iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦒

Creating a Hamburger Menu Transition from ≡ to × with CSS Animation

に公開
2023/10/23

Introduction

As the title suggests, this is an animation that transforms a hamburger menu's ≡ or = into an ×.
Originally, I was forcing the implementation by dividing the height of a square by √2 to make the horizontal bars form the diagonals of the square. However, I've managed to implement it in a much better way this time, so I'm sharing it in the hopes that others might be facing the same struggle. To be honest, it might be so straightforward that you'll wonder why it was even an issue, but please bear with me.
Also, if anyone knows an even simpler or more stylish way to implement this, I'd love to hear about it.

The Finished Product

Source Code
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hamburger</title>
    <style>
        *,
        *::before,
        *::after {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --bar-thickness: 5px;
        }

        section {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        body{background-color: #eee;}

        .hamburger {
            width: 40px;
            height: 40px;
            position: relative;
            transition: transform .5s;
        }

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

        .hamburger div {
            position: absolute;
            width: 100%;
            height: 5px;
            background-color: #333;
            border-radius: 3px;
            transform-origin: 50% 50%;
            transition: .5s .25s;
        }

        .hamburger div:nth-child(1){top: 0;}
        .hamburger div:nth-child(2){top: calc(50% - var(--bar-thickness) / 2);}
        .hamburger div:nth-child(3){top: calc(100% - var(--bar-thickness));}

        .hamburger:hover div{top: calc(50% - var(--bar-thickness) / 2);}
        .hamburger:hover div:nth-child(1){transform: rotate(-135deg);}
        .hamburger:hover div:nth-child(2){opacity: 0;}
        .hamburger:hover div:nth-child(3){transform: rotate( 135deg);}
    </style>
</head>
<body>
    <section>
        <div class="hamburger">
            <div></div>
            <div></div>
            <div></div>
        </div>
    </section>
</body>
</html>

Explanation

First, set the transform-origin of the horizontal bars to the center of the bar.

.hamburger div {
    position: absolute;
    width: 100%;
    height: 5px;
    background-color: #333;
    border-radius: 3px;
    transform-origin: 50% 50%;
    transition: .5s .25s;
}

Next, instead of using bottom: 0, specify the position from the top for the bottom bar. This gives the source code a sense of consistency and looks cleaner. (To be honest, there's no problem at all if you specify it with bottom.)

.hamburger div:nth-child(1){top: 0;}
.hamburger div:nth-child(2){top: calc(50% - var(--bar-thickness) / 2);}
.hamburger div:nth-child(3){top: calc(100% - var(--bar-thickness));}

Finally, move the three horizontal bars to the center and then rotate them. I chose 135° because it feels more nimble compared to 45°. This part is entirely up to your preference.

.hamburger:hover div{top: calc(50% - var(--bar-thickness) / 2);}
.hamburger:hover div:nth-child(1){transform: rotate(-135deg);}
.hamburger:hover div:nth-child(2){opacity: 0;}
.hamburger:hover div:nth-child(3){transform: rotate( 135deg);}

All that's left is to set the transition nicely and it's complete.

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hamburger</title>
    <style>
        *,
        *::before,
        *::after {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --bar-thickness: 5px;
        }

        section {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        body{background-color: #eee;}

        .hamburger {
            width: 40px;
            height: 40px;
            position: relative;
            transition: transform .5s;
        }

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

        .hamburger div {
            position: absolute;
            width: 100%;
            height: 5px;
            background-color: #333;
            border-radius: 3px;
            transform-origin: 50% 50%;
            transition: .5s .25s;
        }

        .hamburger div:nth-child(1){top: 0;}
        .hamburger div:nth-child(2){top: calc(50% - var(--bar-thickness) / 2);}
        .hamburger div:nth-child(3){top: calc(100% - var(--bar-thickness));}

        .hamburger:hover div{top: calc(50% - var(--bar-thickness) / 2);}
        .hamburger:hover div:nth-child(1){transform: rotate(-135deg);}
        .hamburger:hover div:nth-child(2){opacity: 0;}
        .hamburger:hover div:nth-child(3){transform: rotate( 135deg);}
    </style>
</head>
<body>
    <section>
        <div class="hamburger">
            <div></div>
            <div></div>
            <div></div>
        </div>
    </section>
</body>
</html>

Conclusion

It's a common feeling when programming, but thinking back, I wonder why I didn't think of this earlier...

Bonus

The transformation from = to × was achievable using just a single div.

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Hamburger2</title>
  <style>
    *,
    *::before,
    *::after {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    :root {
      --bar-height: 20px;
    }

    section {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }

    body {background: #333;}

    .hamburger {
      width: 100px;
      height: 100px;
      position: relative;
      transition: transform .5s;
    }

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

    .hamburger::before,
    .hamburger::after {
    content: "";
    display: block;
    width: 100%;
    height: var(--bar-height);
    border-radius: 5px;
    background: #eee;
    position: absolute;
    transition: .5s .25s;
    }

    .hamburger::before {top: calc(50% - var(--bar-height) * 1.5);}

    .hamburger::after  {top: calc(50% + var(--bar-height) / 2);}

    .hamburger:hover::before,
    .hamburger:hover::after {top: calc(50% - var(--bar-height) / 2);}

    .hamburger:hover::before {transform: rotate(-135deg);}

    .hamburger:hover::after  {transform: rotate( 135deg);}
  </style>
</head>
<body>
  <section>
    <div class="hamburger"></div>
  </section>
</body>
</html><meta name="viewport" content="width=device-width, initial-scale=1.0">

Discussion