CSS width Property

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

What You’ll Learn

The width property controls how wide an element is on the page. You will learn fixed sizes, percentage sizing, responsive limits, and when to let the browser calculate width automatically.

01

Horizontal size

Control box width.

02

auto

Default behavior.

03

Pixels

Fixed widths.

04

Percentages

Relative sizing.

05

max-width

Responsive caps.

06

Layout

Cards & columns.

Introduction

The width property in CSS is used to set the width of an element. This property can be applied to various HTML elements including divs, images, tables, and more.

By specifying the width, you can control how much horizontal space an element should occupy on the page, making it a fundamental tool for layout design in web development.

Definition and Usage

Apply width to block-level elements, inline-block elements, replaced elements (like images), and flex or grid items. The syntax is simple: choose a length, percentage, keyword, or viewport unit.

💡
Beginner Tip

By default, width measures only the content area. Add box-sizing: border-box; if you want padding and border included in the width you set — a very common pattern in modern CSS.

📝 Syntax

The syntax for the width property is simple and straightforward. You can specify the width using various units such as pixels, percentages, ems, and more.

syntax.css
element {
  width: auto | <length> | <percentage> | max-content | min-content | fit-content;
}

Here, the value can be a length (e.g., 100px, 50%, 10em), or keywords such as auto, max-content, min-content, fit-content, or inherit.

Basic Example

width.css
.box {
  width: 200px;
  height: 100px;
  background: #93c5fd;
}

Syntax Rules

  • Length values use units like px, rem, em, or vw.
  • Percentages are relative to the containing block’s width.
  • auto lets the browser decide based on content and layout context.
  • Pair with max-width for responsive designs that do not overflow.
  • The property is not inherited.

Related Properties

  • height — sets vertical size
  • max-width — upper width limit for responsive layouts
  • min-width — minimum width before overflow
  • box-sizing — includes padding and border in width calculations

🎯 Default Value

The default value of the width property is auto. When set to auto, the width of the element is determined by the content inside it, the element’s display type, and the available space in its container.

⚡ Quick Reference

QuestionAnswer
Default valueauto
Fixed widthwidth: 200px;
Half of parentwidth: 50%;
Responsive cardwidth: 100%; max-width: 22rem;
Shrink to contentwidth: fit-content;
InheritedNo

💎 Property Values

The width property accepts lengths, percentages, and keyword values.

ValueExampleDescription
<length>width: 200px;Specifies a fixed width in units such as pixels, ems, or rems.
<percentage>width: 50%;Specifies width as a percentage of the containing element’s width.
autowidth: auto;The browser calculates the width (default).
max-contentwidth: max-content;Uses the intrinsic preferred width of the content.
min-contentwidth: min-content;Uses the intrinsic minimum width of the content.
fit-contentwidth: fit-content;Uses available space up to the content’s preferred width.
inheritwidth: inherit;Inherits the width value from the parent element.
auto 200px 50% fit-content max-content

When to Use width

width is one of the most common layout properties:

  • Cards and panels — Set readable widths with max-width for responsive layouts.
  • Columns and sidebars — Use percentages or fixed pixels for grid-like structures.
  • Images and media — Constrain media with width: 100% inside containers.
  • Buttons and chips — Use fit-content or auto for content-sized controls.
  • Data tables — Assign column widths for consistent alignment.

👀 Live Preview

Three boxes with different fixed widths (5rem, 8rem, 11rem):

5rem
8rem
11rem

Responsive card with width: 100% and max-width: 14rem:

This card grows on small screens but stops at a comfortable reading width.

Examples Gallery

In this example, we’ll set the width of a div element to 200 pixels.

📜 Core Patterns

Start with fixed and relative widths for basic layout control.

Example 1 — Fixed pixel width

In this example, we’ll set the width of a div element to 200 pixels.

index.html
<style>
  .box {
    width: 200px;
    height: 100px;
    background-color: lightblue;
  }
</style>

<div class="box"></div>
Try It Yourself

How It Works

A pixel width gives you an exact horizontal size regardless of content length.

Example 2 — Percentage width

Set a child element to half the width of its parent container.

