grid-template-columns - CSS: Cascading Style Sheets 编辑
The grid-template-columns
CSS property defines the line names and track sizing functions of the grid columns.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
/* 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);
grid-template-columns: subgrid;
grid-template-columns: masonry;
/* <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;
Values
none
- Indicates that there is no explicit grid. Any columns will be implicitly generated and their size will be determined by the
grid-auto-columns
property. [linename]
- A
<custom-ident>
specifying a name for the line in that location. The ident may be any valid string other then the reserved wordsspan
andauto
. Lines may have multiple names separated by a space inside the square brackets, for example[line-name-a line-name-b]
. <length>
- A non-negative length, giving the width of the column.
<percentage>
- Is a non-negative
<percentage>
value relative to the inline size of the grid container. If the size of the grid container depends on the size of its tracks, then the percentage must be treated asauto
.
The intrinsic size contributions of the track may be adjusted to the size of the grid container and increase the final size of the track by the minimum amount that would result in honoring the percentage. <flex>
- Is a non-negative dimension with the unit
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
- Is a keyword representing the largest maximal content contribution of the grid items occupying the grid track.
min-content
- Is a keyword representing the largest minimal content contribution of the grid items occupying the grid track.
minmax(min, max)
- Is a functional notation that defines a size range greater than or equal to min and less than or equal to max. If max is smaller than min, then max is ignored and the function is treated as min. As a maximum, a
<flex>
value sets the track’s flex factor. It is invalid as a minimum. auto
- Is a keyword that is identical to maximal content if it's a maximum. As a minimum it represents the largest minimum size (as specified by
min-width
/min-height
) of the grid items occupying the grid track. Note:
auto
track sizes (and onlyauto
track sizes) can be stretched by thealign-content
andjustify-content
properties.fit-content( [ <length> | <percentage> ] )
- Represents the formula
min(max-content, max(auto, argument))
, which is calculated similar toauto
(i.e.minmax(auto, max-content)
), except that the track size is clamped at argument if it is greater than theauto
minimum. repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )
- Represents a repeated fragment of the track list, allowing a large number of columns that exhibit a recurring pattern to be written in a more compact form.
masonry
- The masonry value indicates that this axis should be laid out according to the masonry algorithm.
subgrid
- The
subgrid
value indicates that the grid will adopt the spanned portion of its parent grid in that axis. Rather than being specified explicitly, the sizes of the grid rows/columns will be taken from the parent grid’s definition.
The masonry
value is from Level 3 of the Grid specification and currently only has an experimental implementation behind a flag in Firefox.
The subgrid
value is from Level 2 of the Grid specification and currently only has implementation in Firefox 71 and onwards.
Formal definition
Initial value | none |
---|---|
Applies to | grid containers |
Inherited | no |
Percentages | refer to corresponding dimension of the content area |
Computed value | as specified, but with relative lengths converted into absolute lengths |
Animation type | simple list of length, percentage, or calc, provided the only differences are in the values of the length, percentage, or calc components in the list |
Formal syntax
none | <track-list> | <auto-track-list> | subgrid <line-name-list>?where
<track-list> = [ <line-names>? [ <track-size> | <track-repeat> ] ]+ <line-names>?
<auto-track-list> = [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>? <auto-repeat> [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>?
<line-name-list> = [ <line-names> | <name-repeat> ]+where
<line-names> = '[' <custom-ident>* ']'
<track-size> = <track-breadth> | minmax( <inflexible-breadth> , <track-breadth> ) | fit-content( [ <length> | <percentage> ] )
<track-repeat> = repeat( [ <positive-integer> ] , [ <line-names>? <track-size> ]+ <line-names>? )
<fixed-size> = <fixed-breadth> | minmax( <fixed-breadth> , <track-breadth> ) | minmax( <inflexible-breadth> , <fixed-breadth> )
<fixed-repeat> = repeat( [ <positive-integer> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
<auto-repeat> = repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )where
<track-breadth> = <length-percentage> | <flex> | min-content | max-content | auto
<inflexible-breadth> = <length> | <percentage> | min-content | max-content | auto
<fixed-breadth> = <length-percentage>where
<length-percentage> = <length> | <percentage>
Examples
Specifying grid column sizes
HTML
<div id="grid">
<div id="areaA">A</div>
<div id="areaB">B</div>
</div>
CSS
#grid {
display: grid;
width: 100%;
grid-template-columns: 50px 1fr;
}
#areaA {
background-color: lime;
}
#areaB {
background-color: yellow;
}
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Grid Layout The definition of 'grid-template-columns' in that specification. | Candidate Recommendation | Initial definition |
CSS Grid Layout Module Level 2 The definition of 'subgrid' in that specification. | Working Draft | Adds subgrid |
CSS Grid Layout Module Level 3 The definition of 'masonry layout' in that specification. | Editor's Draft | Adds masonry |
Browser compatibility
BCD tables only load in the browser
See also
- Related CSS properties:
grid-template-rows
,grid-template-areas
,grid-template
- Grid Layout Guide: Basic concepts of grid layout - Grid Tracks
- Video tutorial: Defining a Grid
- Subgrid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论