In the previous lesson you saw the skeleton of an HTML document. Now it's time to learn, precisely, the grammar used to write everything else: what a tag is, what an element is, what an attribute is and what the rules are for combining them. These concepts come up constantly in documentation, job adverts and technical conversations, and mixing them up is a common source of errors. By the end you will be able to read and write any snippet of HTML with confidence, even if you don't yet know every tag that exists.

Contents

  1. Tag, element and content: not the same thing
  2. Opening and closing tags
  3. Void elements
  4. Correct and incorrect nesting
  5. Attributes: syntax and quotes
  6. Boolean attributes
  7. Global attributes: id, class, title, lang and hidden
  8. HTML comments
  9. Upper case, lower case and whitespace

Tag, element and content: not the same thing

Look at this snippet:

<p class="highlight">Today, carrot cake</p>

Its parts are:

graph TD
    E["Complete p element"] --> A["Opening tag: p with class=highlight"]
    E --> B["Content: Today, carrot cake"]
    E --> C["Closing tag: /p"]
    A --> A1["Attribute: class = highlight"]
Term What it is In the example
Tag The mark between < and > <p class="highlight"> and </p>
Tag name The word that identifies the type p (paragraph)
Attribute Additional information inside the opening tag class="highlight"
Content What lies between the opening and closing tags Today, carrot cake
Element The complete unit: opening tag + content + closing tag The whole line

The key difference: the tag is the mark; the element is the complete piece that appears in the DOM tree. That's why it is correct to say "the p element contains text" and "the closing tag of p is </p>". Informally, many people say "the p tag" to mean the element, and it's understood, but it's worth knowing the difference.

Opening and closing tags

Most elements have two tags:

  • Opening: <name>, optionally with attributes: <name attribute="value">.
  • Closing: </name>, with a slash / before the name and never with attributes.
<h1>The Blue Cup</h1>
<p>We open at 8:00.</p>
<title>Home | The Blue Cup</title>

On each line, everything between the opening and closing tags is "marked up" with the meaning of that element.

What happens if you forget to close one? For some elements, such as p, the specification allows the closing tag to be omitted in certain cases and the browser infers it. For others, the result is unpredictable: everything that follows may end up inside the unclosed element. Professional recommendation: always close elements that have a closing tag. The code is clearer and you avoid surprises.

Void elements

Some elements cannot have content: they don't wrap anything, they are simply "there". They are called void elements and only have an opening tag.

Element What it's for Where you'll see it in the course
<meta> Document metadata You already use it: <meta charset="utf-8">
<br> Line break within text Module 2
<hr> Thematic break between blocks Module 2
<img> Image Module 3
<input> Form field Module 5
<link> Link to an external resource (e.g. CSS) Module 8
<source>, <track>, <wbr>… Other specific cases When relevant

An example with void elements (images and line breaks will be covered in their own modules; here we are only interested in the syntax):

<p>12 Example Street<br>Fictional Town</p>
<img src="img/storefront.jpg" alt="The Blue Cup's storefront">
  • <br> has no </br>: there is nothing to close.
  • <img> doesn't wrap any content; all its information goes in attributes.

<br> or <br />?

You will see both forms. The trailing slash (<br />) comes from XHTML, which required everything to be closed. In modern HTML the slash is optional and has no effect at all. Pick a style and be consistent; in this course we will use the form without the slash: <br>, <img …>, <meta …>.

What you must never write is </br> or </img>: void elements have no closing tag.

Correct and incorrect nesting

You already know that elements go inside one another. The rule is strict: the last element opened is the first one closed. Think of boxes: you can put a small box inside a big one, but it can't end up "half in, half out".

Correct:

<p>We are open <strong>every day</strong> of the year.</p>

p is opened, strong is opened, strong is closed, p is closed. The closing order is the reverse of the opening order.

Incorrect (overlapping elements):

<p>We are open <strong>every day of the year.</p></strong>

Here p is closed while strong is still open. The browser will try to fix it in its own way, but the result may not be what you expected, and the validator will flag it as an error.

Pattern Valid? Reason
<a><b></b></a> Yes b is opened and closed inside a
<a><b></a></b> No The elements overlap
<a></a><b></b> Yes They are siblings, one after the other
<a><b></b> (no </a>) No (or it depends) a is not closed

As well as the order, there are content rules: not every element can go inside any other. For example, a paragraph cannot contain another paragraph, and a heading shouldn't contain a paragraph. You will learn them as you meet each element; the validator (Module 10) will warn you when you break them.

Trick: in VS Code, when you place the cursor on a tag, its matching opening or closing tag is highlighted. Use this to check where each element ends.

Attributes: syntax and quotes

Attributes add information or configuration to an element. They are written only in the opening tag, separated by spaces:

