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.
Fundamentals
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.
Foundation
📝 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.
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
Defaults
🎯 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.
Cheat Sheet
⚡ Quick Reference
Question
Answer
Default value
auto
Fixed width
width: 200px;
Half of parent
width: 50%;
Responsive card
width: 100%; max-width: 22rem;
Shrink to content
width: fit-content;
Inherited
No
Reference
💎 Property Values
The width property accepts lengths, percentages, and keyword values.
Value
Example
Description
<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.
auto
width: auto;
The browser calculates the width (default).
max-content
width: max-content;
Uses the intrinsic preferred width of the content.
min-content
width: min-content;
Uses the intrinsic minimum width of the content.
fit-content
width: fit-content;
Uses available space up to the content’s preferred width.
inherit
width: inherit;
Inherits the width value from the parent element.
auto200px50%fit-contentmax-content
Context
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.
Preview
👀 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.
Hands-On
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.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
Testing tip
Resize the browser window and test percentage widths and max-width caps on mobile breakpoints.
width property99% supported
Bottom line:width is a foundational CSS property with excellent browser support.
Wrap Up
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.