Width, Height, Max-Width, and Min-Width0%

Width, Height, Max-Width, and Min-Width

Beginner9 min readUpdated: 2026-09-10
Study Materials

Width, Height, Max-Width, and Min-Width

When building a website, one of your daily tasks is deciding how big or small your cards, sidebars, images, and text containers should be. 📐

Should a profile card have a fixed width of 300px? What happens when a student opens that same page on a small smartphone with a narrow 360px screen? Does the box spill off the screen causing an ugly horizontal scrollbar?

In this lesson, you will master the dimension properties that make websites flexible, modern, and mobile-friendly:

  1. 1
    width and height
  2. 2
    max-width and min-width
  3. 3
    min-height and the danger of fixed heights
  4. 4
    Centering containers with margin: 0 auto

The School Backpack Analogy 🎒

To visualize the difference between fixed width and max-width, think of your school lunchbox:

Visual Architecture Blueprint
+-------------------------------------------------------------------------+
|                  FIXED WIDTH vs RESPONSIVE MAX-WIDTH                    |
+-------------------------------------------------------------------------+
| 1. FIXED WIDTH (width: 400px) --> RIGID STEEL LUNCHBOX                  |
|    • Takes up exactly 400px of space on every single screen.            |
|    • On a 360px mobile screen, it overflows the bag and tears the zip!  |
|                                                                         |
| 2. RESPONSIVE (width: 100%; max-width: 400px) --> ELASTIC CLOTH POUCH  |
|    • On a wide study table, it expands comfortably up to 400px max.    |
|    • In a narrow school bag, it smoothly compresses to fit inside!      |
+-------------------------------------------------------------------------+

1. width and height (Basic Dimensions)

The width and height properties set the horizontal and vertical size of an element.

Units You Can Use:

  • Pixels (px): Fixed absolute size (width: 300px;).
  • Percentages (%): Relative to the size of the parent container (width: 50%;).
CSS
/* Takes half the width of its parent element */
.sidebar {
width: 50%;
}
 
/* Fixed size icon button */
.icon-btn {
width: 48px;
height: 48px;
}

2. The Danger of Fixed height ⚠️

Here is a golden rule followed by professional frontend developers:

Almost never set a fixed height on containers that hold text!

Why?

On a wide laptop monitor, 3 sentences of text might fit neatly in a single horizontal line inside a height: 60px box. But when a user views that same website on their mobile phone, the screen is narrow! The 3 sentences wrap into 6 lines. If the box is locked at height: 60px, the text will spill out of the box and overlap other elements below it!

Output
[ Laptop Screen ] [ Narrow Mobile Screen ]
+-------------------------+ +---------------+
| Welcome to our annual | | Welcome to our|
| sports tournament 2026. | | annual sports |
+-------------------------+ +---------------+
| tournament | ◄── Text spills out of
| 2026. Collect | fixed height box!
| your passes. |

The Solution: Let Height Be Natural (height: auto) or Use min-height!

By default, block elements have height: auto. This allows the box to expand downwards naturally as more content is added!

If you want a box to have a minimum starting size, use min-height:

CSS
.card {
min-height: 200px; /* Starts at least 200px tall, but expands if text grows! */
}

3. max-width (The Secret to Responsive Layouts ⭐)

max-width defines the maximum upper limit for how wide an element can stretch.

CSS
.container {
width: 100%;
max-width: 900px;
}

How Does This Work?

  1. 1
    On a giant 24-inch desktop screen (1920px wide):

The container expands up to 900px and stops. This prevents lines of text from stretching all the way across the monitor, which is tiring for human eyes to read!

  1. 1
    On an iPad or Tablet (768px wide):

Because width: 100% is active, the container seamlessly shrinks down to 768px.

  1. 1
    On a smartphone (360px wide):

The container smoothly shrinks to 360px without causing any horizontal scrollbars!


4. Centering Containers with margin: 0 auto

How do you take a container with a max-width and place it in the exact middle of the screen?

In CSS, the standard trick is: margin: 0 auto;

CSS
.main-content {
width: 100%;
max-width: 800px;
margin: 0 auto; /* 0 for top/bottom, auto for left/right */
}

How Does auto Work?

When you give an element a defined width (like 800px) and set its horizontal margins to auto, the browser calculates the remaining empty space on the screen, divides it equally into two halves, and assigns equal margins to both the left and right sides. The element ends up perfectly centered!

Visual Architecture Blueprint
+-------------------------------------------------------------+
|                      SCREEN (1400px)                        |
|                                                             |
|   [ margin-left: auto ]  +---------------+  [ margin-right ]|
|          (300px)         | CARD (800px)  |      (300px)     |
|                          +---------------+                  |
+-------------------------------------------------------------+

5. min-width (Preventing Elements from Collapsing)

min-width sets the smallest possible width an element can shrink to.

A common example is a website action button:

CSS
.btn {
min-width: 120px; /* Ensures short words like "OK" still look like a solid button */
padding: 10px 20px;
}

Even if the button text is only two letters (OK), the button will maintain a comfortable, clickable width of at least 120px.


