Cheat sheet #3

HTML element syntax

A cheat sheet for web design students. Learn how an HTML element is written and how attributes work, why your choice of element matters, and how elements fit inside one another.

The short version

An element is usually made up of an opening tag, some content and a closing tag. Attributes always go in the opening tag. Choose the element that describes what the content is.

<a href="contact.html">Contact us</a>
On this cheat sheet

Container and void elements

Container elements

A container element wraps content. It has an opening tag, the content itself, and a closing tag. The closing tag repeats the element name, with a forward slash in front of it.

<p>Welcome to the Hill Café.</p> ^ opening tag ^ content ^ closing tag

The content can be text, other elements, or a mix of both. These are all container elements:

<h1>Opening times</h1> <p>We are open <strong>every day</strong>.</p> <ul> <li>Coffee</li> <li>Cake</li> </ul> <button>Book a table</button>
Do<p>Hello</p>
Don’t<p>Hello <p>Hello<p> <p>Hello<\p>

Forgetting the closing tag is the most common beginner error. Browsers will try to guess what you meant, but they often guess wrong.

Void elements

A void element has no content, so it has no closing tag. It’s a single tag that stands on its own, and it either inserts something into the page (an image or a line break, for example) or gives information to the browser (metadata).

<br> <!-- line break --> <hr> <!-- thematic break (a rule) --> <img src="images/hero-mountains.jpg" alt="Snowy peaks"> <input type="email" name="email-address"> <meta charset="utf-8"> <link rel="stylesheet" href="css/main-styles.css">
Do<img src="logo.svg" alt="Hill Café"> <br>
Don’t<img src="logo.svg" alt="Hill Café"></img> </br>

You may sometimes see a trailing slash, as in <br />. HTML5 allows it, but it isn’t needed. Whichever style you choose, be consistent. This cheat sheet leaves the slash off.

Container elementVoid element
TagsOpening and closingOne tag only
ContentYes: text and/or other elementsNone
Common examplesp h1–h6 a ul li div section button strongimg br hr input meta link source wbr

Attributes

Attribute syntax

Attributes provide extra information about an element. They are always written in the opening tag (never in the closing tag), after the element name, and each one is a name/value pair.

<a href="about-us.html" class="nav-link">About us</a> ^ name ^ value ^ second attribute
RuleExample
Separate the element name and each attribute with a space<img src="a.jpg" alt="Cat">
Join name and value with =, and wrap the value in straight double quotesclass="intro"
Use lowercase attribute nameshref, not HREF
Order of attributes doesn’t matter<img alt="Cat" src="a.jpg"> is fine
Use each attribute only once per elementclass="a b", not class="a" class="b"
Boolean attributes need no value; their presence turns them on<input type="checkbox" checked>
Do<a href="contact.html"> <img src="logo.svg" alt="Logo">
Don’t<a href=contact.html> <a href=“contact.html”> <img src="logo.svg" alt="Logo"/img>

Word processors insert curly “smart” quotes, and these aren’t valid in HTML, so always type straight quotes in a code editor.

Mandatory and optional attributes

Some elements need certain attributes in order to work, or to be valid. Others have attributes that are optional extras. It’s worth learning which is which for each element you use.

ElementMandatoryOptional (examples)
<a>href (without it, the link goes nowhere)target, rel, download
<img>src and altwidth, height, loading
<link> (stylesheet)rel and hrefmedia
<meta> (character set)charsetNone
<label>None, but for is needed to tie it to an input (or wrap the input inside it)class
<input>None, but name is needed for its value to be sent with a formtype (defaults to text), required, placeholder
<html>None, but lang is strongly recommendeddir
<p>, <h1>, <ul>NoneGlobal attributes only
<!-- src and alt are both mandatory; width and height are optional --> <img src="images/team-photo-london.jpg" alt="The London team outside the café" width="800" height="533">

If an image is purely decorative, keep the alt attribute but leave its value empty (alt=""). That tells screen readers to skip the image.

Global attributes

These optional attributes can be used on almost any element.

AttributePurposeExample
idA unique name for one element on the page (link targets, CSS, scripts)<h2 id="contact-details">
classA group name shared by many elements (mostly for CSS)<p class="intro highlight">
langThe language of the content<span lang="fr">bonjour</span>
titleAdvisory text, often shown as a tooltip<abbr title="HyperText Markup Language">HTML</abbr>

