CSS Max-Width Get Certified
What is CSS Max-Width?
The CSS max-width property sets the maximum width an element is allowed to grow to. Unlike a fixed width, max-width lets the element shrink on smaller screens while never becoming wider than the value you set. This makes it essential for responsive layouts.
Why Use CSS Max-Width?
Max-width keeps content readable, prevents overflow, and creates flexible containers that adapt to different screen sizes. It is commonly used for page wrappers, cards, images, and text columns.
Syntax of CSS Max-Width
What this code does: Limits a blue box so it never grows wider than 500px, while still allowing it to shrink on narrow screens.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
max-width: 500px;
background: #007BFF;
color: white;
padding: 20px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-box">This box will never become wider than 500px.</div>
</body>
</html>
Step 2: Center a Container Using Max-Width
A common use of max-width is to create centered webpage containers using margin:auto.
What this code does: Centers a green container on the page by combining max-width: 700px with margin: auto.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-center {
max-width: 700px;
margin: auto;
background: #28a745;
color: white;
padding: 20px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-center">This container stays centered on the page.</div>
</body>
</html>
Step 3: Responsive Images Using Max-Width
Images often use max-width:100% so they never overflow their parent container.
What this code does: Keeps an image inside a 350px container by setting max-width: 100% and height: auto.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-img-wrap img {
max-width: 100%;
height: auto;
display: block;
}
.demo-img-wrap {
max-width: 350px;
border: 2px solid #333;
padding: 10px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-img-wrap">
<img src="https://picsum.photos/600/350" alt="Sample Image">
</div>
</body>
</html>
Step 4: Responsive Text Container
Long paragraphs become easier to read when their width is limited using max-width.
What this code does: Limits a text block to 600px and centers it so lines stay comfortable to read.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-text {
max-width: 600px;
margin: auto;
padding: 20px;
border: 1px solid #ddd;
background: #f8f9fa;
border-radius: 6px;
line-height: 1.6;
}
</style>
</head>
<body>
<div class="demo-text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. The text remains
easy to read because the container width is limited.
</div>
</body>
</html>
Step 5: Difference Between Width and Max-Width
The following example demonstrates why max-width is more responsive than a fixed width.
What this code does: Compares a fixed 500px width box (red) with a max-width 500px box (blue). On narrow screens the max-width box shrinks while the fixed-width box can overflow.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-fixed {
width: 500px;
background: #dc3545;
color: white;
padding: 15px;
margin-bottom: 15px;
border-radius: 6px;
}
.demo-max {
max-width: 500px;
background: #007BFF;
color: white;
padding: 15px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-fixed">Fixed Width: 500px</div>
<div class="demo-max">Maximum Width: 500px</div>
</body>
</html>
Complete CSS Max-Width Example
What this code does: Builds a centered responsive card with a flexible image, text, and button — all constrained with max-width.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
background: #f5f9ff;
padding: 24px;
}
.demo-card {
max-width: 700px;
margin: auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,.15);
}
.demo-card img {
max-width: 100%;
height: auto;
display: block;
border-radius: 6px;
}
.demo-card button {
padding: 12px 20px;
background: #007BFF;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
.demo-card button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="demo-card">
<h2>Responsive Card</h2>
<img src="https://picsum.photos/700/300" alt="Responsive Image">
<p>This layout automatically adjusts on different screen sizes.</p>
<button>Learn More</button>
</div>
</body>
</html>
Advantages of CSS Max-Width
Best Practices
- Use max-width for responsive layouts.
- Combine max-width with margin:auto to center containers.
- Use max-width:100% for responsive images.
- Use height:auto with images.
- Avoid fixed widths whenever possible.
- Test layouts on desktop, tablet, and mobile devices.
- Choose an appropriate maximum width for readable content.
- Use responsive units whenever suitable.
Web Resources on CSS Max-Width
1. MDN Web Docs : max-width CSS property
2. CSS Tricks.com : A Tale of `width` and `max-width`
3. Dev.to : CSS Max-Width Explained, Stop Breaking Your Layout
4. MDN Web Docs : max() CSS function
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.