CSS - Width, Height & Sizing
Overview
Quick links: Exercises • Checks • Print as PDF
Estimated time: 10–15 minutes
Control element dimensions with width, height, and intrinsic sizing keywords for responsive, content-aware layouts.
Learning Objectives
- Use width, height, min/max constraints effectively.
- Apply intrinsic sizing keywords:
min-content
,max-content
,fit-content()
.
Prerequisites
- Understanding of the box model and units.
Details & Examples
.card {
width: 320px;
max-width: 100%; /* responsive constraint */
}
.title {
width: max-content; /* shrink to content width */
}
.paragraph {
width: fit-content(50ch); /* cap at 50ch or available space */
}
Vocabulary
- Intrinsic sizing: Sizing based on content rather than explicit values.
- Constraint: min/max limits that override computed sizes when needed.
Common Pitfalls
- Forgetting
max-width: 100%
makes fixed-width elements overflow on small screens. - Using
min-content
can cause text to wrap aggressively.
Checks for Understanding
- What does
width: max-content
do? - How do you make a 400px element responsive?
Show answers
- It makes the element as wide as its content requires, ignoring available space.
- Add
max-width: 100%
so it shrinks when the container is smaller than 400px.
Exercises
- Make an image responsive with
max-width: 100%
while preserving its intrinsic ratio. - Use
fit-content
to cap a paragraph width to 50ch.
Suggested answers
img{ max-width:100%; height:auto; }
width: fit-content(50ch);