CSS max-width Property
The CSS max-width property sets the maximum width an element is allowed to have.
It prevents an element from becoming wider than a specified limit while allowing it to remain narrower when necessary. The property is commonly used for responsive images, readable text containers, page layouts, cards, and other elements that need flexible width constraints.
max-width Syntax
The max-width property accepts a nonnegative length, percentage, intrinsic sizing value, or the none keyword.
Basic Syntax
selector {
max-width: 600px;
}
Relative Length
.container {
max-width: 70rem;
}
Percentage Value
.container {
max-width: 90%;
}
Viewport-Relative Width
.container {
max-width: 80vw;
}
Intrinsic Sizing
.container {
max-width: min-content;
}
.container {
max-width: max-content;
}
.container {
max-width: fit-content(600px);
}
No Maximum Width
.container {
max-width: none;
}
CSS-Wide Keywords
.container {
max-width: initial;
}
.container {
max-width: inherit;
}
.container {
max-width: unset;
}
.container {
max-width: revert;
}
.container {
max-width: revert-layer;
}
The initial value is none, meaning the property does not impose a maximum width.
Property Values
| Value | Description |
|---|---|
none |
Specifies no maximum width constraint. This is the initial value. |
<length> |
Sets a maximum width using units such as px, em, rem, or vw. |
<percentage> |
Sets a maximum width relative to the containing block's width. |
min-content |
Uses the intrinsic minimum-content width as the maximum. |
max-content |
Uses the intrinsic maximum-content width as the maximum. |
fit-content() |
Uses intrinsic sizing with the supplied available-space limit. |
stretch |
Constrains the element's margin box to the available width of its containing block. Browser support varies. |
initial |
Resets the property to none. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Elements that accept width constraints |
| Computed value | As specified, with relative lengths converted to absolute lengths |
| Animation type | By computed value type |
| Physical dimension | Width |
| Logical equivalent | max-inline-size in horizontal writing modes |
How It Works
The max-width property limits an element's used width. It does not automatically assign the specified width to the element.
Width Below the Maximum
.box {
width: 300px;
max-width: 500px;
}
The box remains 300 pixels wide because its specified width is below the maximum.
Width Above the Maximum
.box {
width: 700px;
max-width: 500px;
}
The maximum constrains the used width to 500 pixels.
Automatic Width
.box {
width: auto;
max-width: 500px;
}
The element's width is determined by its layout context, but it cannot exceed 500 pixels unless another sizing constraint takes precedence.
No Maximum Width
.box {
max-width: none;
}
The property imposes no upper width limit. Other layout rules can still constrain the element.
Important Consideration
A maximum width does not guarantee that the element will fit inside its container. Minimum widths, unbreakable content, padding, borders, and layout behavior can still cause overflow.
max-width vs. width
The width property sets an element's preferred width, while max-width establishes an upper limit.
Fixed Width
.box {
width: 600px;
}
The box has a preferred width of 600 pixels, which may cause horizontal overflow when the available space is smaller.
Maximum Width
.box {
max-width: 600px;
}
The box can remain narrower than 600 pixels depending on its layout context.
Responsive Width
.box {
width: 100%;
max-width: 600px;
box-sizing: border-box;
}
The box uses the available width until it reaches 600 pixels. On narrower screens, it can shrink with its containing block.
Comparison
| Property | Purpose |
|---|---|
width |
Sets the preferred physical width. |
min-width |
Sets the minimum permitted width. |
max-width |
Sets the maximum permitted width. |
Length and Percentage Values
Length and percentage values provide flexible ways to define maximum width constraints.
Pixels
.container {
max-width: 1200px;
}
The container cannot exceed 1200 pixels in width, subject to other applicable sizing constraints.
Root Font Units
.container {
max-width: 75rem;
}
The maximum width is calculated relative to the root element's font size.
Character Units
.article {
max-width: 65ch;
}
The ch unit is based on the advance width of the 0 glyph in the element's font. It is useful for approximating readable line lengths.
Viewport Width
.container {
max-width: 90vw;
}
The maximum width is 90% of the viewport width.
Percentage Width
.parent {
width: 800px;
}
.child {
max-width: 75%;
}
The child's maximum width is 600 pixels because the containing block is 800 pixels wide.
Calculated Maximum
.container {
max-width: calc(100% - 40px);
}
The maximum is calculated from the containing block's width minus 40 pixels.
Negative Values
.container {
max-width: -200px;
}
Negative lengths and percentages are invalid. Zero and positive values are permitted.
Intrinsic Sizing Values
Intrinsic sizing values use the content's natural size and the available layout space to determine the maximum width.
min-content
.box {
max-width: min-content;
}
The maximum is based on the minimum-content width. For text, this is often influenced by the longest unbreakable segment.
max-content
.box {
max-width: max-content;
}
The maximum is based on the width the content would prefer without soft wrapping.
fit-content()
.box {
max-width: fit-content(500px);
}
The fit-content() function uses intrinsic sizing limits while considering the specified available-space value.
stretch
.box {
max-width: stretch;
}
The stretch keyword constrains the element's margin box to the available width of its containing block. Browser support varies.
Important Consideration
Intrinsic sizing values can behave differently across layout contexts. Check compatibility for the specific keyword or function when supporting older browsers.
Minimum and Maximum Width
The min-width and max-width properties establish lower and upper width constraints.
Width Range
.panel {
min-width: 200px;
max-width: 600px;
}
The panel's used width is constrained between the specified minimum and maximum, subject to layout rules.
Preferred Width with a Maximum
.panel {
width: 800px;
max-width: 600px;
}
The maximum constrains the used width to 600 pixels.
Minimum Takes Precedence
.panel {
min-width: 500px;
max-width: 300px;
}
When the minimum exceeds the maximum, the minimum width takes precedence.
Responsive Maximum
.panel {
width: 100%;
max-width: min(100%, 600px);
box-sizing: border-box;
}
The panel can fill the available width without exceeding 600 pixels.
Box Sizing
.panel {
box-sizing: border-box;
max-width: 500px;
padding: 20px;
border: 2px solid #64748b;
}
With border-box, the maximum includes padding and borders. With content-box, the maximum applies to the content box.
Responsive Layouts
The max-width property is widely used to create layouts that expand on smaller screens while remaining comfortably sized on larger displays.
Centered Page Container
.page-container {
width: 100%;
max-width: 1200px;
margin-inline: auto;
box-sizing: border-box;
padding-inline: 20px;
}
The container can fill the available width up to 1200 pixels. Automatic inline margins center it when extra space is available.
Readable Article Width
article {
max-width: 65ch;
margin-inline: auto;
}
Limiting line length can improve readability by preventing excessively wide paragraphs.
Responsive Card
.card {
width: 100%;
max-width: 350px;
box-sizing: border-box;
}
The card can shrink on narrow screens while remaining no wider than 350 pixels.
Using min()
.content {
width: 100%;
max-width: min(90%, 70rem);
}
The maximum is whichever is smaller: 90% of the containing block or 70rem.
Using clamp()
.content {
max-width: clamp(300px, 80vw, 1000px);
}
The maximum width responds to the viewport while remaining between 300 and 1000 pixels. On very narrow screens, the 300-pixel lower bound can still be larger than the available space.
Media Query Adjustment
.content {
width: 100%;
max-width: 1000px;
}
@media (max-width: 767px) {
.content {
max-width: 100%;
}
}
Media queries can adjust maximum widths at different viewport sizes, although flexible percentage-based sizing often reduces the need for additional breakpoints.
Images and Media
Maximum width constraints are especially useful for preventing images and media from extending beyond their containers.
Responsive Image
img {
max-width: 100%;
height: auto;
}
The image can shrink to fit its containing block without exceeding its natural width when its width remains automatic.
Limit Image Width
.article-image {
max-width: 500px;
height: auto;
}
The image is constrained to a maximum width of 500 pixels while preserving its intrinsic aspect ratio under normal automatic sizing.
Responsive Video
video {
max-width: 100%;
height: auto;
}
This helps prevent a video element from exceeding the width of its container.
Image Inside a Fixed Frame
.image-frame {
width: 100%;
max-width: 500px;
aspect-ratio: 16 / 9;
overflow: hidden;
}
.image-frame img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}
The frame remains no wider than 500 pixels. The image fills the frame and may be cropped to maintain the specified aspect ratio.
Important Consideration
Using max-width: 100% does not automatically preserve an image's aspect ratio if conflicting width and height values are also specified. Use automatic sizing on the other axis when appropriate.
Logical Sizing
The max-width property always constrains the physical width. The logical max-inline-size and max-block-size properties adapt to the element's writing mode.
Physical Maximum Width
.box {
max-width: 500px;
}
This always limits the horizontal dimension.
Logical Maximum Inline Size
.box {
max-inline-size: 500px;
}
In horizontal writing modes, this corresponds to max-width. In vertical writing modes, it corresponds to max-height.
Logical Maximum Block Size
.box {
max-block-size: 500px;
}
In horizontal writing modes, this corresponds to max-height. In vertical writing modes, it corresponds to max-width.
Comparison
| Property | Horizontal Writing Mode | Vertical Writing Mode |
|---|---|---|
max-width |
Limits width | Limits width |
max-inline-size |
Limits width | Limits height |
max-block-size |
Limits height | Limits width |
Use physical sizing when a fixed horizontal dimension is required. Use logical sizing when the constraint should follow the writing mode.
Overflow Behavior
A maximum width constraint does not guarantee that all content will fit inside an element. Long unbreakable text, images, and minimum-size rules can cause horizontal overflow.
Long Text
.content {
max-width: 300px;
overflow-wrap: anywhere;
}
The overflow-wrap property allows long text segments to break when necessary.
Horizontal Scrolling
.code-panel {
max-width: 100%;
overflow-x: auto;
}
Horizontal scrolling allows wide content to remain accessible without forcing the entire page to expand.
Prevent Image Overflow
.content img {
max-width: 100%;
height: auto;
}
Images can shrink to fit their containers when their other sizing constraints allow it.
Flex and Grid Items
.flex-item {
min-width: 0;
max-width: 100%;
}
Some flex and grid items have automatic minimum sizes based on their content. Setting min-width: 0 can allow an item to shrink when otherwise prevented by its automatic minimum.
Important Consideration
Use scrolling or appropriate wrapping for content that cannot fit within a maximum width. Avoid clipping essential information without providing another way to access it.
max-width Example
This example compares two content boxes. The first uses the available width without an explicit maximum, while the second is limited to 280 pixels.
HTML Example
<div class="max-width-demo">
<div class="max-width-item">
<h3>max-width: none</h3>
<div class="max-width-box no-limit">
<p>This box can expand across the available width of its container.</p>
</div>
</div>
<div class="max-width-item">
<h3>max-width: 280px</h3>
<div class="max-width-box limited">
<p>This box cannot exceed 280 pixels in width.</p>
</div>
</div>
</div>
CSS Example
.max-width-demo {
display: grid;
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.max-width-item h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.max-width-box {
box-sizing: border-box;
width: 100%;
padding: 18px;
background-color: #dbeafe;
border: 2px solid #2563eb;
border-radius: 6px;
}
.max-width-box p {
margin: 0;
}
.no-limit {
max-width: none;
}
.limited {
max-width: 280px;
}
max-width: none
This box can expand across the available width of its container.
max-width: 280px
This box cannot exceed 280 pixels in width.
The first box uses the available width of its container. The second box stops growing at 280 pixels but can become narrower when less space is available.
Centered Responsive Container
<div class="responsive-container">
<h3>Responsive Content</h3>
<p>This container stays centered and does not exceed its maximum width.</p>
</div>
.responsive-container {
box-sizing: border-box;
width: 100%;
max-width: 700px;
margin-inline: auto;
padding: 24px;
background-color: #f0fdf4;
border: 1px solid #16a34a;
border-radius: 6px;
}
The container expands on smaller screens and remains centered when the available width exceeds 700 pixels.
Common Mistakes
Confusing max-width with width
.box {
max-width: 500px;
}
This does not force the box to become 500 pixels wide. It establishes a maximum.
Using Negative Values
.box {
max-width: -100px;
}
Negative lengths and percentages are invalid.
Conflicting Minimum and Maximum Widths
.box {
min-width: 500px;
max-width: 300px;
}
The minimum width takes precedence when it exceeds the maximum.
Ignoring Padding and Borders
.box {
max-width: 300px;
padding: 20px;
}
With the default content-box sizing, padding increases the total rendered width beyond the content-box maximum. Use box-sizing: border-box when the maximum should include padding and borders.
Assuming max-width Prevents All Overflow
Unbreakable content, images, and minimum-size constraints can still cause overflow. Use appropriate wrapping and sizing rules.
Using a Large Minimum Width on Small Screens
.box {
min-width: 600px;
max-width: 100%;
}
The minimum can force the element wider than the available space. Review minimum-size constraints when building responsive layouts.
Confusing Physical and Logical Sizing
max-width always limits physical width. Use max-inline-size when the maximum should follow the inline axis.
Expecting max-width to Center an Element
A maximum width does not automatically center a block. Use suitable margins or layout alignment when centering is required.
Important Considerations
Keep the following points in mind when using max-width:
- The property sets an upper limit on an element's physical width.
- Its initial value is
none. - The property is not inherited.
- It does not force an element to expand to the maximum width.
- Length and percentage values must be nonnegative.
- Percentage values are relative to the containing block's width.
- Intrinsic sizing keywords depend on content and layout context.
min-widthtakes precedence when its minimum exceeds the maximum.widthsets a preferred width, whilemax-widthconstrains it.box-sizingdetermines whether padding and borders are included in the specified maximum.- Maximum width constraints are useful for responsive page containers and cards.
- Using
max-width: 100%can help images fit within their containers. - Long unbreakable content can still cause overflow.
- Flex and grid items may require adjustments to automatic minimum sizes.
max-widthalways controls physical width, regardless of writing mode.- Use
max-inline-sizewhen the constraint should follow the logical inline axis. - Test layouts at narrow viewport widths and with enlarged text.
Browser Support
The CSS max-width property is Baseline - Widely available. It is supported in current versions of all major browsers, including Chrome, Edge, Firefox, and Safari.
Basic length, percentage, and none values have broad support. Newer intrinsic sizing values, such as stretch, may have different browser compatibility requirements.
For current compatibility information, visit Can I Use? and search for max-width.
When using newer sizing functions or keywords, check compatibility for the specific value rather than relying only on general property support.
Specifications
The max-width property is defined in CSS Sizing Module Level 3.
CSS Sizing Module Level 3: Maximum Size Properties
The specification defines maximum size constraints, accepted sizing values, and their relationship to minimum and preferred sizes.
For additional documentation and examples, consult MDN Web Docs.