<html lang="en">
<img src="img/white-coffee.jpg" alt="Cup of white coffee with a leaf pattern">

Syntax: name="value".

  • lang, src and alt are attribute names.
  • "en", "img/white-coffee.jpg"… are their values.
  • No spaces go between the name, the = and the value (technically they are allowed, but it isn't good practice).
  • The order of attributes doesn't matter.
  • The same attribute cannot be repeated on the same element.

Quotes: double, single or none

Form Example Valid? Recommendation
Double quotes lang="en" Yes Always use this one
Single quotes lang='en' Yes Useful if the value contains double quotes
No quotes lang=en Yes, if the value has no spaces or certain characters Avoid it

Why avoid unquoted values? Because a single space is enough to break them:

<!-- Incorrect: the browser understands alt="Black" plus a stray attribute called "coffee" -->
<img src="img/coffee.jpg" alt=Black coffee>

<!-- Correct -->
<img src="img/coffee.jpg" alt="Black coffee">

If the value contains double quotes, wrap it in single quotes: title='Our "coffee of the month"'.

Boolean attributes

Some attributes work like switches: their mere presence means "on" and their absence, "off". They are called boolean attributes.

<p hidden>Message hidden until the new menu is published.</p>

The following three forms are equivalent and all switch the attribute on:

<p hidden>…</p>
<p hidden="">…</p>
<p hidden="hidden">…</p>

And a very common mistake:

<!-- This HIDES it too! -->
<p hidden="false">…</p>

Since what counts is presence, hidden="false" is still present and so the paragraph is hidden. To switch off a boolean attribute, remove it. You will see many more boolean attributes in forms (required, disabled, checked) in Module 5.

Global attributes: id, class, title, lang and hidden

Most attributes are specific to certain elements (src only makes sense on elements that load a resource). But there are global attributes, which can be used on any element. These are the most important:

Attribute What it's for Example Key rules
id Unique identifier for the element on the page <h2 id="opening-hours"> Cannot be repeated in the same document; no spaces
class One or more classes for grouping elements (mainly for CSS) <p class="price offer"> Several classes separated by spaces; can be repeated on many elements
title Supplementary information, shown as a tooltip on mouse hover <p title="Price includes VAT"> Don't use title for essential information: on mobile and with a keyboard it's barely visible
lang Language of that element, if it differs from the page's <p lang="it">Ristretto</p> Same values as in <html lang>
hidden Hides the element (boolean) <p hidden> Hidden from everyone, including screen readers

An example applied to The Blue Cup, combining them:

<h2 id="speciality">Coffee of the week</h2>
<p class="product highlight" title="Origin: Ethiopia">
  Our coffee of the week is a fruity <span lang="it">ristretto</span>.
</p>
<p class="notice" hidden>Sold out for today.</p>

Explanation:

  • id="speciality": uniquely identifies that heading. Later on it will be used to link directly to that section (Module 3) or to give it a specific style.
  • class="product highlight": the paragraph belongs to two classes. Other products can share the product class, and only some the highlight class. You will see how classes are used with CSS in Module 8.
  • title="Origin: Ethiopia": appears as a small box when you hover the mouse over it.
  • lang="it" on a span (a generic inline element you will study in Module 2): indicates that "ristretto" is in Italian, so that a screen reader pronounces it correctly.
  • hidden: the sold-out notice exists in the code, but is not displayed.

id versus class

id class
How many times per page? Only once Unlimited
How many per element? One Several, separated by spaces
Typical use Link anchors, forms, unique references Grouping elements with a common style or function
Analogy Passport number A group you belong to (e.g. "club members")

To name id and class values, use lower case and hyphens, with no spaces or accents: coffee-menu, offer-price. It's the same convention you will use for file names.

HTML comments

Comments are notes for the people reading the code; the browser ignores them and doesn't display them on the page.

<!-- Opening hours section: update in summer -->
<p>We are open from 8:00 to 20:00.</p>

<!--
  To do: add the winter menu
  once the bakery confirms the products.
-->
  • They start with <!-- and end with -->.
  • They can take up one line or several.
  • They cannot be nested: a comment inside another breaks at the first -->.
  • In VS Code, Ctrl+/ comments or uncomments the current line.

Common uses: explaining decisions, marking sections of a long document or temporarily "switching off" a snippet.

Careful: comments are not private. Anyone can see them with "View source". Never write passwords, personal data or internal remarks you don't want to publish in them.

Upper case, lower case and whitespace

Upper and lower case

HTML tag and attribute names are case-insensitive: <P>, <p> and <P CLASS="x"> all work the same. Even so, the universal convention is to write everything in lower case. Attribute values, on the other hand, can be case-sensitive: class="Price" and class="price" are different classes as far as CSS is concerned.

Whitespace

Within text content, the browser collapses whitespace: several consecutive spaces, tabs and line breaks are displayed as a single space.

<p>Speciality     coffee
   and
       pastries.</p>

This is displayed as: "Speciality coffee and pastries." on a single line (which will wrap according to the window width).

Practical consequences:

  • You can indent and break lines in your code freely to make it readable: it won't affect the result.
  • You cannot use spaces or Enter to separate text visually. That's what elements (paragraphs, line breaks) and CSS are for.
  • There are exceptions (preformatted text) that you will see in Module 2.

Common Mistakes and Tips

  • Confusing tag with element: the tag is <p>; the element is <p>…</p> with its content.
  • Closing a void element (</img>, </br>): those tags don't exist.
  • Attributes on the closing tag (</p class="x">): attributes only go on the opening tag.
  • Repeating an id: two elements with id="opening-hours" cause problems with links and styles. If you need to repeat it, it's a class.
  • Several classes written wrongly: class="product" class="highlight" is a repeated attribute; the correct form is class="product highlight".
  • Forgetting a quote: alt="Black coffee> makes the browser "swallow" the rest of the line. The editor's syntax highlighting usually gives it away.
  • Using title as the only source of information: it doesn't reach mobile or keyboard users.
  • Tip: write the closing tag straight after the opening one and then fill in the content. VS Code does this for you when autocompleting.

Exercises

Exercise 1. Identify the parts. In the following snippet, point out: the tag name, the attributes (name and value), the content and whether there is a void element.

<p id="welcome" class="intro">We look forward to seeing you at 12 Example Street<br>Fictional Town</p>

Exercise 2. Fix the code. This snippet contains six errors. Find them and write the correct version.

<P Class=product highlight>Lemon sponge cake <strong>€2.50</P></strong>
<p id="price">Croissant</p>
<p id="price">Muffin</p>
<p hidden="false">Autumn promotion</p>
<img src="img/lemon-cake.jpg" alt="Lemon sponge cake"></img>

Exercise 3. Write with attributes. Write a snippet for The Blue Cup's home page with:

  1. A comment saying "Welcome block".
  2. An h2 heading with id="welcome" and the text "Welcome".
  3. A paragraph with the classes intro and highlight, a title saying "Since 2015", and the text "Speciality coffee and handmade pastries in your neighbourhood".
  4. A hidden paragraph with the text "Closed for holidays".

Solutions

Solution 1.

  • Tag name: p.
  • Attributes: id with the value welcome and class with the value intro.
  • Content: We look forward to seeing you at 12 Example Street<br>Fictional Town (text and a br element).
  • Void element: <br>, which has no closing tag.

Solution 2. Errors:

  1. P and Class in upper case (it works, but breaks the convention).
  2. The class value has no quotes and contains a space: highlight is interpreted as another attribute.
  3. p and strong overlap: strong must be closed before p.
  4. id="price" is repeated on two elements.
  5. hidden="false" hides the paragraph; if you want it visible, you have to remove the attribute.
  6. </img> doesn't exist: img is a void element.

Corrected version:

<p class="product highlight">Lemon sponge cake <strong>€2.50</strong></p>
<p class="price">Croissant</p>
<p class="price">Muffin</p>
<p>Autumn promotion</p>
<img src="img/lemon-cake.jpg" alt="Lemon sponge cake">

(For point 4, the id has been turned into a class, since the value is repeated.)

Solution 3.

<!-- Welcome block -->
<h2 id="welcome">Welcome</h2>
<p class="intro highlight" title="Since 2015">Speciality coffee and handmade pastries in your neighbourhood</p>
<p hidden>Closed for holidays</p>

Check in the browser that the last paragraph isn't visible, but does appear in the DevTools Elements panel.

Conclusion

You now have a command of HTML grammar: you can tell tag, content and element apart; you know that most elements are opened and closed, that void elements such as meta, br or img have no closing tag, and that nesting must respect the reverse closing order. You know how to write attributes with double quotes, you understand the trap of booleans such as hidden and you know the global attributes id, class, title, lang and hidden. You also know how to comment your code and how HTML treats upper case and whitespace. In the next lesson, Creating Your First HTML Page, you will put all of this into practice by writing The Blue Cup's index.html, step by step.

HTML Course

Module 1: Introduction to HTML

Module 2: Text Formatting in HTML

Module 3: Links and Media in HTML

Module 4: Tables in HTML

Module 5: Forms in HTML

Module 6: HTML5 Semantic Elements

Module 7: Advanced HTML Techniques

Module 8: Integrating HTML and CSS

Module 9: Responsive Web Design

Module 10: Best Practices and Accessibility

Module 11: Project: Building a Complete Website

© Copyright 2026. All rights reserved