Flexbox with unordered list

I am trying to learn flexbox and I really like it. I am trying to play with dynamic width, and when I do this with divs, it works. If I try to do this with li, this will not work. My code is on codepen .

div.example
  ul
    li
      | 1
    li
      | 2
    li
      | 3
    li
      | 4
    li
      | 5
    li
      | 6

div.container
  div.col
    | 1
  div.col
    | 2
  div.col
    | 3
  div.col
    | 4
  div.col
    | 5
  div.col
    | 6

SASS Code

.container {
  border: 1px solid #000;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 50%;

  .col {
    width: calc(100% / 3);
    height: 120px;
    text-align: center;
  }
}

.example {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  width: 50%;
  border: 1px solid #000;
  margin: 1em 0;

  ul {
    width: 100%;
    padding-left: 0;
  }

  li {
    list-style: none;
    display: inline-block;
    width: calc(100% / 3);
    height: 120px;
    text-align: center;
  }
}
+19
source share
3 answers

You need to apply flex properties to <ul>

ul {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

Putting properties in <div>tells him to display <ul>in flex, not <li>.

+32
source

Based on your markup, note that the elements <li>are children <ul>and not a division element .example.

<div class="example">
  <ul>
    <li>1</li>
  </ul>
</div>

<li> <ul>, flex <ul>, <li> <div>:

.example {
  width: 50%;
  border: 1px solid #000;
  margin: 1em 0;

  ul {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    width: 100%;
    padding-left: 0;
  }

  li {
    list-style: none;
    display: inline-block;
    width: calc(100% / 3);
    height: 120px;
    text-align: center;
  }
}

http://codepen.io/anon/pen/NGPBbg

+9

I have the same problem ... no matter what I do, the list items are mixed together ...

<!DOCTYPE html>
<html>
<head>
</head>
  <header id="header">
    <div id="nav-container">
    <nav id="nav-bar">
<ul>
<li><a class="nav-link" href="#Beginner">Beginner</a></li>
<li><a class="nav-link" href="#Intermediate">Intermediate</a></li>
<li><a class="nav-link" href="#Advanced">Advanced</a></li>
</ul>
</nav>
    </div>
  </header>
<body>
</body>
    </div>
</html>

This is my CSS code:

#nav-container{
  height: auto;
  width: 1000px;
  background-color: lightblue;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
}

nav ul{
  display:flex;
  justify-content: space-between;
  flex: auto;
}

#header{
    position:fixed;
}

https://codepen.io/sidneyyin/pen/rgzJrP

-1
source

Source: https://habr.com/ru/post/1606003/


All Articles