P粉8212742602023-08-28 13:45:30
For a vertically flowing grid that creates new columns as needed and has no defined rows, consider using CSS Multi-Column Layout (Example). CSS Grid Layout (at least the current implementation - Level 1) cannot perform this operation task. The question is this:
In CSS grid layout, the grid-auto-flow
and grid-template-rows
/ grid-template-columns< /code> properties.
More specifically, grid items can be oriented horizontally if both grid-auto-flow: row
(the default setting) and grid-template-columns
are defined Flows nicely, automatically creating new rows as needed. This concept is illustrated in the code in the question.
#container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-flow: row; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
However, after switching to grid-template-rows
, the grid items are stacked in a single column.
#container { display: grid; grid-template-rows: 1fr 1fr 1fr; grid-auto-flow: row; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
Using grid-auto-flow: row
and grid-template-rows
will not automatically create columns. grid-template-columns
must be defined (thus, inversely related to grid-auto-flow
).
#container { display: grid; grid-template-rows: 1fr 1fr 1fr; grid-template-columns: 1fr 1fr 1fr; grid-auto-flow: row; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
The opposite case also has the same behavior.
With grid-auto-flow:column
and grid-template-rows
defined, grid items can flow nicely in the vertical direction and be automatically created as needed new column.
#container { display: grid; grid-template-rows: 1fr 1fr 1fr; grid-auto-flow: column; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
However, after switching to grid-template-columns
, the grid items are stacked in a row. (This is the question most people ask, including in this question.)
#container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-auto-flow: column; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
Rows will not be created automatically. This requires defining grid-template-rows
. (This is the most commonly offered solution, but is usually rejected because the layout has a variable number of rows.)
#container { display: grid; grid-template-columns: 1fr 1fr 1fr; grid-template-rows: 1fr 1fr 1fr; grid-auto-flow: column; }
<div id="container"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div>8</div> <div>9</div> </div>
Therefore, please consider adopting the multi-column layout solution, as mentioned above.
Specification reference: 7.7. Automatic placement: grid-auto-flow
Properties