How we can create a menu with a grid layout using HTML CSS? Solution: CSS Grid Menu Layout With Display Flex, Which is a Responsive Grid Design.
I am sure that you know what is CSS Grid, & almost every designer or developer knows how to create girds. Using CSS grid and flex properties, the easiest point is making an object responsive. Nowadays the grid-based menu is on trend, Many creative developers now choose a grid-based menu over simple navbar.
Yesterday, I was surfing some personal websites, Then I saw a menu with grid view. Believe me, I feel these types of the menu is more attractive than a list type menu. If you want to make your website simple and fast, then don’t use this. Otherwise, you can use this to make your website more good looking.
Now the question is how to create the menu? Don’t worry, because today I am sharing CSS Grid Menu Layout Using Display Flex. In other words, I am sharing a Responsive Grid Based Menu Design. I added using display flex because I am also using it along with grid property. & it is also responsive, means it fits on every screen sizes.
If you are thinking now how this menu actually is, then see the preview given below.
Preview Of Responsive Grid Layout
See this video preview to getting an idea of how this menu looks like.
Now you can see this visually. If you like this, then get the source code of its.
You May Also Like:
- HTML CSS Glitch Effect
- Light & Dark Mode Using CSS
- CSS Animated Contact Form
- Split Image Using JavaScript
CSS Grid Menu Source Code
As always, Before sharing source code let’s talk about it. As you know I got inspiration from a personal portfolio website to creating this. Actually, I created a clone of that website’s menu, sadly I forget the websites name. I used HTML CSS and JavaScript to create this menu.
Actually, JavaScript has a very small part of this menu. I used JavaScript to just create a toggle button, nothing else. Left all is based on Pure CSS and of course HTML also. All we know that we can’t create a webpage without HTML.
First I created Button using HTML and put two class, one for styling and one for creating a toggle effect.
1 |
<button type="button" class="menu-button menu-toggle" aria-label="Click to open menu"></button> |
That’s how the buttons HTML looks like. I used “+” sign on button, you can also use external icons like font-awesome. After click on plus button its rotate on 765 deg. For creating the line effect on hover I used :before
property. At before section, I created a blank content with 3px width, bottom:20px;
and transform-origin: bottom;
property. After positioning and alignment, the final line you can see on the video.
I used display: grid;
property only in the main div. Inside the main div, I created 5 divs with class= "mainn-menu"
 And inside the maiin-menu div, I create another div with class = "content"
