Search code examples
htmlcssgridresponsivehamburger-menu

Create an hamburger menu into a grid layout (without javascript)


First of all, I'm french, so sorry if I make some mistakes while explaining my problem, as my english is not perfect. I'm also a Webdesigner, not basically a pure dev, that's why I'm asking for help here (and because I didn't found a perfect answer to my problem on the web).

I need to create an hamburger menu for mobile / tablet devices, using a grid layout.

How I would like it to looks like when clicked https://i.sstatic.net/Xh5sV.png

I would like the menu to looks like it when on clicked (based on a 320px's width device). When not clicked, the #menu-ul is hidden (-767px) and you can see the content of the page instead.

My code for the moment looks like this :

body {
display: grid; 
}

#back-top {
position: fixed;
bottom: 40px;
right: 14px;
z-index: 9995;
width: 35px; 
height: 35px;
text-align: center;
font-size: 45px;
font-family: 'Agency FB', arial;
line-height: 32px;
background: #22cfb5;
color: #fff;
cursor: pointer;
border-radius: 50%;
transform: rotate(-90deg);
-o-transition:background-color .5s;
-ms-transition:background-color .5s;
-moz-transition:background-color .5s;
-webkit-transition:background-color .5s;
 transition:background-color .5s;
text-decoration: none;
}

#back-top:hover {
background: #4c4bbf;
}

header {
position: sticky;
top: 0;
z-index: 9999;
}

#navcontainer {
background-color: #4c4bbf;
}

.menu span {
display: block;
width: 19px;
height: 2px;
margin-bottom: 3px;
position: relative;
background: #ffffff; 
z-index: 1;
transform-origin: 4px 0px;
transition: transform 0.5s 
cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s 
cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
transition-property: transform, background, opacity;
transition-duration: 0.5s, 0.5s, 0.55s;
transition-timing-function:
cubic-bezier(0.77, 0.2, 0.05, 1),
cubic-bezier(0.77, 0.2, 0.05, 1),
ease;
transition-delay: 0s, 0s, 0s;
}

.menu input {
display: block;
width: 19px;
height: 13px; 
position: absolute;
cursor: pointer;
opacity: 0;
z-index: 2;
}

.menu input:checked~span {
opacity: 1;
transform: rotate(-45deg) translate(-2px, -1px);
}

.menu input:checked~span:nth-last-child(3) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}

.menu input:checked~span:nth-last-child(2) {
transform: rotate(45deg) translate(-2px, -1px);
}

@media (max-width: 767px) {
body {
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 
  74.375rem 11.1875rem;
  gap: 0px 20px;
}

header {
  grid-column: 1 / span 6;
  grid-row: 1 / span 1; 
} 

#navcontainer {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 4.375rem;
  column-gap: 10px;
  place-items: center;
}

.logo {
  grid-column: 1 / span 5; 
}

.menu {
  grid-column: 6 / span 1;
}

#menu-ul {
  background: linear-gradient(-30deg,#6633ff,#6666ff);
  z-index: 9998;
  grid-column: 1 / span 6; 
  grid-row: 1 / span 7;
  position: absolute;
  width: 100%;
  left: -767px;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 4.375rem 60.625rem 33.75rem 102.6875rem 103.75rem 74.375rem 11.1875rem;
  gap: 0px 20px;
  transition: transform .3s ease-in-out;
}

ul {
  grid-column: 2 / span 4;
  grid-row: 1 / span 3;
  list-style-type: none;
  -webkit-font-smoothing: antialiased;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem 4.375rem 
  4.375rem;
  place-items: center;
}

.menu input:checked~#menu-ul {
  transform: translateX(767px);
}

.a-menu1 {
  grid-column: 2 / span 4;
  grid-row: 1 / span 3;
}

.a-menu2 {
  grid-column: 2 / span 4;
  grid-row: 2 / span 3;
}

.a-menu3 {
  grid-column: 2 / span 4;
  grid-row: 3 / span 3;
}

.a-menu4 {
  grid-column: 2 / span 4;
  grid-row: 4 / span 3;
}

#linkedin-menu {
  height: 28px;
  width: 28px;
  grid-column: 1 / span 3;
  grid-row: 6  / span 2;
}

#insta-menu {
  height: 28px;
  width: 28px;
  grid-column: 4 / span 3;
  grid-row: 6  / span 2;
}
}
<body>
<a href="#" id="back-top" title="Back top">></a>
<header>
    <nav role="navigation" id="navcontainer">
        <div class="logo">
            <img src="../logo_249.png">
        </div>
        <div class="menu">
            <input type="checkbox"/>
            <span></span>
            <span></span>
            <span></span>
        </div>
    </nav>
</header>

<div id="menu-ul">
    <ul>
        <a class="a-menu1" href="#bienvenue"><li>À propos</li></a>
        <a class="a-menu2" href="#services"><li>Mes services</li></a>
        <a class="a-menu3" href="#projets"><li>Mes projets</li></a>
        <a class="a-menu4" href="#contact"><li>Contact</li></a>
        <img src="img/linkedin-white.png" id="linkedin-menu">
        <img src="img/insta-white.png" id="insta-menu">
    </ul>
</div> 
<!-- + all the remaining content of the page, hidden by the menu ul list when clicked -->
</body>

Having this code, I have many problems: first of all, I basically don't manage to toggle my #menu-ul while clicking on my checkbox / burger menu.

In the second time, I already tried to put my whole #menu-ul into the .menu, and from there the toggle works but I have a weird results (the #menu-ul coming over a part of the nav / header, and the span becoming a cross becomes broken because of it).

At last, I would also like the cross + clicking on my anchors' menu (except from Instagram / LinkedIn that will be target:_blank) to make my #menu-ul go back to an hidden / off-canva position.

Does someone can help me with this, moreover explain me so I don't have anymore difficulties in the future? Thank you and if you need anymore information, I can give you this :)


Solution

  • You need this kind of Output?If yes,Then Let me know I will manage your code properly.