Free HTML tutorial

HTML Attributes: Add Information to Elements

Learn HTML attribute syntax with examples of href, src, alt, id, class and boolean attributes.

Write an attribute

Attributes provide additional information about an element. They are written in the opening tag as a name and, for many attributes, a value.

<a href="https://example.com/">Visit the example site</a>

Quote attribute values, especially when they contain spaces. Quoted values are consistent and easy to read.

Common attributes

  • href gives a link its destination.
  • src identifies a resource such as an image file.
  • alt provides alternative text for an image. Use an empty value for decorative images.
  • id gives an element a unique identifier in the document.
  • class assigns one or more reusable class names.
<img src="images/flower.jpg" alt="Pink flower in a garden">
<button type="button" disabled>Unavailable</button>

Boolean attributes

A boolean attribute is true when it is present. For example, adding disabled disables a form control; leaving it out means the control is not disabled by that attribute.

Attributes have element-specific rules. Check that an attribute is supported on the element where you use it.