Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox with ul list

Tags:

css

flexbox

I want to use flexbox to build 2x2 grid with my list. My code is as follows:

<ul class="cont">
    <li>text</li>
    <li>text</li>
    <li>text</li>
    <li>text</li>
</ul>

css:

.cont { display: flex; }
.cont li { width: 50%; }

Unfortunately, it aligns four of my li in a row.

like image 852
Tomek Buszewski Avatar asked Sep 07 '15 12:09

Tomek Buszewski


People also ask

How to turn an unordered list into a flex container?

Edit the CSS code in order to turn the unordered list into a flex container: The list elements are now flex items. In order to make them take the whole available space within the container, you have to use the flex property on the list items.

Where can I find Flexbox examples?

Flexyboxes is an app that allows you to see code samples and change parameters to see how Flexbox works visually. Flexbox Patters is a website that shows off a bunch of Flexbox examples. We've covered all the most common CSS flexbox properties.

What are the key Flexbox terms?

Here are definitions of the key flexbox terms, taken from the official W3C specification for flexbox. main-axis: The main axis of a flex container is the primary axis along which flex items are laid out. The direction is based on the flex-direction property.

What are the use cases of Flexbox?

Typical use cases of Flexbox 1 Navigation. A common pattern for navigation is to have a list of items displayed as a horizontal bar. ... 2 Split navigation. Another way to align items on the main axis is to use auto margins. ... 3 Center item. ... 4 Card layout pushing footer down. ... 5 Media objects. ... 6 Form controls. ... 7 Conclusion. ...


2 Answers

Wrap the flexible box row and give the items flex-basis: 50% or width: 50%

.cont {
  display: flex;
  flex-flow: row wrap; /* Shorthand for flex-direction: row and flex-wrap: wrap */
}
.cont li {
  flex-basis: 50%; /* or width: 50% */
}
<ul class="cont">
  <li>text</li>
  <li>text</li>
  <li>text</li>
  <li>text</li>
</ul>
like image 162
m4n0 Avatar answered Oct 03 '22 20:10

m4n0


flex-wrap: wrap let's flex know it's allowed to wrap. It's default argument when undefined is nowrap.

https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap

like image 32
Dan Gamble Avatar answered Oct 03 '22 20:10

Dan Gamble