Skip to main content

HTML Paragraphs

The <p> element defines a paragraph, which is usually a block of text. A paragraph always starts on a new line, and browsers add some space (a margin) before and after it.

Example

Two paragraphs.

Prompt
A page with two paragraphs in p tags: "This is a paragraph." and "This is another paragraph."
Output: complete HTML page

HTML Display

You cannot be sure exactly how HTML will look. Different screen sizes and resized windows give different results. You also cannot change the display by adding extra spaces or extra lines in your code; the browser removes any extra spaces and lines when it shows the page.

Example

Extra spaces and line breaks in the source are collapsed by the browser.

Prompt
A page with two paragraphs whose source code has lots of extra spaces and line breaks, to show that the browser collapses them into single spaces and ignores the extra lines.
Output: complete HTML page

HTML Horizontal Rules

The <hr> tag defines a thematic break in the page, usually shown as a horizontal line. It is used to separate content or mark a change. <hr> is an empty tag with no end tag.

Example

Horizontal rules separating sections of content.

Prompt
A page that uses the hr tag as a thematic break between sections: an h1 and paragraph, an hr, then an h2 and paragraph, then another hr.
Output: complete HTML page

HTML Line Breaks

The <br> element makes a line break. Use it when you want a new line without starting a new paragraph. <br> is an empty tag with no end tag.

Example

A paragraph with line breaks made by <br>.

Prompt
A single paragraph that uses br tags to break onto new lines without starting new paragraphs: "This isa paragraphwith line breaks."
Output: complete HTML page

The Poem Problem

Text that relies on line breaks and spacing, like a poem, does not keep its shape in a normal paragraph. The browser collapses it onto a single line.

Example

A poem in a paragraph, shown all on one line.

Prompt
A paragraph holding the four lines of the "My Bonnie" poem written on separate source lines, to show the problem that the browser collapses them onto a single line.
Output: complete HTML page

Solution - The HTML <pre> Element

The <pre> element defines preformatted text. The text inside it shows in a fixed-width font (usually Courier) and keeps both its spaces and line breaks, so a poem displays correctly.

Example

The poem inside a <pre> element, keeping its line breaks.

Prompt
The same "My Bonnie" poem placed inside a pre element, so its line breaks and spacing are preserved and it shows in a fixed-width font.
Output: complete HTML page