Sub topic 2
The content area: width and height
This chapter assumes you can write CSS rules with selectors and declarations, and that you know the units px, em, rem, and %. It covers the innermost layer of every box: the content area.
What the content area is
The content area is the rectangle where an element's text, images, or child elements are laid out. Padding, border, and margin all wrap around it. The width and height properties set the size of this rectangle by default, because browsers use box-sizing: content-box unless you change it. Later chapters cover padding, border, and box-sizing in detail. Here the focus is on how the content area gets its size.
How width is resolved
A block-level element such as div or p has width: auto by default. With auto, the box stretches to fill the available width of its container, and the content area takes whatever space is left after padding, border, and margin.
When you set an explicit width, the content area is exactly that wide. The box no longer fills its container.
.fill {
/* width: auto is the default, so the box fills its container */
background: #e0f2fe;
}
.fixed {
width: 300px;
background: #fef3c7;
}How height is resolved
Height works differently. The default is height: auto, which means the content area grows to fit its content. A paragraph with three lines of text is three lines tall. Add a fourth line and the box grows.
If you set a fixed height, the box keeps that height even when the content needs more room. The extra content spills out of the box. The Overflow chapter explains what happens then.
Percentages
A percentage width is relative to the width of the containing block, which is usually the parent's content area.
.parent {
width: 600px;
}
.child {
width: 50%; /* 300px */
}A percentage height only works when the parent has a definite height. If the parent's height is auto, the browser cannot resolve the percentage, so the child behaves as if height: auto were set. This is a common reason for height: 100% appearing to do nothing.
.parent {
height: 400px;
}
.child {
height: 50%; /* 200px, because the parent's height is definite */
}Elements that ignore width and height
width and height do not apply to every element. A non-replaced inline element, such as a span or a containing text, ignores both properties. Its size comes from its text. If you need to size such an element, change its display value. The display types chapter covers this.
Replaced elements, such as img, have an intrinsic size taken from the file they load. If you set only one dimension, the browser scales the other to keep the aspect ratio. This only works when the other dimension is left as auto.
<style>
.photo {
width: 200px;
/* height stays auto, so the image keeps its proportions */
}
</style>
<img class="photo" src="photo.jpg" alt="A mountain lake">Common errors
- Setting
heighton a container and then watching text overflow. Preferheight: autoand let content decide. - Expecting
height: 100%to work inside a parent with no set height. - Setting
width: 100%on an element that also has padding or border. Withcontent-box, the total box is wider than the parent. Thebox-sizingchapter fixes this. - Applying
widthto aspanand seeing no change.
Exercise
A .parent has width: 800px and no height set. Its child has width: 25% and height: 50%. What are the child's content width and height?
Show answer
The width is 200px, because 25% of the parent's 800px content width is 200px. The height is not 50% of anything. The parent's height is auto, so the percentage cannot be resolved and the child's height is auto. It is as tall as its own content.
Exercise
You set width: 200px; height: 100px; on a span that contains text. The span looks unchanged. Why?
Show answer
A span is a non-replaced inline element, and inline elements ignore width and height. Its size comes from its text. Changing its display to inline-block or block makes the properties apply.