CHAPTER 21

CSS Max-width

Learn how max-width controls the maximum size of an element and helps create responsive layouts.

01. What is CSS Max-width?

The max-width property defines the maximum width an element can have.
An element can become smaller than max-width when the available screen space is smaller.
max-width is especially useful for responsive websites.

02. Syntax

selector { max-width: value; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.container {

    width: 100%;

    max-width: 900px;

    margin: auto;

    padding: 30px;

    background: #dbeafe;

}

</style>

</head>

<body>

<div class="container">

    <h1>AIFA Infotech</h1>

    <p>
        This container will not become wider than 900px.
    </p>

</div>

</body>
</html>

04. Important Points

✓ max-width limits the maximum width of an element.
✓ It is useful for responsive containers.
✓ width: 100% with max-width is a common responsive pattern.
✓ margin: auto can center a fixed or maximum-width container.

05. Common Mistakes

⚠ Using a very small max-width that makes content difficult to read.
⚠ Forgetting responsive padding.
⚠ Using fixed widths instead of flexible widths on mobile.

06. Real World Use

max-width is commonly used for website containers, blog content, landing pages, cards and readable text sections.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a Responsive Container

Create a container with width: 100% and max-width: 1000px. Center it and add padding.