Updated hamburger button snippet to be clearer
This commit is contained in:
@ -6,59 +6,59 @@ tags: interactivity,beginner
|
|||||||
This is a way to build simple hamburger button for menu bar.
|
This is a way to build simple hamburger button for menu bar.
|
||||||
|
|
||||||
```html
|
```html
|
||||||
<button class="hb"></button>
|
<div class="hamburger-menu">
|
||||||
|
<div class="top"></div>
|
||||||
|
<div class="middle"></div>
|
||||||
|
<div class="bottom"></div>
|
||||||
|
</div>
|
||||||
```
|
```
|
||||||
|
|
||||||
```css
|
```css
|
||||||
.hb,
|
.hamburger-menu {
|
||||||
.hb:before,
|
display: flex;
|
||||||
.hb:after {
|
flex-direction: column;
|
||||||
position: relative;
|
flex-wrap: wrap;
|
||||||
width: 30px;
|
justify-content: space-between;
|
||||||
height: 5px;
|
height: 2.5rem;
|
||||||
border: none;
|
width: 2.5rem;
|
||||||
outline: none;
|
|
||||||
background-color: #333;
|
|
||||||
border-radius: 3px;
|
|
||||||
transition: 0.5s;
|
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
|
||||||
|
|
||||||
.hb:before,
|
.bar {
|
||||||
.hb:after {
|
height: 5px;
|
||||||
content: '';
|
background: black;
|
||||||
position: absolute;
|
border-radius: 5px;
|
||||||
top: -7.5px;
|
margin: 3px 0px;
|
||||||
left: 0;
|
transform-origin: left;
|
||||||
}
|
transition: all 0.5s;
|
||||||
|
}
|
||||||
|
|
||||||
.hb:after {
|
&:hover {
|
||||||
top: 7.5px;
|
.top {
|
||||||
}
|
transform: rotate(45deg);
|
||||||
|
}
|
||||||
|
|
||||||
.hb:hover {
|
.middle {
|
||||||
background-color: transparent;
|
opacity: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hb:hover:before,
|
.bottom {
|
||||||
.hb:hover:after {
|
transform: rotate(-45deg);
|
||||||
top: 0;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
.hb:hover::before {
|
|
||||||
transform: rotate(45deg);
|
|
||||||
}
|
|
||||||
|
|
||||||
.hb:hover::after {
|
|
||||||
transform: rotate(-45deg);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Explanation
|
#### Explanation
|
||||||
|
|
||||||
- Use a `<button>` element for the middle bar of the hamburger icon.
|
- Use a `hamburger-menu` container div to which contanins the top, bottom and middle bars.
|
||||||
- Use the `::before` and `::after` pseudo-elements to create the top and bottom bars of the icon.
|
- The container is set to be a flex container (`display: flex`) with `flex-direction` to be `column` and `flex-wrap` to be `wrap`.
|
||||||
- Use `position: relative` on the `<button>` and `position: absolute` on the pseudo-elements to place them appropriately.
|
Alternatively, you can set both properties by a shorthand `flex-flow: column wrap`
|
||||||
- Use the `:hover` pseudo-selector to rotate `:before` to `45deg` and `:after` to `-45deg` and hide the center bar using`:background-color` transparent.
|
- We add distance between the bars using `justify-content: space-between`
|
||||||
|
- The animation has 3 parts, top and bottom bars transforming to 45 degree angles (`rotate(45deg)`) and the middle bar fading away by setting the `opacity: 0`
|
||||||
|
- The `transform-origin` is set to `left` so the rotation point of origin is left
|
||||||
|
- We set `transition all 0.5s` so that both `transform` and `opacity` are properties are animated for half a second
|
||||||
|
|
||||||
#### Browser support
|
#### Browser support
|
||||||
|
|
||||||
|
- Flexbox - https://caniuse.com/#feat=flexbox
|
||||||
|
- CSS Transitions - https://caniuse.com/#feat=css-transitions
|
||||||
|
|||||||
Reference in New Issue
Block a user