Sub topic 2
The content area: width and height
Every element's box has a content area at its centre. This chapter covers how its size is set, how browsers compute it when you don't set it, and how it relates to the total space a box takes up. It assumes you can write CSS rules with selectors and declarations, and that you know the units px, em, rem, and %. It also assumes you have seen the four layers of the box (content, padding, border, margin) in the overview chapter.
What the content area is
The content area is the innermost rectangle of a box. It holds the element's text, child elements, or image. Padding, border, and margin are drawn around it.
The width and height properties set the size of this rectangle. That is true under the default box-sizing: content-box. The next chapter covers how border-box changes that meaning. This chapter uses content-box throughout.
Default sizing: `auto`
If you don't set width or height, both default to auto. What auto does depends on the element and the axis.
| Property | Typical `auto` behaviour for a block element |
|---|---|
| `width` | Fills the available width of the containing block, minus the element's own margin, border, and padding |
| `height` | Grows or shrinks to fit the content |
The two axes follow different rules because of how text flows. Lines of text wrap to fit the available width, so width is set by the parent. Height is whatever is left after the text has wrapped, so height is set by the content.
An empty div with no height has a content height of 0. Adding padding or a border still gives it a visible size, but the content area stays 0 tall.
Setting explicit lengths
You can set width and height to any non-negative length or percentage. Negative values are invalid, and the browser drops the declaration.
.panel {
width: 20rem;
height: 10rem;
}Two points about explicit lengths matter in practice:
- A fixed
heightremoves the link between box size and content size. If the content needs more room than you gave it, it spills out. The overflow chapter covers what happens then. - A fixed
widthon a block element also changes how its margins resolve. Withwidth: auto, the browser adjusts the content width to fit the container. With a fixed width, the box no longer stretches, so any leftover horizontal space has to go somewhere. That is whymargin-inline: autocan centre a block that has a width set.
Percentages
A percentage width is relative to the width of the element's containing block, which is usually the content area of the parent. A percentage height is relative to the height of the containing block, with an important condition.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Percentage heights</title>
<style>
.fixed-parent {
height: 400px;
background: #eee;
}
.auto-parent {
background: #ddd;
}
.child {
height: 50%;
background: #7aa7e8;
}
</style>
</head>
<body>
<div class="fixed-parent">
<div class="child">Height: 200px</div>
</div>
<div class="auto-parent">
<div class="child">Height: auto</div>
</div>
</body>
</html>In the first case, the parent has a definite height of 400px, so the child resolves to 200px. In the second case, the parent's height depends on its content, which includes the child. Resolving 50% against that would be circular. The browser treats the percentage as auto instead, and the child is only as tall as its text.
A percentage height works when the parent has a definite height, such as a length, or a percentage that itself resolves. For a full-page layout, that chain of heights has to start at the root. Setting height: 100% on html and body is the classic fix, though min-height: 100vh (viewport units) is simpler for most pages.
Total space with `content-box`
Padding and border are added outside the content area. The rendered size of a box is therefore the content size plus everything around it. Take this rule from the topic's running example:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}| Layer | Horizontal contribution |
|---|---|
| Content width | 300px |
| Padding (left + right) | 40px |
| Border (left + right) | 10px |
| Border box width | 350px |
| Margin (left + right) | 20px |
| Margin box width | 370px |
The width you wrote is 300px, but the visible box, from the outside edge of its border on one side to the other, is 350px. The space it occupies in a layout, including margins, is 370px. The same arithmetic applies vertically, using height, top and bottom padding, top and bottom border, and top and bottom margin.
The formulas are:
border-box width = width + padding-left + padding-right + border-left + border-right
margin-box width = border-box width + margin-left + margin-rightMost of the bugs people hit with width come from this rule.
The `width: 100%` overflow
The most common bug follows from that arithmetic. Compare two ways of making a padded block fill its parent.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>auto vs 100%</title>
<style>
.parent {
width: 400px;
background: #eee;
}
.a, .b {
padding: 20px;
border: 5px solid #333;
margin-bottom: 10px;
}
.a { /* width: auto by default */
background: #9fd89f;
}
.b {
width: 100%;
background: #f0a0a0;
}
</style>
</head>
<body>
<div class="parent">
<div class="a">Fits: content width is 350px</div>
<div class="b">Overflows: border box is 450px</div>
</div>
</body>
</html>The .a element uses width: auto. The browser subtracts padding and border from the available 400px, so the content area is 350px and the border box is exactly 400px.
The .b element asks for a content width of 400px. Padding and border are then added, giving a border box of 450px, which sticks out of its parent by 50px.
Elements where `width` and `height` behave differently
The auto rules above describe non-replaced block elements. Other kinds of boxes follow different rules.
- Replaced elements, such as
img,video, andcanvas, have an intrinsic size taken from their content. Animgwith no CSS renders at its natural pixel size. If you set onlywidthand leaveheightasauto, the browser keeps the aspect ratio. If you set both, the image stretches unless you also useobject-fit. - Non-replaced inline elements, such as
spananda, ignorewidthandheight. Their size comes entirely from their text. The display types chapter explains why, and howinline-blockchanges it. - Floated and absolutely positioned boxes with
width: autoshrink to fit their content instead of filling the container.
You can give any box a preferred shape with aspect-ratio:
.thumbnail {
width: 100%;
aspect-ratio: 16 / 9;
}With height: auto, the browser computes the height from the width and the ratio. If the content is taller than that height, the content wins unless you also constrain the height explicitly.
Keyword sizes
Besides lengths, width and height accept keywords that size the box from its content:
min-contentis the narrowest width that doesn't cause overflow, for example the longest unbreakable word in a paragraph.max-contentis the width the content would take with no wrapping.fit-contentis the content's natural width, capped at the available space.
.tag {
width: fit-content;
padding: 0.25rem 0.5rem;
}This makes a block-level element shrink to its text without changing its display value.
Common mistakes
- Expecting
widthto include padding and border. Undercontent-boxit never does. - Setting
height: 100%on a child whose parent hasheight: auto. The percentage silently becomesauto. - Fixing
heighton a box that holds text. Text size, zoom, and translation change line counts, and the text can then overflow. - Using
width/heighton aspanand seeing no effect. - Forgetting that percentage
widthrefers to the parent's content width, not the viewport.
Summary of the model
The content area is the box's innermost rectangle. Width and height set its size, defaulting to auto. Block width follows the parent, and block height follows the content. Percentage heights need a definite parent height. Padding and border add to the content size under content-box, and margins add again outside that. The next chapters build on this: padding, border, and margin in turn, then box-sizing, which changes what width measures.
Exercise
A block element has width: 240px; height: 100px; padding: 10px 15px; border: 4px solid; margin: 8px; and the default box-sizing. What are its border-box width and height, and how much horizontal space does it occupy in a layout including margins?
Show answer
Border-box width is 240 + 15 + 15 + 4 + 4 = 278px. Border-box height is 100 + 10 + 10 + 4 + 4 = 128px. Including 8px margins on the left and right, it occupies 278 + 16 = 294px horizontally.
Exercise
A div with height: 50% sits inside a parent whose height is not set. The parent contains only this child, which has two lines of text. What height does the child get, and why?
Show answer
The child is only as tall as its two lines of text. The parent's height depends on its content, so the percentage cannot be resolved without a circular dependency, and the browser treats height: 50% as auto.