Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 122 Β· Getting Started

Attributes

Learn how attributes add extra information to HTML elements. Build links with href, show images with src and alt, and use global attributes like id, class, title and lang.

Tags tell the browser what a piece of content is. Attributes add the details: where a link should go, which picture to show, what language a quote is in, whether a checkbox starts ticked. Almost every useful page relies on them.

In this lesson you will learn how attributes are written, meet the ones you will use on nearly every page, and pick up the small rules about quotes, boolean values and duplicates that trip up beginners. By the end you will be able to read an opening tag full of attributes and know exactly what each one does.

Attribute syntax

Attributes always live inside the opening tag. Each one has a name, an equals sign and a value in quotes:

HTML
<a href="https://example.com/docs">Read the documentation</a>
  • a is the element (an anchor, better known as a link).
  • href is the attribute name. It stands for hypertext reference, the address the link points to.
  • "https://example.com/docs" is the value.

An element can have several attributes, separated by spaces, in any order:

HTML
<a href="contact.html" title="Send us a message">Contact</a>

Attributes never go in the closing tag, and they never appear on the page as text. They are settings for the element, not content.

Quoting values

HTML accepts double quotes, single quotes or, in some cases, no quotes at all. All three of these are valid:

HTML
<img src="logo.svg" alt="Company logo">
<img src='logo.svg' alt='Company logo'>
<img src=logo.svg alt=Logo>

The unquoted form is a trap. An unquoted value ends at the first space, so alt=Company logo gives the image the alt text "Company" and creates a second, meaningless attribute called logo. Double quotes everywhere is the simplest habit, and it is what this course uses.

Single quotes are handy in one situation: when the value itself contains double quotes. title='The "best" seat' works, and so does title="The &quot;best&quot; seat", which uses a character reference from HTML Entities.

Always quote attribute values. Using double quotes everywhere means you never have to think about spaces or special characters, and your code looks consistent.

Links are what put the "hyper" in HyperText. The href value can be a full web address, or a path to another file in your own site:

Links

The target="_blank" attribute asks the browser to open the link in a new tab. Use it sparingly: most people prefer to decide for themselves where a link opens, and a new tab can confuse screen reader users who don't notice the switch. You will learn the details in Links and Link Targets.

Images: src and alt

The <img> element is a void element; everything it needs is provided by attributes:

  • src (source): the address of the image file.
  • alt (alternative text): a short description of the image.
  • width and height: the image's size in pixels, written as plain numbers without px.
Images

When the image cannot load, the browser shows the alt text instead. More importantly, screen readers read it aloud to people who cannot see the picture, and search engines use it to understand what the image shows. Write alt text as if you were describing the picture to a friend on the phone. If an image is purely decorative, use an empty value, alt="", so screen readers skip it. The Images lesson goes deeper.

Set width and height. When the browser knows an image's size in advance, it can reserve the space before the file arrives. That stops the text from jumping around as the page loads. CSS can still make the image responsive later.

Global attributes

Some attributes work only on specific elements: href belongs to links, src to images and media. Others work on every element. These are called global attributes. You will meet these four constantly:

Attribute What it does
id Gives an element a unique name on the page. Useful for links that jump to a section, and for CSS and JavaScript.
class Puts the element into one or more named groups, mostly used for styling. Several elements may share a class.
title Extra advisory text, usually shown as a tooltip when hovering.
lang The language of the element's content.
Global attributes

A link whose href starts with # jumps to the element with that id on the same page. That is exactly how the "On this page" links beside this lesson work. Notice also that the second dish has two classes, dish and special, separated by a space. A class value is a space-separated list, so one element can belong to many groups.

Do not rely on title for anything important. Touch screens have no hover, and many screen readers ignore it. If information matters, put it in visible text.

Other global attributes you will meet later include hidden, tabindex, dir and custom data-* attributes. Global Attributes collects them all, and IDs, Classes and Custom Data Attributes each get a lesson of their own.

Boolean attributes

A few attributes are simply switched on by being present. They need no value at all:

HTML
<input type="checkbox" checked>
<button disabled>You cannot click me</button>
<p hidden>You will not see this paragraph.</p>

Writing checked="false" does not switch the attribute off: the attribute is still present, so it still counts. To turn it off, remove it. If you want to write a value for clarity, the only correct ones are an empty string or the attribute's own name, such as disabled="disabled".

Boolean attributes

Enumerated attributes

Some attributes accept only a fixed list of keywords. type on an input is one: text, email, checkbox, date and so on. Another is dir, which accepts ltr, rtl or auto. If you misspell the keyword, the browser silently falls back to a default. A typo like type="emial" gives you a plain text field rather than an email field, with no warning at all.

Rules and common mistakes

A few small rules explain most attribute bugs:

  • Names are not case-sensitive, but write them in lowercase. HREF works, but nobody writes it that way anymore.
  • Values can be case-sensitive. id="Menu" and id="menu" are different ids, and class names are case-sensitive too. File names in src and href often are as well, depending on the server.
  • Each attribute once per element. If you repeat an attribute, the browser keeps the first one and ignores the rest. Two class attributes do not combine; put all the classes in one.
  • Unknown attributes are ignored. A misspelled scr instead of src produces no error, just a missing image.
  • Spaces around = are allowed but unusual. Write href="x", not href = "x".
Duplicate and misspelled attributes

The console line shows that the browser kept only red from the first paragraph. Fix the image by correcting scr to src.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. Where do attributes go?

    Attributes always live inside the opening tag, for example <a href="...">.
  2. 2. Which attribute gives an image its text alternative?

    alt is read by screen readers and shown if the image fails to load. src is the image's address and title is an optional tooltip.
  3. 3. How many elements on a page may share the same id?

    An id must be unique on the page. Use class when several elements belong to the same group.
  4. 4. How do you turn off the boolean attribute in <button disabled>?

    Boolean attributes are on whenever they are present, whatever their value. To switch one off, remove it.
  5. 5. What happens with <p class="note" class="wide">?

    Repeated attributes are ignored after the first. Put several classes in one attribute, separated by spaces.

Summary

  • Attributes add details to elements and always go in the opening tag.
  • They are written as name="value", separated by spaces; always use double quotes.
  • href sets where a link goes; src and alt describe an image.
  • Global attributes such as id, class, title and lang work on any element.
  • Boolean attributes such as checked, disabled and hidden are on when present and off when absent, whatever their value.
  • Repeated attributes are ignored after the first, and misspelled ones are ignored silently.

Next: Headings and Paragraphs, where you start organizing text into a clear outline.