📅  最后修改于: 2023-12-03 15:14:19.575000             🧑  作者: Mango
The grid-template-columns
property is used in CSS Grid Layout to define the size and distribution of columns in a grid container. It allows you to specify a series of track sizes, and can be used either as a single value or as a space-separated list of values.
/* single value syntax */
grid-template-columns: <track-size>;
/* multiple value syntax */
grid-template-columns: <track-size> <track-size> ...;
The track size can be specified as a length, a percentage, or one of several keywords:
auto
(the default) - the size of the track is based on the size of the grid items that span itmin-content
- the size of the track is based on the size of its contents, but never smaller than the minimum size specified by the contentmax-content
- the size of the track is based on the size of its contents, but never larger than the maximum size specified by the contentfit-content(<length-percentage>)
- similar to max-content
, but allows for a maximum size to be specified.container {
display: grid;
grid-template-columns: 100px;
}
This will create a grid container with one column that is 100 pixels wide.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
This will create a grid container with three columns, where the first and last columns take up 1 fraction of the available space, and the middle column takes up 2 fractions.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
This will create a grid container with columns that have a minimum size of 200 pixels and take up as much space as possible (1fr
), but will never be smaller than their content. The auto-fit
value allows the number of columns to adjust based on the available space.
The grid-template-columns
property is supported in all modern browsers, including Chrome, Firefox, Safari, and Edge. It is not supported in Internet Explorer.
Check out this Firefox Grid Inspector tutorial and Can I Use for more information on browser compatibility.