I have a nav bar that I want to display as a column on small screens and as a row on desktop screen sizes. My problem is that I do not know why "flex-direction: row;" won't change the nav bar to fit side by side.
.top-nav li {
list-style-type: none;
font-size: 22px;
border: 2px solid #333;
width: 80%;
padding: 0;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
box-shadow: 2px 2px 2px #333;
}
.top-nav {
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
}
.top-nav ul {
padding: 0;
margin: 0;
}
@media (min-width: 750px) {
/* top nav */
.top-nav {
flex-direction: row;
}
.top-nav li {
margin: 0;
width: auto;
}
}
<div class="top-nav">
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Category</a></li>
<li><a>Recent</a></li>
<li><a>All recipes</a></li>
</ul>
</nav>
</div>
You have only declared the flex-wrap component: wrap . This means that the flex-direction component remains the default value: row . Hence, your flex items are aligning horizontally in a row that wraps. Also important: If you want flex items to wrap in column-direction, you need to define a height on the container.
flex-direction: row-reverse; The flexbox items are ordered the opposite way as the text direction, along the main axis. flex-direction: column; The flexbox items are ordered the same way as the text direction, along the cross axis.
Problem in your selector scope. Your flex
is not working at all to manage your nav item layout. You applying CSS the navigation container not the menu item container. Even when it is showing as a column this is also not credit of flex
property here. See below it may help you:
The key part is:
.top-nav ul {
display: flex;
flex-direction: row;
}
.top-nav li {
list-style-type: none;
font-size: 22px;
border: 2px solid #333;
width: 80%;
padding: 0;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
box-shadow: 2px 2px 2px #333;
}
.top-nav {
display: flex;
flex-direction: column;
text-align: center;
justify-content: center;
}
.top-nav ul {
padding: 0;
margin: 0;
}
@media (min-width: 750px) {
/* top nav */
.top-nav {
flex-direction: row;
}
.top-nav ul {
display: flex;
flex-direction: row;
}
.top-nav li {
margin: 0;
width: auto;
}
<div class="top-nav">
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Category</a></li>
<li><a>Recent</a></li>
<li><a>All recipes</a></li>
</ul>
</nav>
</div>
Following changes in Your CSS For solution
.top-nav li {
list-style-type: none;
font-size: 22px;
border: 2px solid #333;
width: 80%;
padding: 0;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
box-shadow: 2px 2px 2px #333;
}
.top-nav {
text-align: center;
justify-content: center;
}
.top-nav ul
{
padding: 0;
margin: 0;
display: flex;
}
@media screen and (max-width: 480px)
{
.top-nav ul
{
flex-direction:column;
}
.top-nav li
{
margin: 0;
width: auto;
}
}
<div class="top-nav">
<nav>
<ul>
<li><a>Home</a></li>
<li><a>Category</a></li>
<li><a>Recent</a></li>
<li><a>All recipes</a></li>
</ul>
</nav>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With