CSS Sizing: Width & Height

Learn to control the dimensions of your elements and master the all-important `box-sizing` property for predictable, responsive layouts.

Lesson ProgressStep 1 of 10
Box
0 EXP

Welcome! Let's learn to control element dimensions. Here is a simple box.

.box {
  background: #6366F1;
}

The `width` Property

The `width` property controls the horizontal size of an element. You can use **absolute units** like pixels (`px`) for a fixed size, or **relative units** like percentages (`%`) for a flexible, responsive size.

.fixed { width: 300px; }
.fluid { width: 50%; }

A percentage-based width is relative to the width of the element's **parent container**.

System Check

If a parent container is 800px wide, how wide will a child element with `width: 50%;` be?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

📏
Sizing Starter

Set a fixed width and height on an element.

🌊
Responsive Whiz

Use relative units and min/max properties for a fluid layout.

📦
Box Model Expert

Master the layout-saving `box-sizing: border-box`.

Mission: Build a Responsive Box

Create a CSS rule for a class named `.my-box`. Give it a `width` of `80%`, a `max-width` of `600px`, and set `box-sizing` to `border-box`.

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Box Model

Drag the box model properties into the correct order, from the **inside** (content) to the **outside**.

border
width & height (content)
margin
padding

Challenge: Complete the Syntax

Fill in the missing values to make a full-width, full-height hero section that is *at least* 600px tall.

.hero {
width:;
height:;
min-height:;
}

Consult A.D.A.

Community Holo-Net

The Architect's Toolkit: Mastering CSS Sizing

In web design, sizing is the foundation of layout. It's the blueprint that dictates how elements occupy space, interact with each other, and adapt to different screens. While `width` and `height` are the basic tools, mastering layout requires understanding **how** these properties are calculated and **when** to use flexible alternatives.

The Box Model Problem: `content-box` vs. `border-box`

By default, CSS uses the `content-box` model. This means when you set `width: 200px`, only the **content area** is 200px wide. Any `padding` or `border` you add is tacked onto the *outside*, increasing the element's total visible width. This is often counter-intuitive and leads to broken layouts, especially when using percentages.

Enter `box-sizing: border-box;`. This property changes the calculation. When you set `width: 200px`, the **total width** of the element, including `padding` and `border`, will be 200px. The browser automatically shrinks the content area to make room. This is vastly more predictable.

❌ Default (`content-box`)

.box {
  width: 200px;
  padding: 20px;
  border: 5px solid;
}

Total Width: 200px (content) + 40px (padding) + 10px (border) = 250px

✔️ Best Practice (`border-box`)

.box {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 5px solid;
}

Total Width: 200px

It is so common to use `border-box` that many developers apply it to every element on the page with this universal snippet:

html {
  box-sizing: border-box;
}

*, *::before, *::after {
  box-sizing: inherit;
}

Fluid vs. Fixed: Building Responsive Layouts

A **fixed width** (e.g., `width: 800px;`) is brittle. It will create horizontal scrollbars on mobile devices. A **fluid width** (e.g., `width: 100%;`) is better, as it adapts to its parent.

The best approach combines fluidity with control. Use `width: 100%;` to make an element fill its container, but add a `max-width` (e.g., `max-width: 800px;`) to prevent it from becoming unreadably wide on large desktop monitors. This is the cornerstone of responsive design.

.container {
  width: 100%;
  max-width: 800px;
  margin: 0 auto; /* Centers the container */
}

Setting Boundaries: `min-` and `max-` Properties

The `min-` and `max-` properties act as guards for your layout:

  • `max-width`: As seen above, this is the most common. It allows an element to be smaller than the value, but never larger.
  • `min-width`: Ensures an element is *at least* a certain width. Useful for sidebars that should not become too squished.
  • `min-height`: Extremely useful. A common trick is to set `min-height: 100vh;` on your main page wrapper to ensure your footer always stays at the bottom of the screen, even on pages with little content.
  • `max-height`: Less common, but can be used to limit the height of an element, often combined with `overflow: auto;` to create a scrollable area.

Extrinsic vs. Intrinsic Sizing

So far, we've discussed **extrinsic sizing**, where *you* (the developer) define the size with properties like `width`. But HTML elements also have an **intrinsic size**, or a natural size based on their content.

The `width` property's default value is `auto`. For block elements like `<div>`, `auto` means "fill 100% of the parent's width." For inline-block elements like `<img>`, `auto` means "be as wide as my content (the image file)."

Modern CSS gives you even more control over intrinsic sizing with keywords:

  • `width: min-content;`: The element will shrink to be as narrow as possible without overflowing its content (e.g., the width of its longest word).
  • `width: max-content;`: The element will expand to be as wide as necessary to fit all its content on a single line.
  • `width: fit-content;`: A mix of both. The element acts like `max-content`, but won't grow larger than its available space (or a `max-width`, if set).
Key Takeaway: Start all your projects with a universal `box-sizing: border-box`. Build fluid layouts using percentages and `max-width`. Use `min-height` to ensure components have adequate space, and explore `vh`/`vw` units for sizing relative to the screen.

CSS Sizing & Box Model Glossary

`width`
A CSS property that defines the horizontal size of an element's content area (by default).
`height`
A CSS property that defines the vertical size of an element's content area. The default value is `auto`, meaning it grows to fit its content.
`box-sizing`
A property that controls how the total width and height of an element are calculated.
`content-box` (default)
The default value for `box-sizing`. The `width` and `height` properties apply only to the content. Padding and border are added *on top* of this size.
`border-box`
The preferred value for `box-sizing`. The `width` and `height` properties define the *total* size of the element, including the padding and border.
`max-width`
Sets the maximum allowed width of an element. It can be narrower, but never wider. Essential for responsive design.
`min-width`
Sets the minimum allowed width of an element. It can be wider, but never narrower.
`min-height`
Sets the minimum allowed height. Ensures an element is *at least* this tall, but allows it to grow if content is added.
Viewport Units (`vw`, `vh`)
Relative units based on the browser window's (viewport's) dimensions. `100vh` is 100% of the viewport's height. `100vw` is 100% of the viewport's width.
Intrinsic Sizing
An element's natural size based on its content (e.g., the dimensions of an image, or the text inside a button).
`min-content` / `max-content`
Values for `width` or `height` that tell an element to size itself based on the smallest or largest possible space its content could occupy.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust, responsive web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications from the W3C and is periodically reviewed.

External Resources

Found an error or have a suggestion? Contact us!