W3cubDocs

/CSS

fit-content

The fit-content behaves as fit-content(stretch). In practice this means that the box will use the available space, but never more than max-content.

When used as laid out box size for width, height, min-width, min-height, max-width and max-height the maximum and minimum sizes refer to the content size.

The CSS Sizing specification also defines the fit-content() function. This page details the keyword.

Syntax

width: fit-content
block-size: fit-content

Examples

Using fit-content for box sizing

HTML

<div class="container">
  <div class="item">Item</div>
  <div class="item">Item with more text in it.</div>
  <div class="item">Item with more text in it, hopefully we have added enough text so the text will start to wrap.</div>
</div>

CSS

.container {
  border: 2px solid #ccc;
  padding: 10px;
  width: 20em;
}

.item {
  width: -moz-fit-content;
  width: fit-content;
  background-color: #8ca0ff;
  padding: 5px;
  margin-bottom: 1em;
}

Result

Specifications

Specification Status Comment
CSS Box Sizing Module Level 4
The definition of 'fit-content' in that specification.
Editor's Draft Defines the value as laid out box size for width, height, min-width, min-height, max-width and max-height.

Browser compatibilityUpdate compatibility data on GitHub

Supported for width (and other sizing properties)

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
fit-content 46
46
22
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
1 — 48
Uses the non-standard name: intrinsic
79
79
79
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
3
Prefixed
3
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
No 33
33
15
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
15 — 35
Uses the non-standard name: intrinsic
11
11
6.1
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
2
Uses the non-standard name: intrinsic
Mobile
Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
fit-content 46
46
≤37
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
1 — 48
Uses the non-standard name: intrinsic
46
46
25
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
18 — 48
Uses the non-standard name: intrinsic
4
Prefixed
4
Prefixed
Prefixed Implemented with the vendor prefix: -moz-
33
33
14
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
14 — 35
Uses the non-standard name: intrinsic
11
11
7
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
1
Uses the non-standard name: intrinsic
5.0
5.0
1.5
Prefixed
Prefixed Implemented with the vendor prefix: -webkit-
1.0 — 5.0
Uses the non-standard name: intrinsic

See also

© 2005–2020 Mozilla and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/fit-content