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).
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 element
Void element
Tags
Opening and closing
One tag only
Content
Yes: text and/or other elements
None
Common examples
ph1–h6aullidivsectionbuttonstrong
imgbrhrinputmetalinksourcewbr
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
Rule
Example
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 quotes
class="intro"
Use lowercase attribute names
href, not HREF
Order of attributes doesn’t matter
<img alt="Cat" src="a.jpg"> is fine
Use each attribute only once per element
class="a b", not class="a" class="b"
Boolean attributes need no value; their presence turns them on
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.
Element
Mandatory
Optional (examples)
<a>
href (without it, the link goes nowhere)
target, rel, download
<img>
src and alt
width, height, loading
<link> (stylesheet)
rel and href
media
<meta> (character set)
charset
None
<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 form
type (defaults to text), required, placeholder
<html>
None, but lang is strongly recommended
dir
<p>, <h1>, <ul>
None
Global 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.
Attribute
Purpose
Example
id
A unique name for one element on the page (link targets, CSS, scripts)
<h2 id="contact-details">
class
A group name shared by many elements (mostly for CSS)
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.
Accessibility. Screen readers announce headings, lists, landmarks and buttons, and they let users jump between them.
Built-in behaviour. A button can be reached with the keyboard and activated with Enter or Space, but a div can’t.
Search engines. Headings and document structure help search engines understand the page.
Maintainability. Meaningful elements make your code easier to read and easier to style.
Choosing the right element
This content…
Use
Not
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
└── main
└── p
└── strong
Term
Meaning
Example from the code above
Parent
The element directly containing another
main is the parent of p
Child
An element directly inside another
h1 is a child of header
Ancestor
Any element above, at any distance: parent, grandparent and so on
body is an ancestor of strong
Descendant
Any element below, at any depth: children, grandchildren and so on
strong is a descendant of main and of body
Sibling
Elements with the same parent
header 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.
Rule
Example 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.