Favorites

Attributes - HTML Tutorial

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee

Attributes give an element extra information. They can change how an element behaves, where a link goes, which image is displayed and much more. You used one in Tinkering when you added style to the body.

How Attributes Are Written

Attributes always go inside the opening tag, after the element name:

<a href="https://www.wikipedia.org/">Visit Wikipedia</a>

Each attribute has two parts:

  • a name, here href, which says what kind of information this is
  • a value in quotes, here "https://www.wikipedia.org/", which is the information itself

The name and value are joined with an equals sign. In this example, the a (anchor) element creates a link, and the href attribute tells the browser where the link goes. Without href, the a element isn't a working link at all.

Rules for Attributes

  • Put attributes in the opening tag only. Closing tags never have attributes.
  • Separate attributes with a space. An element can have several attributes, in any order.
  • Always put values in quotes. Quotes are sometimes optional, but values containing spaces or special characters break without them. Using quotes every time avoids problems.
  • Write attribute names in lowercase. Names aren't case-sensitive, but lowercase is the convention.
  • Be careful with the case of values. Some values are case-sensitive. File names and class names are good examples: on many web servers, Photo.jpg and photo.jpg are different files.

Here's an element with more than one attribute. Long tags can be split over several lines to make them easier to read:

Example: Multiple Attributes
<a href="https://www.gutenberg.org/"
   title="Free ebooks from Project Gutenberg"
   lang="en">Public domain books</a>

Boolean Attributes

Some attributes don't need a value. Simply including them switches a feature on. These are called boolean attributes. For example, the checked attribute makes a checkbox start out ticked:

<input type="checkbox" checked>

You'll use a few of these in the Forms chapter.

Global Attributes

Each element has its own set of attributes it understands. For example, href only makes sense on links, and src only makes sense on elements such as images that load a file.

A few attributes, called global attributes, work on almost any element. The most common are:

  • id gives an element a unique name on the page.
  • class puts an element into a named group, usually for styling.
  • style adds CSS directly to an element.
  • lang sets the language of the element's content.
  • title adds advisory information, often shown as a tooltip.

You'll use id and class a lot when you start styling pages in Classes and IDs.

When Attributes Go Wrong

Just like unknown elements, browsers quietly ignore attributes they don't recognize. If you type hreff instead of href, your link won't work and you won't see an error. If something isn't behaving, check the spelling of your attribute names and make sure every value has an opening and a closing quote.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee