Elements, headings and paragraphs
Understand HTML elements, tags and attributes, then structure text with headings h1 to h6, paragraphs, line breaks, emphasis and comments, the way real pages do.
🎯 By the end of this lesson you will be able to:
- Name the parts of an HTML element: tags, attributes and content
- Use headings h1 to h6 in a sensible outline
- Mark up paragraphs, important text, emphasis and comments
Outline
What you'll learn and why it matters
HTML is made of elements: building blocks that tell the browser what each piece of content is. A heading, a paragraph, a link and an image are all elements. Once you can read one element, you can read any web page's code. Good headings also help search engines and screen readers understand your page.
The parts of an element
Most elements have an opening tag, some content and a closing tag. The closing tag has a forward slash. Some elements also have attributes: extra settings written inside the opening tag as name="value".
| Term | Example | Meaning |
|---|---|---|
| Tag | <p> and </p> | The markers around content, written in angle brackets |
| Element | <p>Hello</p> | The opening tag, content and closing tag together |
| Attribute | lang="en" | Extra information inside an opening tag |
| Void element | <br>, <img>, <meta> | An element with no content and no closing tag |
Headings: the outline of your page
HTML has six heading levels, from <h1> (most important) to <h6> (least). Use them like the outline of a document: one <h1> for the page's main title, <h2> for sections, <h3> for parts of a section.
<h1>Green Leaf Café</h1>
<h2>Our menu</h2>
<h3>Hot drinks</h3>
<p>Masala chai, filter coffee and hot chocolate.</p>
<h3>Snacks</h3>
<p>Samosas, cheese toast and banana bread.</p>
<h2>Opening hours</h2>
<p>Every day from 8am to 8pm.</p>Browser preview
Paragraphs, line breaks and emphasis
Browsers ignore extra spaces and line breaks in your code: any run of spaces and new lines becomes a single space. To start a new paragraph, use a new <p>. For a line break inside a paragraph, such as in an address or a poem, use <br>.
<p>This sentence is written
over two lines with extra spaces, but the browser shows it on one line.</p>
<p>Green Leaf Café<br>
12 Park Road<br>
Pune 411001</p>
<p><strong>Important:</strong> we are closed on 1 January.</p>
<p>Our banana bread is <em>really</em> popular.</p>
<!-- This is a comment. Browsers do not show it. -->Browser preview
| Element | Use it for | Default look |
|---|---|---|
<p> | A paragraph of text | Space above and below |
<br> | A line break inside a paragraph (addresses, poems) | Next line |
<strong> | Text that is important or urgent | Bold |
<em> | Stress, the way your voice changes when you say it | Italic |
<!-- … --> | A note to yourself or other developers | Not shown |
Elements inside elements
Elements can sit inside other elements: in the last example, <strong> is inside <p>. This is called nesting. The rule is simple: close elements in the reverse order you opened them, like closing boxes inside boxes.
<!-- Correct: strong opens inside p and closes inside p -->
<p>This is <strong>very important</strong> news.</p>
<!-- Wrong: the tags cross over -->
<p>This is <strong>very important</p></strong>Practice
Guided exercise
A recipe page
Build the top of a recipe page: a main heading for the dish, a section heading Ingredients with a paragraph, and a section heading Method with two paragraphs. Make the cooking time important.
- Start with
<h1>for the dish name. - Add
<h2>Ingredients</h2>and a<p>listing the ingredients. - Add
<h2>Method</h2>and two<p>elements, one per step. - Wrap the cooking time in
<strong>.
Show the solution
<h1>Vegetable Upma</h1>
<h2>Ingredients</h2>
<p>Semolina, onion, carrot, peas, mustard seeds, curry leaves, salt and water.</p>
<h2>Method</h2>
<p>Roast the semolina in a dry pan until it smells nutty, then set it aside.</p>
<p>Fry the spices and vegetables, add water, stir in the semolina and cook for <strong>5 minutes</strong>.</p>Browser preview
How to get there: The dish name is the one <h1>; the two sections are <h2>. Each step is its own paragraph, and <strong> sits inside the paragraph, closing before </p>.
Your turn
Fix the broken page
The starter code has three problems: a heading level is skipped, the nesting is crossed, and the last paragraph is not closed. Rewrite it correctly.
<h1>My Holiday</h1>
<h3>Day one</h3>
<p>We arrived in <em>Goa</p></em>
<p>The beach was beautiful.Show the solution
<h1>My Holiday</h1>
<h2>Day one</h2>
<p>We arrived in <em>Goa</em></p>
<p>The beach was beautiful.</p>Browser preview
How to get there: Sections under the <h1> start at <h2>, </em> must close before </p> because it opened inside it, and every paragraph gets a closing </p> so the code is clear to read.
Common mistakes
Using several <h1> elements for decoration
You see: Search engines and screen readers cannot tell what the page is about
Fix: Keep one <h1> for the page title and use <h2> and <h3> below it.
Pressing Enter to make a new line
You see: Lines join together into one
Fix: Use a new <p> for a new paragraph, or <br> for a line break inside one.
Crossing tags, like <p><em>…</p></em>
You see: Styles leak into the wrong text, or the validator reports an error
Fix: Close the most recently opened element first.
Forgetting the slash in a closing tag
You see: Everything after it turns bold, italic or into a heading
Fix: Closing tags start with </, for example </strong>.
Recap
- An element is an opening tag, content and a closing tag; attributes add extra information.
- Use one
<h1>, then<h2>to<h6>in order, chosen for meaning not size. - Browsers collapse spaces and new lines; use
<p>for paragraphs and<br>for line breaks. <strong>is for importance,<em>for stress; close nested tags in reverse order.
Key terms
- Element
- A piece of a page made of an opening tag, content and a closing tag.
- Attribute
- Extra information in an opening tag, written as name="value".
- Nesting
- Putting elements inside other elements.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. In `<a href="menu.html">Menu</a>`, what is `href`?
2. Which heading should you normally use only once per page?
3. You type three spaces and press Enter twice between words inside one `<p>`. What does the browser show?
4. Which line is nested correctly?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.