Favorites

fit-content() CSS Function

fit-content()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The fit-content() function sizes a grid track to fit its content, but never larger than a given maximum, such as fit-content(300px).

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The fit-content() CSS function sizes a grid track to fit its content, but never larger than a given maximum. It is used in grid-template-columns, grid-template-rows, grid-auto-columns and grid-auto-rows. A track defined with fit-content(200px) shrinks around short content, grows with longer content, and stops at 200px, after which its content wraps.

The track size is calculated as min(max-content, max(auto, limit)). In practice it behaves like auto up to the limit, but it does not stretch to fill leftover space the way an auto track can. The argument is a length or a percentage; percentages are resolved against the size of the grid container in the relevant axis. Note that the function form is different from the fit-content keyword used with properties such as width.

/* Sidebar sized to its content, up to 250px */
.layout {
  display: grid;
  grid-template-columns: fit-content(250px) 1fr;
  gap: 1rem;
}

/* Labels column that never gets wider than 30% */
.form {
  display: grid;
  grid-template-columns: fit-content(30%) 1fr;
}

Interactive Demo

A column with quite a lot of text in it
Two
Short
Four
Demo Code
HTML Code
<div class="demo-element">
	<div>A column with quite a lot of text in it</div>
	<div>Two</div>
	<div>Short</div>
	<div>Four</div>
</div>
CSS Code
.demo-element > div {
	padding: 8px;
	background: #4361ee;
	color: #fff;
	font-size: 13px;
}
.demo-element > div:nth-child(2n) {
	background: #f72585;
}
.demo-element {
	display: grid;
	width: 300px;
	gap: 6px;
	padding: 6px;
	box-sizing: border-box;
	background: #f4f4f4;
	outline: 1px dashed #b0b0b0;
	grid-template-columns: fit-content(50px) 1fr;
}

Syntax

fit-content() = fit-content( <length-percentage [0,∞]> )

Values

  • <length-percentage>The maximum size of the track, as a non-negative <length> or a <percentage> of the grid container's size in that axis. The track is sized to its content but clamped to this limit.

Example

<div class="wrap">
<h1>CSS fit-content() examples</h1>
<div class="card">
<div class="layout">
<div class="side">Short</div>
<div class="main">1fr column</div>
</div>
<div class="layout">
<div class="side">This sidebar has much longer content, so it stops at 200px and wraps</div>
<div class="main">1fr column</div>
</div>
<div class="label">grid-template-columns: fit-content(200px) 1fr</div>
</div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* fit-content() examples */
.layout {
display: grid;
grid-template-columns: fit-content(200px) 1fr;
gap: 12px;
margin-bottom: 12px;
}

.side,
.main {
padding: 12px;
border-radius: 8px;
color: #fff;
font-weight: 600;
}

.side { background: #7c3aed; }
.main { background: #2563eb; }
Open in editor

Browser Support

The following information will show you the current browser support for the CSS fit-content() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.