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.
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.
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.
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.
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. ...
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>
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
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