- fit-content
- Syntax
- Examples
- Using fit-content for box sizing
- HTML
- CSS
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- How to set div width to fit content using CSS?
- What is the div Element?
- Syntax
- Understanding the Basics of CSS box-sizing Property
- Set the Width of a div Element Using CSS
- Using max-content Value to set div Width Dynamically
- Example 1
- Max-Content Example
- Setting Width using fit-content Value for a Flexible Layout
- Example 2
- Fit-Content Example
- Using Auto Value to Automatically Adjust div Width Based on Content
- Example 3
- Using auto value to automatically adjust div width based on content
- Applying CSS Overflow Property to Handle Content Overflow
- Example 4
- Applying CSS overflow property to handle content overflow
- Conclusion
- width
- Try it
- Syntax
- Values
- Accessibility concerns
- Formal definition
- Formal syntax
- Examples
- Default width
- Example using pixels and ems
- Example with percentage
- Example using «max-content»
- Example using «min-content»
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
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.
Note: 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">Itemdiv> 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: fit-content; background-color: #8ca0ff; padding: 5px; margin-bottom: 1em; >
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
How to set div width to fit content using CSS?
CSS (Cascading Style Sheets) is a powerful tool that is used to style web pages and create dynamic layouts, including the width property. Using CSS, the width property of a ‘div’ can be easily customized to create unique designs that enhance the user experience.
What is the div Element?
A is a block-level element in HTML that is used for defining a section of the document. Large sections of HTML elements can be grouped together using the div tag and formatted with CSS.
By default, a div element expands to fill the width of its parent container. This means that if the parent container is wider than the content inside the div, the div will also be wider than its content.
Syntax
Understanding the Basics of CSS box-sizing Property
The box-sizing property controls how the width and height of an element are calculated. To calculate the width and height of an element based on a combination of its content, padding, and border, we can set the box-sizing property to border-box.
Set the Width of a div Element Using CSS
The most common way to set the width of a div element is to use the width property in CSS. For example, we can use the following CSS code to set the width of a div element to 300 pixels −
Here, we have created a div element with the class name my-div and set the width to 300px.
Now, we will discuss the following approaches of how to set div width to fit content using CSS −
- Using max-content value to set div width dynamically
- Setting width using fit-content value for a flexible layout
- Using auto value to automatically adjust div width based on content
- Applying CSS overflow property to handle content overflow
Using max-content Value to set div Width Dynamically
Using the max-content value, the width of a div element can be set dynamically based on its content. The max-width CSS property sets the maximum width of an element while ignoring any specified width properties. To use the max-content value, we can use the following CSS rule −
This code will set the width of the div element to the maximum width of its content.
Example 1
Setting the width of a div to fit the content using the max-content value.
h2 < text-align: center; >.maxMax-Content Example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit euismod, vehicula felis et, faucibus enim. Sed sit amet arcu nunc.
Setting Width using fit-content Value for a Flexible Layout
Using the fit-content value, the width of a div element can be set to fit its content. The fit-content CSS property sets the width of an element to the minimum width of its content and allows the element to expand based on its parent container’s width. To use the fit-content value, we can use the following CSS rule.
This code will set the width of the div element to the minimum width of its content. Changing the content inside the div element will also adjust the width of the element accordingly.
Example 2
Setting the width of a div to fit the content using the fit-content value.
h2 < text-align: center; >.fitFit-Content Example
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit euismod, vehicula felis et, faucibus enim. Sed sit amet arcu nunc.
Using Auto Value to Automatically Adjust div Width Based on Content
Using the auto value, the width of the div element can be set to fit its content. The auto value sets the width of an element to the width of its content, and allows the element to expand based on its parent container’s width. To use the auto value, we can use the following CSS rule.
This code will set the width of the div element to the width of its content. Changing the content inside the div element will also adjust the width of the element accordingly.
Example 3
Setting the width of a div to fit the content using the ‘auto’ value.
h2 < text-align: center; >.autoUsing auto value to automatically adjust div width based on content
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit euismod, vehicula felis et, faucibus enim. Sed sit amet arcu nunc.
Applying CSS Overflow Property to Handle Content Overflow
The overflow property controls the content inside an element overflows its boundaries. There are several values that can be used with the overflow property, including visible, hidden, scroll, and auto. To prevent overflow of content in a div element, we can set the overflow property to scroll. This will scroll any content that overflows the boundaries of the div element. for doing this, we can use the following CSS rule −
Example 4
Setting the width of a div to fit the content and handle overflow using the overflow property.
h2 < text-align: center; >.scrollApplying CSS overflow property to handle content overflow
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit euismod, vehicula felis et, faucibus enim. Sed sit amet arcu nunc. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel velit euismod, vehicula felis et, faucibus enim. Sed sit amet arcu nunc.
Conclusion
In this article we have discussed the several approaches to set div width to fit content using CSS such as max-content value, fit-content value, and auto value. So setting the width of a div element to fit its content is a simple and effective way to ensure that the website looks great.
width
The width CSS property sets an element’s width. By default, it sets the width of the content area, but if box-sizing is set to border-box , it sets the width of the border area.
Try it
The specified value of width applies to the content area so long as its value remains within the values defined by min-width and max-width .
- If the value for width is less than the value for min-width , then min-width overrides width .
- If the value for width is greater than the value for max-width , then max-width overrides width .
Syntax
/* values */ width: 300px; width: 25em; /* value */ width: 75%; /* Keyword values */ width: max-content; width: min-content; width: fit-content(20em); width: auto; /* Global values */ width: inherit; width: initial; width: revert; width: revert-layer; width: unset;
Values
Defines the width as a distance value.
Defines the width as a percentage of the containing block’s width.
The browser will calculate and select a width for the specified element.
The intrinsic preferred width.
The intrinsic minimum width.
Uses the fit-content formula with the available space replaced by the specified argument, i.e. min(max-content, max(min-content, )) .
Accessibility concerns
Ensure that elements set with a width aren’t truncated and/or don’t obscure other content when the page is zoomed to increase text size.
Formal definition
Initial value | auto |
---|---|
Applies to | all elements but non-replaced inline elements, table rows, and row groups |
Inherited | no |
Percentages | refer to the width of the containing block |
Computed value | a percentage or auto or the absolute length |
Animation type | a length, percentage or calc(); |
Formal syntax
width =
auto |
|
min-content |
max-content |
fit-content( )
=
|
Examples
Default width
p class="goldie">The Mozilla community produces a lot of great software.p>
Example using pixels and ems
.px_length width: 200px; background-color: red; color: white; border: 1px solid black; > .em_length width: 20em; background-color: white; color: red; border: 1px solid black; >
div class="px_length">Width measured in pxdiv> div class="em_length">Width measured in emdiv>
Example with percentage
.percent width: 20%; background-color: silver; border: 1px solid red; >
div class="percent">Width in percentagediv>
Example using «max-content»
p.maxgreen background: lightgreen; width: intrinsic; /* Safari/WebKit uses a non-standard name */ width: -moz-max-content; /* Firefox/Gecko */ width: -webkit-max-content; /* Chrome */ width: max-content; >
p class="maxgreen">The Mozilla community produces a lot of great software.p>
Example using «min-content»
p.minblue background: lightblue; width: -moz-min-content; /* Firefox */ width: -webkit-min-content; /* Chrome */ width: min-content; >
p class="minblue">The Mozilla community produces a lot of great software.p>
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 18, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.