CSS max-width Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Sizing

What You’ll Learn

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.

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.

📝 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.

Basic Example

max-width.css
.container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background-color: #f0f0f0;
}

Syntax Rules

  • The initial value is none, meaning no maximum width constraint.
  • Length values such as px, rem, and vw set a fixed upper limit.
  • Percentages are relative to the containing block’s width.
  • auto is not valid for max-width; use none to remove the cap.

⚡ Quick Reference

QuestionAnswer
Initial valuenone
Applies toAll elements
InheritedNo
AnimatableNo
Common useCentered containers, content columns, responsive layouts

🎯 Default Value

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.

💎 Property Values

These are the most common values you will use with max-width.

ValueExampleMeaning
lengthmax-width: 500px;Specifies a fixed maximum width in units such as px or rem
percentagemax-width: 80%;Maximum width as a percentage of the containing block’s width
nonemax-width: none;No maximum width constraint (default)
600px

Fixed pixel cap for centered containers.

Common for page content wrappers.

90%

Scales with the parent element width.

Great for responsive layouts.

none

No ceiling — content can grow freely.

Default browser behavior.

max-width vs max-inline-size

PropertyAxisBest for
max-inline-sizeInline axis (depends on writing mode)International layouts, reusable components, logical sizing systems
max-widthAlways horizontalSimple layouts with fixed maximum widths in horizontal writing
widthAlways horizontalPreferred width paired with max/min limits

👀 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.

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>

<div class="container">
  This container will not exceed 600 pixels in width.
</div>
Try It Yourself

How It Works

The container grows with its parent up to 600px wide, then stops. margin: 0 auto keeps it centered.

Example 2 — Percentage-Based Max Width (90%)

Cap width at 90% of the parent so the layout stays responsive on different screen sizes.

max-width-percent.html
<style>
  .container {
    max-width: 90%;
    margin: 0 auto;
    padding: 20px;
    background-color: #e0e0e0;
  }
</style>

<div class="container">
  Responsive container content…
</div>
Try It Yourself

How It Works

90% scales with the parent width, so the container shrinks on small screens and uses more space on large ones — up to the percentage cap.

📈 Responsive Maximums

Use viewport units and comparisons so width caps adapt across screen sizes.

Example 3 — Viewport-Relative Cap (90vw)

Limit width relative to the viewport so content never spans the entire window on ultra-wide monitors.

max-width-vw.css
.card {
  max-width: 90vw;
  margin: 0 auto;
  padding: 1.5rem;
}
Try It Yourself

How It Works

90vw means 90% of the viewport width. The cap shrinks on narrow screens and grows on wide ones.

Example 4 — Compare No Max vs Two Caps

See how the same content behaves with no limit, a tight cap, and a roomier cap.

max-width-compare.css
.none { max-width: none; }
.sm { max-width: 400px; }
.md { max-width: 600px; }
Try It Yourself

How It Works

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

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Opera All modern versions
Full support
max-width property 99% supported

Bottom line: You can rely on max-width in production layouts across all major browsers.

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.

💡 Best Practices

✅ Do

  • 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)

Key Takeaways

Knowledge Unlocked

Five things to remember about max-width

Use these points when capping horizontal width in layouts.

5
Core concepts
📝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.

Practice in the Live Editor

Open the HTML editor, set max-width, and preview capped scrollable content instantly.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful