Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flex-direction: row is not working; it's still displaying as a column

Tags:

html

css

flexbox

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>
like image 889
joseph chauvin Avatar asked Dec 07 '18 04:12

joseph chauvin


People also ask

Why does flex direction column not work?

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.

What is the difference between Flex direction row and column?

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.


Video Answer


2 Answers

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>
like image 63
Hanif Avatar answered Oct 11 '22 23:10

Hanif


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>
like image 26
Chandresh Polra Avatar answered Oct 11 '22 22:10

Chandresh Polra