The grid wrapper pattern is useful for aligning grid content within a central wrapper, while also allowing items to break out and align to the edge of the containing element or page when desired.
Items placed on the grid should be able to align to a horizontally-centered max-width wrapper and/or the outer edges of the grid.
This recipe uses the CSS Grid
minmax()
function to define the grid track sizes in the
grid-template-columns
property. For the central columns with a maximum width we can set a minimum value of 0 or greater and a maximum value that specifies the maximum size the column tracks will grow to. Using a numeric unit (pixels, ems, rems) will create a fixed maximum size for the central wrapper, whereas using percentage values or viewport units will mean this wrapper grows or shrinks in response to its context.
The outer two columns have a maximum size of
1fr
, meaning that they will each expand to fill the remaining available space in the grid container.
When using this recipe at page level it can be useful to set a
max-width
along with left and right
auto
margin
s to center the content horizontally:
.grid {
max-width: 1200px;
margin: 0 auto; // horizontally centers the container
}
/* Remove the max-width and margins if the browser supports Grid */
@supports (display: grid) {
.grid {
display: grid;
/* Other grid code goes here */
max-width: none;
margin: 0;
}
}
To “break out” a full-width item to the edge of the viewport you can then use this trick (courtesy of Una Kravets ):
.item {
width: 100vw;
margin-left: 50%;
transform: translate3d(-50%, 0, 0);
}
This gives a good approximation of the layout, only without the benefit of being able to align items easily on an exact grid.
Although Grid enables us to position items anwhere (within reason), it is important when placing items using CSS Grid that your underlying markup follows a logical order (see CSS Grid layout and accessibility for more details).
The various layout methods have different browser support. See the charts below for details on basic support for the properties used.
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
grid-template-columns
|
Chrome
完整支持
57
|
Edge
完整支持
16
|
Firefox
完整支持
52
|
IE
完整支持
10
Alternate Name
|
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
| Animation of tracks | Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 66 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 66 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
fit-content()
|
Chrome 完整支持 29 | Edge 完整支持 16 | Firefox 完整支持 51 | IE 不支持 No | Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 51 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
minmax()
|
Chrome
完整支持
57
|
Edge 完整支持 16 |
Firefox
完整支持
52
|
IE 不支持 No |
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
repeat()
|
Chrome
完整支持
57
|
Edge 完整支持 16 |
Firefox
部分支持
57
注意事项
|
IE 不支持 No |
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
subgrid
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
完整支持
71
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
部分支持
不支持
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
grid-template-columns