'Vertical nav using flexbox
I am trying to create a website using flexbox. While creating navigation menu, I am facing a problem where my nav links aren't showing up vertically in smaller screens though it appears horizontally in large screens. Could you please correct me what am I missing? Here's my CSS JS and HTML for your reference.
<body>
<nav>
<div id="brand">
<img src="logo.svg" id="brandImg">
</div>
<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()">☰</a>
</div>
</nav>
<script type="text/javascript" src="js/main.js"></script>
</body>
Here is My CSS code including media queries.
html,body{
margin: 0;
padding: 0;
}
nav{
display: flex;
}
#brand{
flex: 1;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
background-color: #333;
}
#brandImg{
height: 4em;
flex: 1;
}
#myTopnav{
flex: 5;
background-color: #333;
}
.topnav{
display: flex;
flex-direction: row;
justify-content: flex-end;
}
.topnav a {
color: #f2f2f2;
text-align: center;
padding: 2em 4em;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.active {
background-color: #4CAF50;
color: white;
}
.topnav .icon {
display: none;
}
@media screen and (max-width: 600px) {
#brandImg{
height: 3em;
}
.topnav a{display: none;}
.topnav a.icon {
padding: 2em;
float: right;
display: block;
}
}
@media screen and (max-width: 600px) {
.topnav.responsive a {
flex-flow: column;
}
}
And my JS:
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|