Defining grids with fixed columns

grid-template-columns

Use this property to define a grid system that has a fixed number of columns.

Definitions

Refer to these definitions to better understand how the property values are used.

Grid lines

Each reference to a grid line refers to the imaginary line on either side of grid cells.

For a layouts with N rows, there are N+1 horizontal grid lines — one at the top, one between each row, and one at the bottom.

For a layouts with M columns, there are M+1 vertical grid lines — one at the left edge, one between each column, and one at the right edge.

Grid lines are numbered starting at 1. For rows, the topmost line is 1 and the bottommost line is N+1. For columns, the leftmost line is 1 and the rightmost line is M+1. *

* Arabic, Hebrew and other left-to-right writing systems, reverse everything.
Grid line aliases

For clarity and ease of use, each grid line can be aliased with an arbitrary name. That alias can be used with grid-row-start, grid-row-end, grid-column-start and grid-column-end property values.

Grid line aliases are defined when specifying grid-template-rows, grid-template-columns, and grid-template-areas.

In addition, when a grid line alias is declared by the user within a grid-template-areas definition, the browser implicitly defines two other names that can be used in those start/end properties. For example, if a grid-template-areas defines an area named foo:

generated name property interpretation
'foo-start' grid-row-start The topmost line of the area
'foo-start' grid-column-start The leftmost line of the area
'foo-end' grid-row-end The bottommost line of the area
'foo-end' grid-column-end The rightmost line of the area

Property values

The grid-template-columns property declares the width of each column in a grid system using any of these units:

  • percentages
  • fractional units
  • fitting functions
  • typographic units
  • fixed size units
More about units for specifying column widths . . .

Use a percentage or a factional unit:

% percentage A percent of the grid container's width
fr fractional A fractonal portion of the grid container's remaining available width (where the sum of all the fractional declarations equals the whole)

Or use a fitting function:

min-content The width of the element is the minimum amount necessary for any element in the grid column to have all its content fit without overflowing.
max-content The width of the element is the maximum amount necessary for any element in the grid column to have all its content fit without overflowing.
minmax(H1, H2) The width is not less than H1 and not more than H2
fit-content(H) The width expands to whatever is needed to fit the contents of the element, but never more than H
auto Use whatever space is available after all other column constraints have been met

Or use any of these font-relative typographic units to set each grid template column width:

em em The font-size of the current font
rem relative em The font-size of the <html> element
ch "0" The advance measure (the width) of the current font's "0" glyph
ic ideograph count The advance measure (the width) of the current font's "水" ideograph

Or use any of these fixed size units which are independent of the current font:

in inch A size equal 96px
px pixels A size equal to 1/96 inch
pt point A size equal 1in/72 = 1.33px
pc pica A size equal 1in/6 = 12pt = 16px
cm centimeter A size equal to 96px/2.54 = 37.8px
mm millimeter A size equal to 96px/25.4 = 3.8px
Q quarter millimeter A size equal to 96px/1016 = 0.94px

Specify one value for each column in the container. For example, a container with 3 columns might look like:

display: grid;            
grid-template-columns: 15% 75% 10%
A grid with 3 columns, specified using percentages
display: grid;            
grid-template-columns: 1.5fr 7.5fr 1fr
A grid with 3 columns, specified using fractional units

Grid line aliases

Grid line aliases may be defined between the column widths, by specifying an arbitrary name enclosed in square brackets. These aliases may then be used in other CSS declarations that specify starting and ending lines for columns. Each grid line may be aliased with more than one name.

For example, this declaration defines convenient aliases for use in a classic menuarea, body, sidebar grid container.

display: grid;            
grid-template-columns: [menu-area-left] 15% [menu-area-right body-left] 75% [body-right sidebar-left] 10% [sidebar-right]
A grid with 3 columns, and grid line aliases

Shorthand for similar sized columns

When several columns have the same width, it may be convenient to specify them using the repeat() function, as in this example:

display: grid;            
grid-template-columns: 4rem repeat(3, 1fr);
A grid with one fixed width column, and 3 equal-width fractional parts

style > grid > grid-template-columnsDefining grids with fixed columns

🔎