percent.css
.parent { width: 100%; max-width: 20rem; }
.child { width: 50%; }
Try It Yourself

How It Works

Percentage widths scale with the parent. The child always occupies half of the parent’s content width.

📈 Responsive Layouts

Combine width with max-width and compare auto sizing behavior.

Example 3 — Responsive card with max-width

Use width: 100% with max-width so content stays readable on large screens and fluid on small ones.

responsive.css
.card {
  width: 100%;
  max-width: 22rem;
  margin: 0 auto;
}
Try It Yourself

How It Works

The element fills its container up to the max-width limit, then stops growing — a core responsive design pattern.

Example 4 — auto vs fixed width

Compare how width: auto shrinks to content while a fixed width forces a specific box size.

auto-fixed.css
.auto { width: auto; display: inline-block; }
.fixed { width: 160px; }
Try It Yourself

How It Works

auto is the default and adapts to content. A fixed width overrides that and may cause text to wrap inside the box.

♿ Accessibility

  • Allow text to reflow — Avoid fixed widths that force horizontal scrolling when users zoom in.
  • Use max-width for readability — Long lines of text are harder to read; cap content width around 60–75 characters when possible.
  • Test on small screens — Percentage and fixed widths should not clip essential content or controls.
  • Do not rely on width alone for touch targets — Buttons still need adequate padding and height for usability.

width + max-width + box-sizing

A reliable responsive pattern sets full width with a cap, and includes padding in the width calculation:

responsive-pattern.css
.container {
  box-sizing: border-box;
  width: 100%;
  max-width: 72rem;
  padding: 0 1rem;
  margin: 0 auto;
}

🧠 How width Works

1

You set a width value

Choose px, %, auto, or a keyword like fit-content.

Input
2

Browser resolves the box size

Percentages use the parent width; auto uses content and layout rules.

Calculate
3

Layout places the element

Siblings and flex/grid rules position the sized box in the page flow.

Layout
=

Controlled horizontal space

The element occupies the width you intended.

Browser Compatibility

The width property is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a standard property in CSS and can be used with confidence in any web project.

Modern browsers · Widely supported

Universal sizing support

Length, percentage, and keyword width values work consistently in all major browsers.

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
Full support
Opera All versions
Full support

Testing tip

Resize the browser window and test percentage widths and max-width caps on mobile breakpoints.

width property 99% supported

Bottom line: width is a foundational CSS property with excellent browser support.

Conclusion

The width property is essential for controlling the horizontal dimension of elements on a web page.

Whether you are designing a responsive layout or simply adjusting the size of a single element, understanding how to use the width property effectively is crucial. Experiment with different units and values to see how they affect the layout and appearance of your web pages.

💡 Best Practices

✅ Do

  • Use width: 100% with max-width for responsive containers
  • Set box-sizing: border-box when padding is involved
  • Prefer rem over large fixed pixel widths for scalability
  • Test layouts at mobile and desktop breakpoints
  • Combine with flexbox or grid for complex layouts

❌ Don’t

  • Hard-code wide pixel widths that break on small screens
  • Forget that default content-box excludes padding from width
  • Use fixed widths on long body text paragraphs
  • Assume percentage widths work without a defined parent context

Key Takeaways

Knowledge Unlocked

Five things to remember about width

Use these points when sizing elements horizontally.

5
Core concepts
🕐 02

auto

Default value.

Default
🔀 03

px & %

Fixed & relative.

Units
🔒 04

max-width

Responsive cap.

Pattern
🌐 05

box-sizing

Include padding.

Tip

❓ Frequently Asked Questions

The width property sets the horizontal size of an element. It controls how wide a box appears on the page.
The default is auto, which means the browser calculates width from content, display type, and available space.
width sets the target size. max-width sets an upper limit so the element can shrink on smaller screens but never grow beyond that value.
By default (content-box), width applies to the content area only. With box-sizing: border-box, width includes padding and border.
No. width is not inherited, but percentage values are calculated relative to the containing block width.

Practice in the Live Editor

Open the HTML editor and experiment with px, percentage, and max-width values.

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