Semantics

Describe what the content is, not how it looks

A semantic element tells browsers, search engines and assistive technologies what a piece of content means. How it looks is the job of CSS. If you choose an element because of the way it looks by default, you’re using HTML incorrectly.

Do<h1>Hill Café</h1> <nav> <ul> <li><a href="menu.html">Menu</a></li> </ul> </nav> <button>Book now</button>
Don’t<div class="big-bold-text">Hill Café</div> <div class="menu"> <span><a href="menu.html">Menu</a></span> </div> <div onclick="book()">Book now</div>

Why it matters:

Choosing the right element

This content…UseNot
The main title of the page<h1><p> with big text
Section headings, in order (h2 under h1, h3 under h2)<h2>–<h6>Skipping levels to get a smaller size
A paragraph of text<p>Text with <br><br> between blocks
A list of items<ul> or <ol> with <li>Lines starting with a typed • or a number
Main navigation links<nav><div class="nav">
Page banner and footer<header>, <footer><div id="top">, <div id="bottom">
The unique main content of the page<main><div id="content">
A self-contained piece, like a blog post<article><div class="post">
Something that triggers an action<button><div> or <span> with a click handler
Strong importance / emphasis<strong> / <em><b> / <i> used just for style
Tabular data (rows and columns)<table>Tables used for page layout

The div (block) and span (inline) elements have no meaning. They’re fine as generic wrappers for styling when no semantic element fits, but they should be your last choice, not your first.

Nesting and family relationships

Elements go inside other elements

Placing one element inside another is called nesting. The inner element must be fully contained by the outer one, which means you must close elements in the reverse order to the one you opened them in. Indent each level so that the structure is easy to see.

Do<p> I love <strong><em>coffee</em></strong>. </p>
Don’t (overlapping)<p> I love <strong><em>coffee</strong></em>. </p>

Think of a set of nested boxes. The last box you open is the first one you close.

Parents, children, ancestors, descendants and siblings

Nesting creates a family tree, and this is the structure that CSS selectors and JavaScript rely on. Take this code, for example:

<body> <header> <h1>Hill Café</h1> </header> <main> <p>Welcome to <strong>Hill Café</strong>.</p> </main> </body>

As a tree, it looks like this:

TermMeaningExample from the code above
ParentThe element directly containing anothermain is the parent of p
ChildAn element directly inside anotherh1 is a child of header
AncestorAny element above, at any distance: parent, grandparent and so onbody is an ancestor of strong
DescendantAny element below, at any depth: children, grandchildren and so onstrong is a descendant of main and of body
SiblingElements with the same parentheader and main are siblings

Every parent is an ancestor, but not every ancestor is a parent. In the same way, every child is a descendant, but not every descendant is a child. The html element is the root of the tree, so it’s the ancestor of every other element on the page.

Some nesting is not allowed

Each element has rules about what it may contain and where it may sit. If you break those rules, browsers will silently rearrange your code.

RuleExample of a mistake
li belongs inside ul or ol, and a list’s direct children should be li<ul><p>Item</p></ul>
p can hold only inline content (text, strong, a, img), not other blocks<p><div>Hi</div></p>
A link can’t contain another link<a href="a.html"><a href="b.html">Go</a></a>
Headings can’t sit inside p<p><h2>Title</h2></p>

A complete page skeleton

<!DOCTYPE html> <html lang="en-GB"> <!-- root element, with an attribute --> <head> <meta charset="utf-8"> <!-- void element: no closing tag --> <title>Hill Café</title> <!-- container element --> <link rel="stylesheet" href="css/main-styles.css"> <!-- void --> </head> <body> <header> <h1>Hill Café</h1> </header> <main> <p>Welcome to <strong>Hill Café</strong>.</p> <img src="images/hero-mountains.jpg" alt="Snowy peaks"> </main> </body> </html>

Note that <!DOCTYPE html> isn’t an element. It’s a declaration that tells the browser to use modern HTML.

Check before you upload

Learn more

Tip: run your page through the W3C Markup Validator to catch any missing tags, bad nesting or missing mandatory attributes.