fit-content() CSS function clamps a given size to an available size according to the formula min( maximum size , max( minimum size , argument )) .

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.

The function can be used as a track size in CSS Grid properties, where the maximum size is defined by max-content and the minimum size by auto , which is calculated similar to auto (即, minmax(auto, max-content) ), except that the track size is clamped at argument if it is greater than the auto minimum.

The function can also be used as laid out box size for width , height , min-width , min-height , max-width and max-height , where the maximum and minimum sizes refer to the content size.

句法

fit-content() function accepts a <length> <percentage> as an argument.

/* <length> values */
fit-content(200px)
fit-content(5cm)
fit-content(30vw)
fit-content(100ch)
/* <percentage> value */
fit-content(40%)
				

<length>

An absolute length.

<percentage>

A percentage relative to the available space in the given axis.

In grid properties it is relative to the inline size of the grid container in column tracks and to the block size of the grid container for row tracks. Otherwise it is relative to the available inline size or block size of the laid out box depending on the writing mode.

范例

Sizing grid columns with fit-content

HTML

<div id="container">
  <div>Item as wide as the content.</div>
  <div>
    Item with more text in it. Because the contents of it are
    wider than the maximum width, it is clamped at 300 pixels.
  </div>
  <div>Flexible item</div>
</div>
					

CSS

#container {
  display: grid;
  grid-template-columns: fit-content(300px) fit-content(300px) 1fr;
  grid-gap: 5px;
  box-sizing: border-box;
  height: 200px;
  width: 100%;
  background-color: #8cffa0;
  padding: 10px;
}
#container > div {
  background-color: #8ca0ff;
  padding: 5px;
}
					

结果

规范

规范 状态 注释
CSS Box Sizing Module Level 3
The definition of 'fit-content()' in that specification.
工作草案 Defines the function as laid out box size for width , height , min-width , min-height , max-width and max-height .
CSS 栅格布局
The definition of 'fit-content()' in that specification.
候选推荐 Defines the function when used as a track size.

浏览器兼容性

The compatibility table on 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.

Supported for width (and other sizing properties)

更新 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
fit-content Chrome 完整支持 46
完整支持 46
Prefixed Implemented with the vendor prefix: -webkit-
不支持 1 — 48 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Edge 完整支持 79 Firefox 完整支持 3 Prefixed
完整支持 3 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
IE 不支持 No Opera 完整支持 33
完整支持 33
Prefixed Implemented with the vendor prefix: -webkit-
不支持 15 — 35 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Safari 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 2 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
WebView Android 完整支持 46
完整支持 46
Prefixed Implemented with the vendor prefix: -webkit-
不支持 1 — 48 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Chrome Android 完整支持 46
完整支持 46
Prefixed Implemented with the vendor prefix: -webkit-
不支持 18 — 48 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Firefox Android 完整支持 4 Prefixed
完整支持 4 Prefixed
Prefixed Implemented with the vendor prefix: -moz-
Opera Android 完整支持 33
完整支持 33
Prefixed Implemented with the vendor prefix: -webkit-
不支持 14 — 35 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Safari iOS 完整支持 11
完整支持 11
Prefixed Implemented with the vendor prefix: -webkit-
完整支持 1 Alternate Name
Alternate Name Uses the non-standard name: intrinsic
Samsung Internet Android 完整支持 5.0
完整支持 5.0
Prefixed Implemented with the vendor prefix: -webkit-
不支持 1.0 — 5.0 Alternate Name
Alternate Name Uses the non-standard name: intrinsic

图例

完整支持

完整支持

不支持

不支持

使用非标名称。

使用非标名称。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

Supported in grid layout

更新 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
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

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改:
  1. CSS
  2. CSS 参考

Copyright  © 2014-2026 乐数软件    

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