The CSS width property is used to set the width of an element. By default, the width property sets a fixed width. However, you can set the width to automatically adjust based on the content using the value "fit-content".
The fit-content value is extremely useful when you don‘t know the exact width of the content or when the content is dynamic. Using fit-content allows the width to shrink-wrap around the content.
In this comprehensive guide, we will cover everything you need to know about using the CSS width fit-content property, including:
- What is CSS Width Fit Content?
- Browser Support
- How to Use Width Fit Content
- Differences Between Width Auto and Fit Content
- Advantages and Disadvantages
- Use Cases
- Fit Content with Overflow
- Common Issues
What is CSS Width Fit Content?
The fit-content value was introduced in CSS3 as part of the CSS Intrinsic & Extrinsic Sizing module. When you set width: fit-content, the width will automatically adjust to fit the content inside the element.
Here is the syntax:
width: fit-content;
The fit-content value works similarly to width: auto. The key difference is that fit-content will shrink wrap the content while auto will expand to fill the parent container.
The element will take up only as much width as needed to display the content without any overflow. This allows the width to dynamically grow and shrink as you add, remove or change content.
Browser Support
The fit-content value has good browser support, but lacks full support in older browsers.
Browser | Version Support |
---|---|
Chrome | 59+ |
Firefox | 52+ |
Safari | 10.1+ |
Edge | 79+ |
IE | No support |
For full cross-browser support, you can implement a CSS width fit content polyfill using JavaScript.
How to Use Width Fit Content
Using width fit-content is extremely simple. You just set width: fit-content on the element you want to shrink wrap.
Here is an example:
.fit-content {
width: fit-content;
height: 100px;
border: 1px solid #333;
}
And the HTML:
<div class="fit-content">
<p>This container will shrink wrap my content.</p>
</div>
The container would adjust its width automatically based on the paragraph content inside of it.
If you added more content or longer text, the container would grow wider to fit it without overflowing. And if you reduce the content, the container shrinks down with it.
Differences Between Width Auto and Fit Content
At first glance, fit-content seems similar to setting width: auto. But there is an important distinction.
Using auto will make the element fill the entire width of its parent container, only shrinking when there is an explicit width set on the parent.
Fit-content skips filling the parent and instead shrink-wraps around the content itself.
Here is an example to see the difference:
HTML
<div class="parent">
<div class="auto">I will fill the parent width.</div>
<div class="fit-content">I will shrink wrap my content.</div>
</div>
CSS
.parent {
width: 500px;
}
.auto {
width: auto;
border: 1px solid blue;
}
.fit-content {
width: fit-content;
border: 1px solid red;
}
The auto div fills the full 500px width of the parent. The fit-content div only takes up as much width as needed to fit the text.
This distinction makes fit-content very useful for creating responsive components that size to their content.
Advantages of Using Width Fit Content
There are several advantages to using the fit-content value:
Dynamic Widths – Great for responsive designs where you don‘t know the exact content width. Fit content adjusts as you add or remove content.
Less CSS Dependencies – With fixed pixel widths, changing one element‘s width can cause layout breaks. Fit content isolates components, reducing CSS dependencies.
Better Accessibility – Screen readers don‘t have to parse large empty spaces. Fit content wraps content tightly keeping focus clear.
Intrinsic Ratios – Makes keeping height and width ratios much easier when used with properties like aspect-ratio.
Performance Gains – Eliminates unused space in the layout, allowing browsers to do less rendering work.
Disadvantages of Fit Content
The fit-content value also comes with some disadvantages to consider:
Limited Old Browser Support – Not supported in older browsers like IE without JavaScript polyfills.
More Difficult Horizontal Alignment – Since widths vary, aligning components horizontally takes a bit more work.
Flexible Layout Dependency – For fit-content to work well, your entire layout needs to use flexbox, grids or be designed responsively.
Not Valid for Block Formatting Context – Fit-content cannot be used on elements that establish block formatting contexts like floats or overflow: hidden.
Use Cases for Width Fit Content
There are a few common use cases where using fit-content for width shines:
Flexbox & CSS Grids
Fit content was designed to work hand-in-hand with modern CSS layout methods like Flexbox and CSS Grid.
By combining auto-sizing with flex and grid flexibility, you can create completely responsive interfaces without ever needing to set fixed widths anywhere.
Intrinsic Ratios
Maintaining height and width ratios on responsive elements can be tricky. Fit content combined with the aspect-ratio property provides an easy way to create true intrinsic ratios.
This Instagram embed is keeping a perfect square using this technique:
.instagram {
width: fit-content;
aspect-ratio: 1 / 1;
}
The width fit-content allows it to scale up and down while aspect-ratio handles the 1:1 ratio.
Responsive Images & Frames
Making images and other media fluidly responsive has traditionally required max-width: 100% which isn‘t ideal.
Using width: fit-content allows the image or video frame to scale responsively without distortion or overflow. Much easier!
Wrapping Text Content
You often want text content in articles or paragraphs to wrap, but have containers scale naturally.
Instead of trying to calculate maximum widths, allow the container to fit-content and text will wrap automatically creating balanced line lengths.
Dealing with Overflow and Fit Content Widths
One complication that can happen when using fit-content is overflow. If unaccounted for, content can overflow its container causing layout issues.
There are three main ways to handle overflow with fit-content widths:
Overflow Property
The easiest solution is to set overflow: auto or overflow: scroll. This will simply add scrollbars automatically if content overflows.
Max-Width Property
You can also set an explicit max-width as a fallback. This ensures content will scroll past a certain point instead of overflowing:
.fit-content {
width: fit-content;
max-width: 320px;
}
Now, once 320px is reached, scrolling will kick in rather than overflow.
Breakpoint & Media Queries
Another option is to use breakpoints and media queries. This allows you to specifically override the fit-content behavior at different screen sizes:
/* Mobile */
.fit-content {
width: fit-content;
}
/* Tablet & Up */
@media (min-width: 641px) {
.fit-content {
width: 80%;
}
}
Here, fit-content is used for mobile while a fixed 80% width is set for larger tablet & desktop views.
Common Problems with CSS Width Fit Content
There are also a few common pitfalls and issues to look out for when working with fit-content:
Block Formatting Contexts
Fit content widths do not work on elements that create block formatting contexts like floats, overflow: hidden and display: flow-root.
Lack of Support for max-width
Currently, you cannot combine fit-content widths with max-width percentages or viewport units. max-width only works with fixed pixel values.
Vertical Overflow Issues
With height, there is no equivalent “fit-content” value so vertical overflow of content can still happen even with a width that shrink-wraps.
Unexpected Breaking
Since fit-content depends on content to wrap well, one small DOM or content change can inadvertently break layouts if overflow is not handled properly.
Being aware of these limitations helps avoid and prevent common pain points with fit-content.
Conclusion
The width: fit-content value provides an enormously useful tool for creating responsive and content-driven layouts with less rigid fixed widths required.
It automatically sizes elements to their content width allowing for more adaptive interfaces. And fit-content can help reduce layout complexity and CSS dependencies.
With a good understanding of how it differs from auto, potential overflow issues, browser support status, and common use cases, you can leverage fit-content to improve resilience and flexibility in projects requiring dynamic widths.