Lesson 8 of 51 · Module 2, lesson 2 of 9 · 4 min

Attributes

Add extra information to elements with attributes, and write them correctly.

What you'll learn

  • What an attribute is and how to write one correctly
  • How to use several attributes on one element
  • What a boolean attribute is

Why this matters

Many elements need extra information to work. A link needs to know where it goes. An image needs to know which file to show. That information is written in attributes. You already used two — lang="en" and charset="UTF-8" — so you know what they look like.

How to write an attribute

An attribute goes inside the opening tag, after the element name:

html
<p lang="fr">Bonjour !</p>
  • lang is the attribute name.
  • "fr" is the attribute value. It goes in quotes.
  • An equals sign = joins them, with no spaces around it.

This attribute tells the browser (and screen readers) that this one paragraph is in French, even if the rest of the page is in English. A screen reader then pronounces it with a French voice.

A complete example

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Greetings</title>
</head>
<body>
  <h1>How to say hello</h1>
  <p>In English: Hello!</p>
  <p lang="fr">En français : Bonjour !</p>
  <p lang="es">En español: ¡Hola!</p>
  <p lang="ar" dir="rtl">بالعربية: مرحبا!</p>
</body>
</html>

What you'll see: four lines of greeting. The Arabic line is on the right side of the page, because Arabic is written from right to left.

A page titled "How to say hello" with four greetings: "In English: Hello!", a French line, a Spanish line, and an Arabic line that is aligned to the right side of the page.

Several attributes on one element

The last paragraph has two attributes: lang="ar" and dir="rtl". dir means direction. rtl means right to left. Separate attributes with a space. The order does not matter.

html
<p lang="ar" dir="rtl">مرحبا</p>

Rules for writing attributes

  • Always use quotes around the value. Double quotes "…" are the usual choice.
  • Names in lower case: lang, not LANG.
  • Values keep their meaning: some values must be exact (dir="rtl"), and others are free text that you write normally, with capital letters where you need them.
  • Only in the opening tag. Never write an attribute in a closing tag.

Boolean attributes

Some attributes have no value at all. If the attribute is there, the feature is on. If it is missing, the feature is off. These are called boolean attributes.

html
<p hidden>This paragraph is hidden.</p>

The hidden attribute hides an element from the page. You will meet more boolean attributes in forms, such as required.

The title attribute — use it rarely

You may see title on many elements:

html
<p title="A short note">Point at this text.</p>

When you point at the text with a mouse, a small tooltip appears. But people using a phone, a keyboard or a screen reader usually never see it. So never put important information only in a title.

Try it yourself

  1. Select the greetings example and choose Try it.
  2. Add a line in another language you know, with the right lang code. (Examples: de German, pt Portuguese, hi Hindi, zh Chinese, sw Swahili.)
  3. Add hidden to the English paragraph. It disappears from the preview. Remove it again.

Common mistakes

  • Missing quotes, such as <p lang=fr>. It often works, but a value with a space in it breaks. Fix: always use quotes.
  • Missing closing quote, such as <p lang="fr>. Everything after it becomes part of the value, and text disappears from the page. How to spot it: in VS Code, the colours of the rest of the line change. Fix: close the quote.
  • Space around the equals sign, such as lang = "fr". It works, but it is not the usual style. Fix: lang="fr".
  • Putting the attribute in the closing tag, such as </p lang="fr">. It is ignored. Fix: move it to the opening tag.

Check yourself

  1. In <p lang="es">Hola</p>, what is the attribute name and what is its value?
  2. How do you separate two attributes on the same element?
  3. What is a boolean attribute? Give one example.

Answers

  1. The name is lang. The value is es (Spanish).
  2. With a space.
  3. An attribute with no value: if it is there, the feature is on. For example hidden (or, in forms, required).

Next: now that you can write elements and attributes, you will use the most important elements on any page — headings — and learn why their order matters.

Chat with us on WhatsApp