Summary Comparison of Dimension Properties

PropertyMeaningCommon Use Case
widthExact width of the elementFixed icons, grid columns
max-widthElement cannot grow wider than this limitResponsive containers, images (max-width: 100%)
min-widthElement cannot shrink narrower than thisAction buttons, form inputs
height: autoHeight adjusts naturally to fit all contentText cards, article bodies
min-heightElement cannot shrink shorter than thisHero banners, empty dashboard widgets
margin: 0 autoCenters a block element horizontallyMain page layouts, modal dialogs

Common Beginner Mistakes (Do's and Don'ts)

What Beginners Do (Don't ❌)What You Should Do Instead (Do ✅)Why?
Setting a fixed height: 100px on a card with dynamic text.Use min-height: 100px; or height: auto;.Fixed height causes text overflow and overlapping on mobile screens.
Using width: 800px without max-width: 100%.Use width: 100%; max-width: 800px;.Fixed width breaks mobile screens smaller than 800px.
Trying to center an element with margin: 0 auto without setting a width or max-width.Always define a width (e.g. max-width: 600px).Without a defined width, block elements naturally stretch to 100%, leaving no empty space to balance!

Quick Revision Summary

  • width sets fixed or percentage horizontal size.
  • Avoid fixed height on text containers; prefer height: auto or min-height.
  • The combination of width: 100% and max-width: ... creates responsive containers that adapt to any screen size.
  • margin: 0 auto centers a block element horizontally by splitting remaining space equally between left and right.
  • min-width guarantees an element will never shrink below a usable size.

Practice Quiz

Test your understanding with these multiple-choice questions:

1. What is the main risk of setting a fixed pixel height on a paragraph container (e.g., height: 80px;)?

A. The font family will not load B. On smaller or mobile screens, text may wrap and spill out over other elements C. The web browser will crash D. The internet connection will become slow Answer: B Explanation: Fixed heights do not expand when text wraps into multiple lines on narrow screens, causing the content to overflow and overlap neighboring elements.


2. Which CSS property pair makes a card stretch on small screens but stop growing on large screens?

A. width: 100%; max-width: 600px; B. width: 600px; min-width: 100%; C. height: 100%; max-height: 600px; D. min-width: 600px; max-width: 100px; Answer: A Explanation: width: 100% allows the card to adapt to mobile screens, while max-width: 600px ensures it never stretches wider than 600px on large displays.


3. In the declaration margin: 0 auto;, what does the value auto accomplish?

A. It automatically sets the text color B. It calculates equal left and right margins, centering the element horizontally C. It makes the webpage scroll automatically D. It resets all borders to zero Answer: B Explanation: Setting horizontal margins to auto instructs the browser to divide the remaining space equally between the left and right sides, centering the element.


4. Why must an element have a specified width or max-width for margin: 0 auto to work?

A. Because CSS requires at least two properties in every rule B. Because without a width, a block element naturally expands to 100% width, leaving no leftover space to distribute C. Because auto only works with pixel values D. Because the browser will throw an error Answer: B Explanation: Block elements naturally take up 100% of available width. If there is no leftover horizontal space, auto margins evaluate to zero and centering cannot occur.


5. What does setting min-height: 250px; on a dashboard card guarantee?

A. The card will always be exactly 250px and never grow B. The card will start at least 250px tall, but can freely expand taller if more content is added C. The card can never be taller than 250px D. The card's width will be 250px Answer: B Explanation: min-height establishes a minimum height floor. If the content needs more than 250px, the box expands naturally to accommodate it.


Practice Challenge (Try It Yourself)

  1. 1
    Open your code editor and create responsive-card.html.
  2. 2
    Build a modern, responsive centered student profile card:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Responsive Container Challenge</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
 
body {
font-family: Arial, sans-serif;
background-color: #f1f5f9;
padding: 20px;
}
 
/* Responsive Centered Card */
.profile-card {
width: 100%;
max-width: 480px; /* Stops at 480px on desktop */
margin: 40px auto; /* Centered on the page */
background-color: white;
border-radius: 12px;
padding: 24px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
}
 
.profile-card h2 {
color: #1e3a8a;
margin-bottom: 8px;
}
 
.profile-card p {
color: #475569;
line-height: 1.6;
margin-bottom: 16px;
}
 
.action-btn {
min-width: 140px; /* Minimum button width */
background-color: #2563eb;
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
}
</style>
</head>
<body>
<div class="profile-card">
<h2>Aryan Gupta</h2>
<p>Class: 11th Science | Roll No: 1042</p>
<p>Aryan is representing our school in the National Cyber Olympiad 2026. Wish him the best of luck!</p>
<button class="action-btn">View Profile</button>
</div>
</body>
</html>
  1. 1
    Open the file in your browser and resize your browser window from wide to narrow:
  • Notice how on desktop, the card sits comfortably in the center.
  • On mobile screen widths, the card compresses smoothly without any horizontal scrollbars! 🎯
Next Lesson

The Box-Sizing Property: border-box vs content-box

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

PrevNext