Created
June 7, 2019 01:54
-
-
Save asifbacchus/542129c748f15d174924608461d26839 to your computer and use it in GitHub Desktop.
Pure CSS hamburger menu using target pseudo-class to avoid using java to close
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* dirty global reset -- in practice this should target elements | |
since '*' is known to be slow */ | |
* { | |
box-sizing: border-box; | |
margin: 0; | |
padding: 0; | |
} | |
/* set variables and font size to 62.5% (10px for most systems) */ | |
:root { | |
--headingFont: 'Oswald', sans-serif; | |
--bodyFont: 'EB Garamond', serif; | |
--fgColour: #8eb8e5; | |
--bgColour: #5b5750; | |
--accentColour: #f5fbef; | |
--bgMenuColour: #6b7f82; | |
font-size: 62.5%; | |
} | |
body { | |
background-color: var(--bgColour); | |
font-size: 1.6rem; | |
line-height: 2.5rem; | |
padding-top: 50px; | |
} | |
h1,h2,h3,h4,h5,h6 { | |
margin: 2rem 0; | |
font-family: var(--headingFont); | |
color: var(--fgColour); | |
} | |
h1 { | |
font-size: 5rem; | |
} | |
h2 { | |
font-size: 3rem; | |
} | |
p { | |
font-family: var(--bodyFont); | |
color: var(--fgColour); | |
margin: 1rem 0; | |
} | |
/* menu wrapper -- layer 1, above content on layer 0 and fixed to upper right*/ | |
.menu-wrapper { | |
position: fixed; | |
z-index: 1; | |
top: 0; | |
right: 0; | |
} | |
/* menu links -- fixed to upper right, hidden but above everything else */ | |
.menu-wrapper .menu-link { | |
position: absolute; | |
z-index: 2; | |
top: 0; | |
right: 0; | |
cursor: pointer; | |
height: 35px; | |
width: 35px; | |
opacity: 0; | |
} | |
/* default state: hide the menu-close link */ | |
.menu-close { | |
display: none; | |
} | |
/* | |
----------------------- | |
All about the burger... | |
----------------------- | |
*/ | |
/* | |
Make the burger | |
*/ | |
/* place hamburger just under link and with the same dimensions */ | |
.menu-wrapper .menu .hamburger { | |
position: absolute; | |
z-index: 1; | |
top: 0; | |
right: 0; | |
cursor: pointer; | |
height: 35px; | |
width: 35px; | |
background: transparent; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
padding: 0.5rem; | |
} | |
/* generate hamburger middle line */ | |
.menu-wrapper .menu .hamburger .hamburger-lines { | |
position: relative; | |
z-index: 1; | |
height: 2px; | |
width: 100%; | |
display: flex; | |
flex: none; | |
align-items: center; | |
justify-content: center; | |
background-color: var(--fgColour); | |
transition: all 0.5s ease; | |
} | |
/* generate hamburger top and bottom lines */ | |
.menu-wrapper .menu .hamburger .hamburger-lines::before, | |
.menu-wrapper .menu .hamburger .hamburger-lines::after { | |
content: ''; | |
position: absolute; | |
z-index: 1; | |
top: -7px; | |
height: 2px; | |
width: 100%; | |
background: inherit; | |
} | |
/* move lower hamburger line into position */ | |
.menu-wrapper .menu .hamburger .hamburger-lines::after { | |
top: 7px; | |
} | |
/* | |
Dress the burger (animations) | |
*/ | |
/* change hamburger line colour when hovering over link */ | |
.menu-wrapper .menu .menu-open:hover ~ .hamburger .hamburger-lines { | |
background-color: var(--accentColour); | |
} | |
/* turn the burger into an 'X' when menu is active */ | |
.menu-wrapper .menu:target .hamburger-lines { | |
transform: rotate(135deg); | |
} | |
.menu-wrapper .menu:target .hamburger-lines::before, | |
.menu-wrapper .menu:target .hamburger-lines::after { | |
top: 0; | |
transform: rotate(90deg); | |
} | |
/* | |
--------------- | |
Menu is open... | |
--------------- | |
*/ | |
/* Swap menu open and close links */ | |
.menu-wrapper .menu:target .menu-open { | |
display: none; | |
} | |
.menu-wrapper .menu:target .menu-close { | |
display: block; | |
} | |
/* change 'X' to foreground colour for visibility */ | |
.menu-wrapper .menu:target .hamburger .hamburger-lines { | |
background-color: var(--accentColour); | |
} | |
/* rotate the 'X' on hover */ | |
.menu-wrapper .menu:target .menu-close:hover ~ .hamburger .hamburger-lines { | |
transform: rotate(225deg); | |
} | |
/* | |
--------------------- | |
Style the menu itself | |
--------------------- | |
*/ | |
/* basic styling for 'nav' container */ | |
.menu-wrapper .menu .menu-items { | |
position: fixed; | |
top: 0; | |
left: 0; | |
z-index: 0; | |
height: 100%; | |
width: 100%; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
overflow: hidden; | |
visibility: hidden; | |
} | |
/* make inner container overflow 'nav' so we can grow/shrink via animation */ | |
.menu-wrapper .menu .menu-items > div { | |
display: flex; | |
flex: none; | |
height: 200vh; | |
width: 200vw; | |
align-items: center; | |
justify-content: center; | |
background-color: var(--bgMenuColour); | |
border-radius: 50%; | |
transform: scale(0); | |
transition: all 0.5s ease; | |
} | |
/* style menu list */ | |
.menu-wrapper .menu .menu-items > div > ul { | |
text-align: left; | |
max-width: 90vw; | |
max-height: 100vh; | |
list-style: none; | |
opacity: 0; | |
transition: opacity 0.5s ease; | |
} | |
/* style list items */ | |
.menu-wrapper .menu .menu-items > div > ul > li { | |
display: block; | |
color: var(--accentColour); | |
font-family: var(--headingFont); | |
font-size: 3rem; | |
padding: 1.5rem; | |
border-left: 5px solid transparent; | |
border-right: 5px solid transparent; | |
} | |
/* style links */ | |
.menu-wrapper .menu .menu-items > div > ul > li > a { | |
display: block; | |
color: inherit; | |
text-decoration: none; | |
} | |
/* animate menu choices */ | |
.menu-wrapper .menu .menu-items > div > ul > li:hover { | |
border-left: 5px solid var(--accentColour); | |
border-right: 5px solid var(--accentColour); | |
} | |
/* | |
--------------------------------- | |
Show the menu when link is active | |
--------------------------------- | |
*/ | |
.menu-wrapper .menu:target .menu-items { | |
visibility: visible; | |
} | |
.menu-wrapper .menu:target .menu-items > div { | |
transform: scale(1); | |
transition-duration: 0.75s; | |
} | |
.menu-wrapper .menu:target .menu-items > div > ul { | |
opacity: 1; | |
} | |
/* | |
--------------------------------- | |
Page content | |
--------------------------------- | |
*/ | |
.content-wrapper { | |
display: flex; | |
flex-direction: column; | |
padding: 0 4rem 0 2rem; | |
} | |
.content-wrapper h1 { | |
align-self: center; | |
margin-bottom: 3rem; | |
} | |
.item { | |
padding-bottom: 100rem; | |
} | |
.realtext { | |
color: yellow; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment