--- title: 网格模板列 slug: Web/CSS/grid-template-columns tags: - CSS - CSS網格 - 列子 translation_of: Web/CSS/grid-template-columns ---
The grid-template-columns
CSS property defines the line names and track sizing functions of the {{glossary("grid column", "grid columns")}}.
{{cssinfo}}
/* Keyword value */ grid-template-columns: none; /* <track-list> values */ grid-template-columns: 100px 1fr; grid-template-columns: [linename] 100px; grid-template-columns: [linename1] 100px [linename2 linename3]; grid-template-columns: minmax(100px, 1fr); grid-template-columns: fit-content(40%); grid-template-columns: repeat(3, 200px); /* <auto-track-list> values */ grid-template-columns: 200px repeat(auto-fill, 100px) 300px; grid-template-columns: minmax(100px, max-content) repeat(auto-fill, 200px) 20%; grid-template-columns: [linename1] 100px [linename2] repeat(auto-fit, [linename3 linename4] 300px) 100px; grid-template-columns: [linename1 linename2] 100px repeat(auto-fit, [linename1] 300px) [linename3]; /* Global values */ grid-template-columns: inherit; grid-template-columns: initial; grid-template-columns: unset;
none
<length>
<percentage>
auto
.<flex>
fr
specifying the track’s flex factor. Each <flex>
-sized track takes a share of the remaining space in proportion to its flex factor.
When appearing outside a minmax()
notation, it implies an automatic minimum (i.e. minmax(auto, <flex>)
).
max-content
min-content
{{cssxref("minmax", "minmax(min, max)")}}
<flex>
value sets the track’s flex factor. It is invalid as a minimum.auto
Note: auto
track sizes (and only auto
track sizes) can be stretched by the {{cssxref("align-content")}} and {{cssxref("justify-content")}} properties.
{{cssxref("fit-content", "fit-content( [ <length> | <percentage> ] )")}}
min(max-content, max(auto, argument))
, which is calculated similar to auto
(i.e. minmax(auto, max-content)
), except that the track size is clamped at argument if it is greater than the auto
minimum.{{csssyntax}}
#grid { display: grid; width: 100%; grid-template-columns: 50px 1fr; } #areaA { background-color: lime; } #areaB { background-color: yellow; }
<div id="grid"> <div id="areaA">A</div> <div id="areaB">B</div> </div>
{{EmbedLiveSample("Examples", "100%", "20px")}}
Specification | Status | Comment |
---|---|---|
{{SpecName("CSS3 Grid", "#propdef-grid-template-columns", "grid-template-columns")}} | {{Spec2("CSS3 Grid")}} | Initial definition |
{{Compat("css.properties.grid-template-columns")}}