Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
HTML Syntax  ›  Lesson

Tags, Elements and Attributes

Tags and Attributes 12 minAn element is an opening tag, its content and a closing tag, and attributes inside the opening tag add extra information as name and value pairs.

Tags, Elements and Attributes

Every HTML page, however large, is built from one repeating shape. Once you can see that shape, you can read any page's source and spot mistakes such as a missing closing tag straight away.

The rule

Open, content, close; extra details ride in the opening tag.

An element has three parts:

<p class="note">Bring an umbrella.</p>
  • <p class="note"> is the opening tag. The element's name, p, sits right after <.
  • Bring an umbrella. is the content.
  • </p> is the closing tag: same name, with a slash.

The class="note" part is an attribute. Attributes add information about the element and always follow the pattern name="value". They go only in the opening tag, separated by spaces. Quote your values with double quotes; it is the most common style and it avoids trouble when a value contains spaces.

Elements can hold other elements. This is called nesting, and the rule is that the last one opened is the first one closed:

<p>This is <strong>very</strong> important.</p>

Some elements have no content at all, so they have no closing tag. These are void elements, for example <img>, <br>, <hr>, <input> and <meta>:

<img src="cat.jpg" alt="A ginger cat asleep on a chair">

A few attributes are boolean: their presence alone means "yes". <input disabled> is disabled; there is no need to write a value.

The shortcut

Read an element like a sandwich: find the name, check the attributes, match the close.

Checking a line of HTMLStep 1 of 4
  1. Find the name

    <a href="/help" target="_blank">Help</a>: the name is a, a link.

The trap is crossed nesting such as <p><em>text</p></em>. Matching closes in reverse order catches it: em opened last, so it must close first.

People often use "tag" and "element" as if they meant the same thing, and in casual talk that is fine. When precision matters, remember the difference: tags are the markers you type, and the element is the whole thing they create, content included. You write tags; the browser builds elements.

Drills

Answer fast — trust the rule.

quick drill

In <img src="logo.png" alt="Pebells logo">, what is alt?

quick drill

Which line is correctly nested?

quick drill

Where can an attribute appear?

quick drill

Which of these is a void element that never has a closing tag?

quick drill

What does <button disabled>Send</button> do?

Remember

  • Element = opening tag + content + closing tag; attributes live in the opening tag as name="value".
  • Shortcut: find the name, check the attributes, match the close, in reverse order when nested.
  • Trap: crossed nesting, and giving void elements like <img> a closing tag.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDissect an element

Take this line: <a href="/about" title="Who we are">About us</a>. Name every part of it (element, tags, attributes, values, content) and explain what each part does.