. In content section I used display: flex;
property.
For placing Image I put background-image
property by selecting .maiin-menu:nth-child
. & I used @media
for creating it responsive. Basically, for the responsive design, I change the grid’s width value (get info), that’s all. You can fully understand after getting the code.
For creating this you have to create 3 files. First for HTML, second for CSS, and third for JavaScript. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<!DOCTYPE html> <!-- code by webdevtrick ( https://webdevtrick.com ) --> <html> <head> <meta charset="UTF-8"> <title>CSS Grid Menu Responsive | Webdevtrick.com</title> <link href="https://fonts.googleapis.com/css?family=Aldrich&display=swap" rel="stylesheet"> <link rel="stylesheet" href="style.css"> </head> <body> <button type="button" class="menu-button menu-toggle" aria-label="Click to open menu"></button> <div class="menu main-menu-switch"> <div class="maiin-menu"> <div class="items"></div> <div class="content"> <a href="#" class="btn-link">HTML</a> </div> </div> <div class="maiin-menu"> <div class="items"></div> <div class="content"> <a href="#" class="btn-link">CSS</a> </div> </div> <div class="maiin-menu"> <div class="items"></div> <div class="content"> <nav class="bundle"> <a class="inside-link" href="#">Web Development</a> <a class="inside-link" href="#">Source Code</a> </nav> </div> </div> <div class="maiin-menu"> <div class="items"></div> <div class="content"> <a href="#" class="btn-link">JavaScript</a> </div> </div> <div class="maiin-menu"> <div class="items"></div> <div class="content"> <a href="https://webdevtrick.com" class="btn-link">Webdevtrick.com</a> </div> </div> </div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put these codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
/** code by webdevtrick ( https://webdevtrick.com ) **/ * { box-sizing: border-box; } body { margin: 0; background-blend-mode: overlay; } .bundle { display: flex; flex-direction: column; flex: 1 1 auto; justify-content: center; align-items: center; width: 100%; height: 100%; } .bundle .inside-link { text-decoration: none; color: #333; font-size: 1.5rem; font-family: 'Aldrich', sans-serif; padding: 1% 0.1rem; font-weight: bold; transition: all 300ms ease; position: relative; display: block; } .bundle .inside-link:hover:after { transform: scaleX(4); } .btn-link { display: flex; height: 100%; width: 100%; flex: 1 1 auto; justify-content: center; align-items: center; text-decoration: none; color: #272727; font-size: 14px; font-weight: bold; letter-spacing: 3px; font-family: 'Aldrich', sans-serif; position: relative; } .btn-link:before { content: ''; position: absolute; display: block; width: 3px; height: calc(50% - 60px); transform-origin: bottom; transition: all 300ms ease; transform: scaleY(0); background: #272727; bottom: 20px; left: 50%; margin-left: -2px; opacity: 1; } .btn-link:hover:before { transform: scaleY(1); } .menu-button { position: fixed; top: 20px; left: 20px; z-index: 3; background: #FF4848; border: none; box-shadow: none; outline: none; width: 60px; height: 60px; padding: 0; font-family: 'Aldrich', sans-serif; font-size: 10px; cursor: pointer; display: flex; flex-wrap: wrap; align-items: center; justify-content: center; border-radius: 50%; transition: all 300ms ease; } .menu-button:after { content: '+'; width: 50px; height: 50px; display: block; color: white; font-size: 48px; font-weight: lighter; transition: all 300ms ease; line-height: 47px; } .menu-button:hover { transform: scale(1.1); } .menu-button.toggle-switch-menu-open:after { transform: rotate(765deg); } .menu { height: 100vh; width: 100vw; display: grid; overflow-y: auto; grid-template-columns: 100%; grid-template-rows: 20% 20% 20% 10% 10% 10% 10%; } .maiin-menu { opacity: 0; transition: all 200ms ease; overflow: hidden; position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; flex: 1 1 auto; } .maiin-menu .items { position: absolute; height: 100%; width: 100%; top: 0; left: 0; background-size: cover; background-repeat: no-repeat; background-position: center; mix-blend-mode: overlay; transform-origin: center; transform: scale(2); transition: all 2000ms ease-out; z-index: 1; } .maiin-menu .content { display: flex; flex: 1 1 auto; justify-content: center; align-items: center; height: 100%; width: 100%; position: relative; z-index: 2; opacity: 0; transition: all 300ms ease; } .maiin-menu:first-child { background-color: #272727; } .maiin-menu:first-child .btn-link { color: #eee; } .maiin-menu:first-child .btn-link:before { background: #ccc; } .maiin-menu:nth-child(2) { background-color: #FF4848; } .maiin-menu:nth-child(3) { background-color: #FFCA31; grid-column-start: 1; grid-row-start: 1; grid-row-end: 4; } .maiin-menu:nth-child(4) { background-color: #70FF84; } .maiin-menu:nth-child(5) { background-color: #eee; } .maiin-menu:nth-child(1) { transition-delay: 50ms; transform-origin: bottom; transform: scaleY(0); } .maiin-menu:nth-child(1) .items { background-image: url(https://images.pexels.com/photos/879109/pexels-photo-879109.jpeg); } .maiin-menu:nth-child(1) .content { transition-delay: 0ms; } .toggle-switch .maiin-menu:nth-child(1) { transition-delay: 200ms; } .toggle-switch .maiin-menu:nth-child(1) .content { transition-delay: 700ms; } .maiin-menu:nth-child(2) { transition-delay: 100ms; transform-origin: left; transform: scaleX(0); } .maiin-menu:nth-child(2) .items { background-image: url(https://images.pexels.com/photos/1181243/pexels-photo-1181243.jpeg); } .maiin-menu:nth-child(2) .content { transition-delay: 50ms; } .toggle-switch .maiin-menu:nth-child(2) { transition-delay: 400ms; } .toggle-switch .maiin-menu:nth-child(2) .content { transition-delay: 900ms; } .maiin-menu:nth-child(3) { transition-delay: 150ms; transform-origin: bottom; transform: scaleY(0); } .maiin-menu:nth-child(3) .items { background-image: url(https://images.pexels.com/photos/214221/pexels-photo-214221.jpeg); } .maiin-menu:nth-child(3) .content { transition-delay: 100ms; } .toggle-switch .maiin-menu:nth-child(3) { transition-delay: 600ms; } .toggle-switch .maiin-menu:nth-child(3) .content { transition-delay: 1100ms; } .maiin-menu:nth-child(4) { transition-delay: 200ms; transform-origin: left; transform: scaleX(0); } .maiin-menu:nth-child(4) .items { background-image: url(https://images.pexels.com/photos/2058128/pexels-photo-2058128.jpeg); } .maiin-menu:nth-child(4) .content { transition-delay: 150ms; } .toggle-switch .maiin-menu:nth-child(4) { transition-delay: 800ms; } .toggle-switch .maiin-menu:nth-child(4) .content { transition-delay: 1300ms; } .maiin-menu:nth-child(5) { transition-delay: 250ms; transform-origin: bottom; transform: scaleY(0); } .maiin-menu:nth-child(5) .items { background-image: url(https://images.pexels.com/photos/1714208/pexels-photo-1714208.jpeg); } .maiin-menu:nth-child(5) .content { transition-delay: 200ms; } .toggle-switch .maiin-menu:nth-child(5) { transition-delay: 1000ms; } .toggle-switch .maiin-menu:nth-child(5) .content { transition-delay: 1500ms; } .toggle-switch .maiin-menu { transform: scale(1); opacity: 1; transition: all 400ms ease; } .toggle-switch .maiin-menu .items { transform: scale(1); } .toggle-switch .maiin-menu .content { opacity: 1; } .toggle-switch .maiin-menu:hover .items { transform: scale(1.2); } @media (min-width: 480px) { .menu { grid-template-columns: 30% 70%; grid-template-rows: 25% 25% 25% 25%; } } @media (min-width: 800px) { .menu { grid-template-columns: 20% 30% 50%; grid-template-rows: 60% 40%; } } @media (min-width: 480px) { .maiin-menu:nth-child(3) { grid-column-start: 2; grid-row-start: 5; grid-row-end: 1; } } @media (min-width: 800px) { .maiin-menu:nth-child(3) { grid-column-start: 3; grid-row-start: 3; grid-row-end: 1; } } @media (min-width: 480px) { .bundle .inside-link { font-size: 2rem; } } |
function.js
The final step, Create a JS file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 |
/** code by webdevtrick ( https://webdevtrick.com ) **/ const menu = document.querySelector('.main-menu-switch'); const menuTrigger = document.querySelectorAll('.menu-toggle'); menuTrigger.forEach(btn => { btn.addEventListener('click', function () { menuTrigger.forEach(b => { b.classList.toggle('toggle-switch-menu-open'); }); menu.classList.toggle('toggle-switch'); }); }); |
That’s It. Now you have successfully created CSS Grid Menu With Display Flex, a Responsive Grid Menu. If you have any doubt or question comment down below.
Thanks For Visiting, Keep Visiting.
Bhai where do you learn all this? It just looks easy. But when comes to implementing, its not easy.
How do you get such ideas?
And that too at this frequency that after every single day, there is something to see from you?
Bro for inspiration I surf multiple designers website. 🙂
How to make this work without the button? Just the grid and it’s contents. I’m new to coding. Thanks
If you want to create it without button, then there is no need for JavaScript. Just create a div and visible grids on hover.
what does “visible grids on hover” mean?
I have tried to get rid of the javascript and wrap all else in a div container. Seems there is more to it. Do you have an example of “visible grids on hover?”