I've been working on creating a navigation bar for my website that is not only centered but also responsive. I've encountered an issue where, upon resizing the browser window to a very small width, the links disappear and are replaced by an icon in the top right corner. Clicking on this icon reveals the links in a different layout. I've tried various methods to center my navigation bar, but they have mostly resulted in it becoming misaligned. While I am familiar with how to center a non-responsive nav bar, I really like the responsiveness of this design as it would look great on mobile devices. If anyone has any tips to help me achieve perfect alignment while maintaining responsiveness, I would greatly appreciate it!
.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: #4CAF50;
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: left;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="topnav" id="myTopnav">
<a href="#news" class="active">HOME</a>
<a href="#news">SERVICES</a>
<a href="#contact">PRODUCTS</a>
<a href="#news">CONTACT</a>
<a href="#news">CALCULATORS</a>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
</body>
</html>