Thanks to visit codestin.com
Credit goes to www.cssportal.com

CSS Portal

CSS max-width Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The max-width property establishes an upper limit on an element’s used width, ensuring the box will not be laid out wider than that limit even if its container or intrinsic content would normally allow it to expand. It acts as a clamp during the layout process: if the element’s natural or specified size would exceed this limit, the final used width is reduced to the maximum. This makes the property a simple but powerful tool for preventing oversized elements and preserving visual rhythm across varying viewport sizes.

Because it participates in the browser’s sizing resolution, max-width interacts closely with other sizing constraints. When combined with a specified width and a min-width, the final used width must satisfy the set of constraints together (the layout system resolves them so the used width does not violate the minimum or exceed the maximum). The effective measurement of the constraint depends on how the box model is interpreted, so differences in whether the element’s width includes decorations or not can change the apparent effect of the upper bound. Percentage-based upper bounds are resolved relative to the element’s containing block, so they scale with available space rather than remaining absolute.

In practice, authors use max-width to create responsive, robust designs: to cap text column measure for readability, prevent images and media from breaking fixed-width layouts, and allow elements to shrink without ever growing past a sensible limit. When content exceeds the constrained width, the treatment of that overflow is governed by the element’s overflow handling, so you should consider overflow when combining behaviors. Also bear in mind that the property remains an upper limit inside different layout contexts (normal flow, flex, grid), but the container’s layout algorithm and the element’s role in that algorithm - for example as a child of a complex layout - can influence the final computed size; it’s useful to keep the container’s display behavior in mind when predicting outcomes.

A few practical tips: use max-width to make media scale down gracefully while allowing them to grow to a sensible maximum, and pair it with careful container planning so elements don’t produce awkward whitespace or overflow. Because the property only limits growth and does not force enlargement, it is often paired conceptually with other sizing rules to produce predictable, adaptive interfaces.

Definition

Initial value
none
Applies to
All elements except non-replaced inline elements and table elements
Inherited
No
Computed value
The percentage as specified or the absolute length or 'none'
Animatable
Yes
JavaScript syntax
object.style.maxWidth

Interactive Demo

Change the maximum width.

Syntax

max-width: [ [<length> | <percentage>] && [border-box | content-box]? ] | available | min-content | max-content | fit-content | none

Values

  • <length>Specifies a fixed width. Negative values are not allowed.
  • <percentage>A percentage relative to the width of the containing block. If the containing block has no width explicitly set then is is treated as none. Negative values are not allowed.
  • max-contentThe max-content width or height.
  • min-contentThe min-content width or height.
  • availableThe containing block width or height minus margin, border, and padding.
  • fit-contentIf the total available space is finite, equals to min(max-content, max(min-content, fill-available)). Otherwise, equal to the max-content measure. Requires CSS Intrinsic & Extrinsic Sizing Module support in browsers.
  • noneThe width has no maximum value.

Example

<div class="wrapper">
<h1>CSS max-width example</h1>
<p class="intro">Resize the browser window to see how the container and image respect their max-width settings.</p>
<img src="https://placehold.co/1200x400" alt="Wide placeholder image">
<p>This paragraph sits inside a centered container with max-width applied. The image above uses max-width: 100% so it will scale down to fit the container while preserving its aspect ratio.</p>
<div class="card">
<h2>Card</h2>
<p>This card has a fixed max-width of 300px, so it won't grow wider than that even on large screens.</p>
</div>
</div>
/* Basic page styles */
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
  line-height: 1.5;
  padding: 2rem;
  background: #f6f8fa;
  color: #111;
}

/* Centered wrapper with a maximum width */
.wrapper {
  max-width: 900px;
  margin: 0 auto;
  background: #fff;
  padding: 1.5rem;
  border: 1px solid #e1e4e8;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04);
}

/* Make images responsive: they won't exceed their container */
.wrapper img {
  display: block;
  width: 100%;
  max-width: 100%;
  height: auto;
  border-radius: 4px;
  margin: 1rem 0;
}

/* A small card demonstrating a fixed maximum width */
.card {
  max-width: 300px;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  background: #fbfbfd;
}

/* Small tweaks for headings */
h1 {
  margin: 0 0 0.5rem 0;
  font-size: 1.5rem;
}

.card h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1rem;
}

Browser Support

The following information will show you the current browser support for the CSS max-width property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property 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
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!