CSS Intrinsic Sizing: min-content, max-content, and fit-content in Layouts

CSS intrinsic sizing: min-content, max-content and fit-content in layouts

The intrinsic sizing keywords let an element take its size from its content instead of a fixed number. min-content is the narrowest the content can be without overflowing (the longest word or the widest image), max-content is the width of the content on one line without any wrapping, and fit-content is in between: as wide as the content needs, but never wider than the available space. They work in width and height and, most usefully, in grid track sizes.

Problem: A centered navigation stretches to the full width, a table-like grid has columns that are either too wide or wrap the labels, and a sidebar needs a fixed width that is wrong for half of its content.

Solution: Use fit-content for elements that should hug their content, max-content and min-content for grid columns sized by their cells, and the fit-content() function for grid tracks with an upper limit.

The three keywords

/* As narrow as possible: wraps at every opportunity */
.tag-cloud__tag {
    width: min-content;
}

/* As wide as the content on one line: never wraps */
.tooltip {
    width: max-content;
    max-width: min( 90vw, 22rem ); /* but keep it readable */
}

/* Hugs the content, but wraps when there is not enough room */
.site-nav {
    display: flex;
    gap: 1rem;
    width: fit-content;
    margin-inline: auto; /* centered, without stretching */
}

The difference between max-content and fit-content shows on narrow screens: a max-content element keeps its width and overflows its parent, a fit-content element shrinks to the available width and wraps.

For a width limit on a fit-content element, combine it with max-width: width: fit-content; max-width: 20rem;.

In grid columns

This is where the keywords are most useful: a column can be exactly as wide as its widest cell.

/* A list like the WordPress admin tables: checkbox, title, author, date */
.post-list {
    display: grid;
    grid-template-columns: max-content 1fr max-content max-content;
    gap: 0.5rem 1.5rem;
}

/* A sidebar sized by its content, up to 300px, and the main column takes the rest */
.sidebar-layout {
    display: grid;
    grid-template-columns: fit-content( 300px ) minmax( 0, 1fr );
    gap: 2rem;
}

/* A label column as narrow as its longest word, the values beside it */
.spec-table {
    display: grid;
    grid-template-columns: min-content 1fr;
}

fit-content( 300px ) as a track size means “as wide as the content needs, but at most 300px”: a short menu gets a narrow sidebar, a long one is capped and wraps. The minmax( 0, 1fr ) keeps the main column from being pushed wider by long words or wide tables in the content.

With flexbox

In flex layouts the keywords decide how items share the space: a button group with flex: 0 0 auto keeps the buttons at their max-content width, and an item with min-width: 0 can shrink below its min-content width, which is the usual fix when long text overflows a flex item.

.card__meta {
    display: flex;
    gap: 0.5rem;
}

.card__meta .author {
    min-width: 0;          /* allow it to shrink */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.card__meta .date {
    flex: 0 0 auto;        /* never shrink the date */
}

NOTE: Intrinsic keywords cannot be used inside clamp(), min() or max(): clamp( min-content, 20vw, 240px ) is invalid and the whole declaration is ignored. Use min-width and max-width around them instead. More on grid tracks in CSS Grid basics and on components that adapt to their space in container queries.