The max-width property sets the maximum horizontal width of an element. It is especially useful for creating flexible layouts that adapt to various screen sizes while maintaining design integrity.
01
Width Cap
Set an upper limit.
02
none Default
No maximum by default.
03
Pixels / %
Fixed and percentage caps.
04
Percentages
Relative to parent.
05
Overflow
Scroll when capped.
06
Related
width, min-width.
Fundamentals
Introduction
The max-width property in CSS is used to set the maximum width of an element. It defines the upper limit of the width an element can grow to, allowing it to be responsive while preventing it from exceeding a specified size.
This property is particularly useful for creating flexible layouts that adapt to various screen sizes while maintaining design integrity.
Definition and Usage
max-width always limits horizontal width in normal horizontal layouts. The element can be narrower than the limit, but it will not grow wider once it reaches the maximum.
Pair max-width with margin: 0 auto to center containers, and use it alongside width and min-width for full horizontal sizing control.
💡
Beginner Tip
Think of max-width as a ceiling: the box can be narrower than the limit, but it cannot grow wider once it hits the maximum.
Foundation
📝 Syntax
The syntax for the max-width property is as follows:
syntax.css
element{max-width:value;}
Here, value can be a length (e.g., pixels, ems) or a percentage.
The default value of the max-width property is none. This means that, by default, there is no maximum width constraint applied, and the element can expand based on its content and other layout factors.
Reference
💎 Property Values
These are the most common values you will use with max-width.
Value
Example
Meaning
length
max-width: 500px;
Specifies a fixed maximum width in units such as px or rem
percentage
max-width: 80%;
Maximum width as a percentage of the containing block’s width
none
max-width: none;
No maximum width constraint (default)
600px
Fixed pixel cap for centered containers.
600px max
Common for page content wrappers.
90%
Scales with the parent element width.
90% max
Great for responsive layouts.
none
No ceiling — content can grow freely.
No max
Default browser behavior.
Scope
Width, min-width, and max-width
Use physical width properties together when you want predictable horizontal sizing in standard layouts.
width sets the preferred or fixed width of the box.
min-width sets the lower limit so content never shrinks below a minimum.
max-width sets the upper limit on the same horizontal axis.
For writing-mode-aware layouts, see max-inline-size on the logical inline axis.
Compare
max-width vs max-inline-size
Property
Axis
Best for
max-inline-size
Inline axis (depends on writing mode)
International layouts, reusable components, logical sizing systems
max-width
Always horizontal
Simple layouts with fixed maximum widths in horizontal writing
width
Always horizontal
Preferred width paired with max/min limits
Preview
👀 Live Preview
Three boxes with the same long text but different max-width caps:
none — grows with all content.
12rem max — Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
20rem max — Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
These values cap horizontal width. Scrollbars appear when content exceeds the limit.
Hands-On
Examples Gallery
Try max-width with centered containers, percentage caps, viewport-relative limits, and side-by-side comparisons.
📚 Maximum Width Caps
Keep containers and content areas from stretching too wide on large screens while staying responsive on smaller ones.
Example 1 — 600px Centered Container
Set a 600px maximum width and center the container with margin: 0 auto.
max-width-600.html
<style>.container{max-width:600px;margin:0 auto;padding:20px;background-color:#f0f0f0;}</style><divclass="container">
This container will not exceed 600 pixels in width.
</div>
Without a maximum, the box expands to fill available width. Caps keep content within predictable boundaries.
🧠 How max-width Works
1
You declare a maximum width
Choose a length, percentage, or none to remove the cap entirely.
CSS rule
2
Content fills the box horizontally
The element grows with its container up to the limit you set.
Box model
3
Growth stops at the ceiling
The box can stay narrower than max-width, but it cannot grow wider once it hits the maximum.
Constraint
=
Responsive layout stays controlled
On smaller screens the box shrinks below the maximum, staying flexible and readable.
Result
Compatibility
🖥 Browser Compatibility
The max-width property is universally supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. Older versions of Internet Explorer also support basic max-width values.
✓ Baseline · Universal support
Fundamental layout property
max-width has long been part of CSS layout and works consistently across browsers, much like width.
99%Browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
OperaAll modern versions
Full support
max-width property99% supported
Bottom line: You can rely on max-width in production layouts across all major browsers.
Wrap Up
Conclusion
The max-width property is a fundamental tool in responsive web design, helping you control the maximum width of elements and maintain a flexible layout.
By setting appropriate values for max-width, you can ensure that your website remains visually appealing and functional across different devices and screen sizes. Experiment with pixels, percentages, and viewport units to find the best fit for your design needs.
Pair max-width with margin: 0 auto when centering fixed-width containers
Use it alongside width and min-width for full horizontal sizing control
Combine max-width with percentage values like 90% for fluid layouts
Test layouts on mobile and ultra-wide screens
Prefer max-width for everyday layouts with broad browser support
❌ Don’t
Set max-width without a plan for narrow viewports
Confuse max-width with width — one sets a ceiling, the other sets preferred size
Use auto as a value for max-width (it is not valid)
Assume max-width always equals max-inline-size in every layout
Rely on max-width alone when layouts change writing mode (use max-inline-size for logical sizing)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about max-width
Use these points when capping horizontal width in layouts.
5
Core concepts
📏01
Maximum Cap
Sets the ceiling.
Purpose
📝02
none Default
No maximum limit.
Default
🔄03
Horizontal Axis
Always horizontal.
Context
📈04
Overflow Pair
Scroll when capped.
UX
🛠05
Height Trio
width, min, max.
System
❓ Frequently Asked Questions
The max-width property sets the maximum horizontal width of an element. The box can be narrower than the limit, but it cannot grow wider once it reaches the maximum.
The default value is none, which means there is no maximum width restriction and the element can grow with its content and container.
width sets a preferred or fixed size. max-width sets an upper limit only — the element can shrink below it when the container is narrower.
Common values include none, length units like px, em, rem, and vw, and percentages relative to the containing block width.
No. auto is not a valid value for max-width. Use none to remove the cap or a length/percentage to set a maximum.