Normal Flow and Block Elements
So far, you've been styling how elements look: their colors, fonts and boxes. In this lesson, you'll learn how the browser decides where to put them on the page, and how to change that with the properties display and position.
Unless we tell it otherwise, the browser places elements on the page one after another, in the same order as they are in the HTML. This is called normal flow. In normal flow, elements behave in two main ways:
- Block elements start on a new line and stack from top to bottom. Unless we set their width, they are as wide as their parent. Headings, paragraphs,
div,ul,li,nav,mainandsectionare block elements. - Inline elements are placed in a line of text, one after another from left to right, just like words. When a line is full, they continue on the next one. Examples are
strong,em, links (a) and images (img).
Whether an element is block or inline is decided by the property display. Every element has a default value: paragraphs have block, strong has inline. In a rule, we can change it, for example:
strong {
display: block;
}With this rule, every strong element would start on a new line and be as wide as its parent, just like a paragraph.
In the editor, you'll find the cat page with a new stylesheet that uses only what you already know. At the top, we've added navigation: a list of links to the sections of the page. In the first paragraph, there's a photo of cats.
Instructions
- 1
The photo (
img) is an inline element, so it sits in the first line of the paragraph like a big letter, and the text continues right next to it. In the rule forimg, add the declarationdisplay: block. The photo moves to a line of its own, and the text continues below it. - 2
Now center the photo: in the same rule, add
margin: 10px auto. As you know from the box model lesson,automargins on the left and right center a block that has a width: the browser splits the free space evenly between them. This works only for block elements, though. On an inline element,automargins do nothing.