Syntax
width: value;
min-width: value;
max-width: value;Examples
A Responsive Container Pattern
The extremely common "fluid but capped" content container.
.container {
width: 100%; /* fills available space on small screens */
max-width: 1200px; /* but never grows beyond this on large screens */
margin: 0 auto; /* centers it once max-width is reached */
padding: 0 16px;
}min-width to Prevent Squishing
Ensuring a flex item never becomes too narrow to be usable.
.sidebar {
flex: 1;
min-width: 200px; /* will not shrink below this, even in a tight flex container */
}Parameters and values
- width / height (length | % | auto): Sets a specific dimension
- min-width / min-height (length): Sets a lower bound the element cannot shrink below
- max-width / max-height (length): Sets an upper bound the element cannot grow beyond
Best practices
- Use max-width: 100% on images so they never overflow their container on smaller screens, a near-universal responsive image rule
- Prefer max-width over a fixed width for content containers, letting them shrink gracefully on smaller screens
- Use min-width on flex/grid items to prevent them from becoming unreadably narrow when space is constrained
- Combine width: 100% with max-width for the classic fluid-but-capped container pattern used in most responsive layouts
At a glance
- Purpose
- Presentation and layout
- File extension
- .css
- Runs in
- Web browsers
- Usually used with
- HTML and JavaScript
Specifications & further reading
Related CSS documentation
Length Units: px, %, em, rem
CSS offers several length units. px is an absolute, fixed unit. % is relative to the parent element. em is relative to the current element's font-size (compounding when nested). rem is relative to the root (html) element's font-size, avoiding the compounding problem of em, and is generally preferred for consistent, predictable sizing.Viewport Units: vh, vw, dvh
Viewport units are relative to the browser window's dimensions rather than a parent element. vh and vw represent 1% of the viewport height and width respectively. The newer dvh (dynamic viewport height) solves the classic mobile browser problem where vh included space later covered by the address bar.calc(), min(), max(), clamp()
These functions let you compute CSS values dynamically. calc() performs math between different units. min() and max() pick the smallest or largest of a list of values. clamp(min, preferred, max) is especially powerful for fluid, responsive sizing - it uses the preferred value but never goes below min or above max.Logical Properties
Logical properties describe direction in terms of writing mode (inline/block flow) rather than fixed physical directions (left/right/top/bottom). margin-inline-start behaves like margin-left in English but automatically adapts for right-to-left languages, making them essential for properly internationalized layouts.
CSS offers several length units. px is an absolute, fixed unit. % is relative to the parent element. em is relative to the current element's font-size (compounding when nested). rem is relative to the root (html) element's font-size, avoiding the compounding problem of em, and is generally preferred for consistent, predictable sizing.Viewport Units: vh, vw, dvh
Viewport units are relative to the browser window's dimensions rather than a parent element. vh and vw represent 1% of the viewport height and width respectively. The newer dvh (dynamic viewport height) solves the classic mobile browser problem where vh included space later covered by the address bar.calc(), min(), max(), clamp()
These functions let you compute CSS values dynamically. calc() performs math between different units. min() and max() pick the smallest or largest of a list of values. clamp(min, preferred, max) is especially powerful for fluid, responsive sizing - it uses the preferred value but never goes below min or above max.Logical Properties
Logical properties describe direction in terms of writing mode (inline/block flow) rather than fixed physical directions (left/right/top/bottom). margin-inline-start behaves like margin-left in English but automatically adapts for right-to-left languages, making them essential for properly internationalized layouts.