Home > Web Front-end > CSS Tutorial > How to Make a CSS Grid Container Fill Columns Instead of Rows?

How to Make a CSS Grid Container Fill Columns Instead of Rows?

Barbara Streisand
Release: 2024-12-18 17:22:19
Original
870 people have browsed it

How to Make a CSS Grid Container Fill Columns Instead of Rows?

Make Grid Container Fill Columns, Not Rows

In CSS Grid Layout, the grid-auto-flow property determines whether items are laid out in rows or columns. When set to row, which is the default setting, items are placed horizontally, creating new rows as needed. This behavior is typically not ideal if the desired layout is vertical.

When grid-auto-flow is set to row, grid-template-columns defines the number of columns in the grid. However, in this scenario, the items still flow horizontally, filling up the rows rather than the columns.

For a vertically-flowing grid that creates new columns as necessary, without defining rows, consider using CSS Multi-Column Layout instead. It allows for vertical stacking of items, automatically creating new columns as needed.

#container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-auto-flow: row;
}
Copy after login

This code snippet illustrates the concept. Items will flow horizontally, creating new rows as necessary, despite the definition of three columns in the grid-template-columns property. Note that using CSS Grid Layout for this specific layout is not recommended.

The above is the detailed content of How to Make a CSS Grid Container Fill Columns Instead of Rows?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template