Skip to content
Kuza Ndoto Academy

Elements & Attributes

BeginnerChapter 1 · Page 5 of 254 min readUpdated 15 Nov 2026

An HTML page is made of elements: a heading, a paragraph, a link, an image. Most elements are written with an opening tag, some content and a closing tag, and many can be configured with attributes. Getting this vocabulary right makes everything else in HTML easier.

Anatomy of an element

<a href="/contact">Contact us</a>

This line creates a link. Here are its parts:

Attribute name Attribute value <a href ="contact.html" > Contact us </a> Opening tag Content Closing tag The whole thing is one element
Figure 1. Attributes always go inside the opening tag. The closing tag repeats the element name with a forward slash.
  • The opening tag <a …> starts the element and names it (a stands for anchor).
  • The content is what the user sees and clicks: "Contact us".
  • The closing tag </a> ends it. Note the forward slash.
  • The element is the whole thing: tags plus content.

People often say "tag" and "element" as if they were the same. Strictly, tags are the markers in angle brackets; the element is what they create.

Void elements

Some elements have no content, so they have no closing tag. These are called void elements. The most common are <img>, <br>, <hr>, <input>, <meta> and <link>.

<img src="logo.png" alt="Kuza Ndoto logo">
<br>
<input type="text" name="town">
Note

You may see void elements written with a slash, such as <br />. In HTML the slash is allowed but does nothing; it is a habit from an older language called XHTML. Both forms work. Just be consistent.

Nesting elements

Elements can contain other elements. This is called nesting. The rule is simple: the last element opened is the first one closed, like boxes inside boxes.

<!-- Correct: strong opens inside p and closes inside p -->
<p>Fares rise at <strong>rush hour</strong> in Nairobi.</p>

<!-- Wrong: the tags overlap -->
<p>Fares rise at <strong>rush hour</p></strong>

Browsers try to repair overlapping tags, but each browser's guess may not match what you meant, and styles or scripts can then behave strangely. Indenting nested elements helps you spot mistakes.

Some combinations are not allowed at all. For example, a paragraph cannot contain a heading or another paragraph. If you write <p> inside an open paragraph, the browser automatically closes the first paragraph before starting the new one.

Attributes

Attributes give extra information or settings to an element. They are written inside the opening tag as name="value", separated by spaces, in any order.

<a href="https://www.example.com" title="Opens the example site">Visit</a>
<img src="team.jpg" alt="Our robotics team in Kampala" width="600" height="400">

Some attributes belong to particular elements (href for links, src and alt for images). Others are global attributes that work on any element:

AttributePurposeExample
idA unique name for one elementid="contact"
classOne or more group names, used by CSS and JavaScriptclass="card featured"
langLanguage of the contentlang="sw"
titleExtra advisory text, often shown as a tooltip on hovertitle="Kenya Shillings"
hiddenHides the elementhidden

Boolean attributes

Some attributes are simply on or off. Their presence means "true", and leaving them out means "false". You don't need a value:

<input type="checkbox" checked>
<button disabled>Pay now</button>
Common mistake

Writing disabled="false" does not enable the button. The attribute is present, so the button is still disabled. To turn a boolean attribute off, remove it completely.

Quotes and case

  • Always put attribute values in double quotes. Quotes are optional for simple values, but values containing spaces need them, and quoting everything avoids bugs.
  • Element and attribute names are not case-sensitive, but the convention is lowercase: <p>, not <P>.

Try it

This page uses nesting, a void element, boolean attributes and global attributes. Try removing checked or disabled, then press Run.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Elements and attributes</title>
  </head>
  <body>
    <h1 id="top">Matatu fare checker</h1>
    <p>Fares are quoted in <abbr title="Kenya Shillings">KES</abbr> and rise at <strong>rush hour</strong>.</p>
    <hr>
    <p><input type="checkbox" checked> Show peak fares</p>
    <p><button disabled>Check fare</button></p>
    <p><a href="#top">Back to top</a></p>
  </body>
</html>

You'll see a heading, a sentence where "rush hour" is bold, a horizontal line, a ticked checkbox and a greyed-out button that can't be clicked. Hover over "KES" to see the tooltip from its title attribute.

Key points

  • An element is usually an opening tag, content and a closing tag; void elements such as <img> have no closing tag.
  • Close nested elements in reverse order: last opened, first closed.
  • Attributes go in the opening tag as name="value"; global attributes like id and class work on every element.
  • Boolean attributes are true when present and false only when removed.
Up next · New chapter: Text & ContentHeadings & ParagraphsStructure text with headings, paragraphs and line breaks.
→

Tip: use the ← and → keys to move between pages.