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.

Recipe

Download this example

Choices made

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.

Useful fallbacks or alternative methods

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.

grid-template-columns

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
grid-template-columns Chrome 完整支持 57
完整支持 57
完整支持 29 Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge 完整支持 16
完整支持 16
不支持 12 — 79 Alternate Name
Alternate Name Uses the non-standard name: -ms-grid-rows
Firefox 完整支持 52
完整支持 52
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
IE 完整支持 10 Alternate Name
完整支持 10 Alternate Name
Alternate Name Uses the non-standard name: -ms-grid-columns
Opera 完整支持 44
完整支持 44
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari 完整支持 10.1 WebView Android 完整支持 57 Chrome Android 完整支持 57
完整支持 57
完整支持 29 Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 完整支持 52
完整支持 52
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 43
完整支持 43
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
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
完整支持 57
完整支持 29 Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge 完整支持 16 Firefox 完整支持 52
完整支持 52
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 44
完整支持 44
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari 完整支持 10.1 WebView Android 完整支持 57 Chrome Android 完整支持 57
完整支持 57
完整支持 29 Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 完整支持 52
完整支持 52
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 43
完整支持 43
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari iOS 完整支持 10.3 Samsung Internet Android 完整支持 6.0
repeat() Chrome 完整支持 57
完整支持 57
完整支持 38 Disabled
Disabled From version 38: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge 完整支持 16 Firefox 部分支持 57 注意事项
部分支持 57 注意事项
注意事项 repeat(auto-fill, ...) and repeat(auto-fit, ...) only support one repeated column (see bug 1341507 ).
不支持 52 — 57 注意事项
注意事项 calc() doesn't work in repeat() (见 bug 1350069 ).
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 44
完整支持 44
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari 完整支持 10.1 WebView Android 完整支持 57 Chrome Android 完整支持 57
完整支持 57
完整支持 38 Disabled
Disabled From version 38: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Firefox Android 完整支持 52
完整支持 52
不支持 40 — 59 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 43
完整支持 43
完整支持 28 Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari iOS 完整支持 10.3 Samsung Internet Android 完整支持 6.0
subgrid Chrome 不支持 No Edge 不支持 No Firefox 完整支持 71
完整支持 71
完整支持 69 注意事项 Disabled
注意事项 Enabled by default in Firefox Nightly.
Disabled ). To change preferences in Firefox, visit about:config.
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

图例

完整支持

完整支持

部分支持

部分支持

不支持

不支持

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

使用非标名称。

使用非标名称。

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考
  3. Layout cookbook
  4. 指南
    1. Breadcrumb Navigation
    2. Card
    3. Center an element
    4. Column layouts
    5. Contribute a recipe
    6. Grid wrapper
    7. List group with badges
    8. Pagination
    9. Recipe: Media objects
    10. Split Navigation
    11. Sticky footers

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1