A few days ago, I watched a video of a foreign boss using HTML + CSS to implement HamburgerMenu. I was watching Sass recently, so I used Sass to implement it.
The final rendering is as follows:
File structure in VS Code (the easy sass is used to compile scss files):
Page structure (index.html):
_config.scss:
/*Set the color and max-width*/$primary-color: rgba(13,110,139,.75);$overlay-color: rgba(24,39,51,.85);$max-width: 980px;/*Set the text color, if the background color is light, set to black, otherwise set to white*/@function set-text-color($color){ @if(lightness($color)>70){ @return #333; }@else{ @return #fff; }}/*Mixer, set the background color and text color*/@mixin set-background($color){ background-color: $color; color: set-text-color($color);}style.scss:
@import '_config';*{ margin: 0; padding: 0;}.container{ max-width: $max-width; margin: 0 auto;}/*Set the background color for showcase, use the pseudo-class to add a background image, and set the z-index of the background image to -1 (this picture will be displayed inside); and then in order to let the text be displayed in the middle, use flex layout*/.showcase{ position: relative; height: 100vh; background-color: $primary-color; &:before{ content: ''; position: absolute; left: 0; top: 0; width: 100%; height: 100%; background: url('../img/pexels-photo-533923.jpeg') no-repeat center center / cover; z-index: -1; } &-inner{ display: flex; height: 100%; flex-direction: column; justify-content: center; align-items: center; text-align: center; color: #fff; font-weight: 100; h1{ font-size: 4rem; padding: 1.2rem 0; } p{ white-space: pre-wrap; font-size: 1.6rem; padding: 0.85rem 0; } .btn{ padding: .65rem 1rem; /*Use the mixer to set the background color and text color*/ @include set-background(lighten($primary-color,30%)); border: none; border: 1px solid $primary-color; border-radius: 5px; text-decoration: none; outline: none; transition: all .2s ease .1s; /* When the button hover is used to set a scaling effect using css3's scale*/ &:hover{ @include set-background(lighten($overlay-color,30%)); border-color: lighten($overlay-color, 25%); transform: scale(.98); } } } }}/*Principle: Change the style by whether to do the checkbox in or not (set the checkbox transparency to 0, and the z-index is set higher). When clicking, a menu will appear. Click again, and the menu will disappear *//*Set fixed for menu-wrap, so that the showcase will fill the entire screen; at the same time, set the opacity of checkbox to 0; Also note that you need to set the z-index of checkbox to 2, because the z-index of hamburger is set to 1, otherwise the click will not work*/.menu-wrap{ position: fixed; left: 0; top: 0; z-index: 1; .toggle{ position: absolute; left: 0; top: 0; width: 50px; height: 50px; opacity: 0; z-index: 2; cursor: pointer; /*When checkbox is checked, set the rotation effect of the div and pseudo-class in hamburger*/ &:checked ~ .hamburger>div{ transform: rotate(135deg); /*The pseudo-class actually rotates 225 degrees, and you need to set the top to 0, otherwise the length of the formed ❌ is inconsistent*/ &:before,&:after{ transform: rotate(90deg); top: 0; } } /* When checkbox is selected, a rotation effect will also be set */ &:checked:hover ~ .hamburger>div{ transform: rotate(235deg); } &:checked ~ .menu{ visibility: visible; >div{ transform: scale(1); >div{ opacity: 1; } } } } } } .hamburger{ position: absolute; left: 0; top: 0; width: 60px; height: 60px; padding: 1rem; background-color: $primary-color; box-sizing: border-box; display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center; z-index: 1; /*div generates the middle horizontal line, then sets the position as relative, and then sets its pseudo-class to absolute, and offsets it with respect to the div*/ >div{ position: relative; left: 0; top: 0; width: 100%; height: 2px; background-color: #fff; transition: all .7s ease; /*Use pseudo-class to generate the first and third horizontal lines*/ &:before, &:after{ content: ''; position: absolute; left: 0; top: -10px; width: 100%; height: 2px; background-color: inherit; } &:after{ top: 10px; } } } /*Set the style of the selected menu*/ /*Set menu to fixed, with width and height of 100%, and then set display to flex, otherwise the menu will not appear in the middle*/ .menu{ position: fixed; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; display: flex; justify-content: center; align-items: center; visibility: hidden; /*Set the menu to invisible, and then set it to visible when checkbox is selected*/ transition: all .75s ease; >div{ @include set-background($overlay-color); width: 200vw; height: 200vh; overflow: hidden; border-radius: 50%; display: flex; justify-content: center; align-items: center; text-align: center; transform: scale(0); transition: all .4s ease; >div{ max-width: 90vw; max-height: 90vh; opacity: 0; transition: all .4s ease; >ul>li{ list-style: none; font-size: 2rem; padding: .85rem 0; text-transform: uppercase; transform: skew(-5deg,-5deg) rotate(5deg);/*Set text tilt*/ a{ color: inherit; text-decoration: none; transition: color .4s ease; } } } } } } } }This is the article about HTML+Sass implementing HambergurMenu (hamburger-style menu). For more related HTML+Sass implementing HambergurMenu, please search for previous articles from Wulin.com or continue to browse the related articles below. I hope everyone will support Wulin.com in the future!