MDN self-hosted library · e01.org

`flex-line-count` CSS property

The flex-line-count CSS property sets the minimum number of flex lines that flex items will be balanced over in cases where a flex container's flex-wrap or flex-flow property includes the balance keyword.

no runnable cells css-property upstream source

The flex-line-count CSS property sets the minimum number of flex lines that flex items will be balanced over in cases where a flex container's flex-wrap or flex-flow property includes the balance keyword.

CSS Demo: flex-line-count

```css interactive-example-choice flex-line-count: 1;


```css interactive-example-choice
flex-line-count: 3;

```css interactive-example-choice flex-line-count: 4;


```html interactive-example
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    <div>Item One</div>
    <div>Item Two</div>
    <div>Item Three</div>
    <div>Item Four</div>
    <div>Item Five</div>
    <div>Item Six</div>
  </div>
</section>

```css interactive-example

example-element {

border: 1px solid #c5c5c5; width: 80%; display: flex; flex-wrap: wrap balance; }

example-element > div {

background-color: rgb(0 0 255 / 0.2); border: 3px solid blue; width: 60px; margin: 10px; }


## Syntax

```css
/* Integer values */
flex-line-count: 1;
flex-line-count: 3;
flex-line-count: 12;

/* Global values */
flex-line-count: inherit;
flex-line-count: initial;
flex-line-count: revert;
flex-line-count: revert-layer;
flex-line-count: unset;

Values

This property is specified as the following value:

  • integer
  • : A positive integer setting the minimum number of flex lines that balanced, wrapped flex items will be distributed over. The default value is 1.

Description

The flex-line-count property sets the minimum number of flex lines that flex items will be distributed over in wrapping, balanced flex containers — in other words, flex containers that include a flex-wrap or flex-flow property with the balance keyword set in addition to the wrap or wrap-reverse keyword.

A key use case for flex-line-count is creating a balanced set of two (or more) columns, regardless of the number of items in a list. In such cases, setting an explicit height or max-height won't work as you don't know how much content you will have, and may end up with fewer or more columns than desired. See Creating balanced columns for an example implementation.

If balance is not set, or if flex items are not set to wrap onto multiple flex lines, the flex-line-count property has no effect.

If the flex-line-count value is equal to or greater than the number of flex items, there will be one flex item per flex line.

Formal definition

Formal syntax

Examples

Effect of different flex-line-count values

This example demonstrates the effects of different values of flex-line-count on four boxes.

HTML

We include four container divs, each with a class of box and ten child <div>s; each container <div> has a different id value.

<div class="box" id="box-no-balance">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
  <div>Four</div>
  <div>Five</div>
  <div>Six</div>
  <div>Seven</div>
  <div>Eight</div>
  <div>Nine</div>
  <div>Ten</div>
</div>

<div class="box" id="box1">...</div>
<div class="box" id="box2">...</div>
<div class="box" id="box3">...</div>

```html hidden live-sample___flex-line-count

No balance

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

flex-line-count: 3

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

flex-line-count: 4

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

flex-line-count: 5

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

#### CSS

```css hidden live-sample___flex-line-count
* {
  box-sizing: border-box;
}

.box {
  width: 100%;
  border: 2px dotted gray;
  margin-bottom: 20px;
  gap: 10px;
}

.box > * {
  border: 2px solid rgb(96 139 168);
  border-radius: 5px;
  background-color: lightgray;
}

We apply display: flex to all the boxes to make them flex containers, then give them a flex-wrap value of wrap balance to make all their flex children wrap onto multiple, balanced lines.

```css live-sample___flex-line-count .box { display: flex; flex-wrap: wrap balance; }


We also set a flex value of `1 1 150px` on the flex children, so they have a base width of `150px` and will distribute any excess space evenly across the items in each flex line.

```css live-sample___flex-line-count
.box > * {
  flex: 1 1 150px;
}

For the #box-no-balance flex container, we remove the balancing, thereby nullifying the line count, by overriding the original flex-wrap: wrap balance value with wrap. We apply different flex-line-count values to each flex container, incrementing them so their children are laid out over a progressively greater number of flex lines.

```css live-sample___flex-line-count

box-no-balance {

flex-line-count: 6; flex-wrap: wrap; }

box1 {

flex-line-count: 3; }

box2 {

flex-line-count: 4; }

box3 {

flex-line-count: 5; }


We've hidden the rest of the CSS for brevity.

#### Results

100%

Note the following:

- As the first flex container doesn't have the `balance` keyword set in its `flex-wrap` value, its children are not given a balanced distribution and its `flex-line-count` value is ignored.
- The second flex container's `flex-line-count: 3` declaration doesn't affect the layout of the flex children; as the flex items are by default distributed over four flex lines, any value of `4` or less has no effect.

### Creating balanced columns

This example demonstrates how `flex-line-count` can be used to create a balanced set of two columns.

#### HTML

We include an ol element containing ten li elements.

```html
<ol>
  <li>
    <a href="#">The Silent Cartographer</a>, published by Meridian House,
    released March 12, 2014.
  </li>
  <li>
    <a href="#">Echoes of the Fallow Field</a>, published by Northbridge Press,
    released July 4, 2009.
  </li>

  ...
</ol>

```html hidden live-sample___balanced-columns

  1. The Silent Cartographer, published by Meridian House, released March 12, 2014.
  2. Echoes of the Fallow Field, published by Northbridge Press, released July 4, 2009.
  3. A Ledger of Small Regrets, published by Ashwood & Kline, released November 21, 2017.
  4. The Clockmaker's Daughter's Shadow, published by Hollow Pine Publishing, released February 8, 2011.
  5. Salt and Signal, published by Redcliffe Editions, released September 30, 2019.
  6. Under a Borrowed Sky, published by Fenwick & Marsh, released May 16, 2006.
  7. The Last Cartel of Winter, published by Graywolf Bindery, released January 2, 2021.
  8. Notes from an Unfinished Atlas, published by Coastline Books, released June 27, 2013.
  9. The Weight of Empty Rooms, published by Draymoor House, released October 15, 2008.
  10. A Brief History of Almost Everyone, published by Ferngate Press, released April 9, 2022.

#### CSS

We set the list's display to `flex`. We set a flex-direction value of `column` and a flex-wrap value of `balance` using the flex-flow shorthand so that the flex lines are arranged in columns and will balance when wrapped. The gap value `10px 40px` specifies a gap of `10px` between flex items within each column and `40px` between flex lines.

Finally, we set a `flex-line-count` value of `2`, meaning that, even though no fixed height is set on the list, its content will always be wrapped over two balanced columns, regardless of how much content is included.

```css live-sample___balanced-columns
ol {
  display: flex;
  gap: 10px 40px;
  flex-flow: column balance;
  flex-line-count: 2;
}

```css hidden live-sample___flex-line-count live-sample___balanced-columns * { box-sizing: border-box; }

body { padding: 10px 30px; }

@supports not (flex-line-count: 3) { body::before { content: "Your browser does not support the flex-line-count property."; background-color: wheat; text-align: center; padding: 1rem 0;

z-index: 1;
position: fixed;
inset: 40% 0 auto;

} } ```

We've hidden the rest of the CSS for brevity.

Results

100%

Specifications

Browser compatibility

See also