can someone please help me center the responsive menu in this W3 example?
What I’m looking to do is:
Desktop: The div and the menu inside it should be centered perfectly in the horizontal-middle of the page, vertically-on-top, with the menu items going from left to right.
Mobile: The "Menu" button should be in the horizontal-center, and each menu item should also be in the horizontal-center, going from top to bottom. Also, I would like the menu items to only be visible in the mobile view when the menu is open, instead of always displaying something like "home" on top. On top, it should just be the "MENU" text, in the center, nothing else.
No matter what I do, I can only get the menu-items go center perfectly, nothing else… what am I missing? I’m very new to HTML/CSS/JS so please go easy on me 🙂
The original example:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04AA6D;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
.topnav a:not(:first-child) {display: none;}
.topnav a.icon {
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive {position: relative;}
.topnav.responsive .icon {
position: absolute;
right: 0;
top: 0;
}
.topnav.responsive a {
float: none;
display: block;
text-align: center;
}
}
</style>
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="#home" class="active">Home</a>
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">MENU</a>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>
3
Answers
You can use
flex
for centering your nav links and also if you don’t want to see thehome
you need to remove:not(:first-child)
from this part of the code:The menu items are centered horizontally and vertically on the page, adapting to the screen size and orientation. The "Menu" button functions as intended, revealing the hidden menu items when clicked in the mobile view.
First of all, you can use
display: grid
as I used in the below code snippet (or evendisplay:flex
!) to position almost anything in the center. To position the menu button in the horizontal-center, I putdisplay: grid;
on the navbar element and then putjustify-self: center
on the menu button, which centers itself in the horizontal-center.Links:
Then in this part of the styles,
I removed the
:not(:first-child)
, which just applies thedisplay: none;
style to all of the elements except the first element of.topnav
.Also to make the menu button still appear in the center even when the menu is opened (in mobile), I had to remove the
position: absolute
from this part of the styles,and also put the menu button / element as the first element inside the navbar, so that the menu button would still appear at the top of the screen.
Note: In the below snippet of code, I have commented on the lines on which I removed or added something.
Example: