HTML elements describe what something is: a paragraph, a link, an image. Attributes add the details: where the link goes, which image to show, what language the text is in. If you have read our HTML basics, you have already used them without knowing it.
How attributes are written
Attributes always go inside the opening tag, after the element name. Each one has a name and, usually, a value between quotes:
<a href="https://www.example.com/" title="Visit our example site">Example</a>
<img src="logo.png" alt="Company logo" width="200" height="80">- Separate attributes with a space; their order does not matter.
- Use double quotes around values. Single quotes work too, but be consistent.
- Attribute names are not case-sensitive, but lowercase is the convention.
- Some attributes are boolean: their presence alone switches something on, like
disabled,requiredorcontrols.
Global attributes
These attributes can be used on every HTML element:
| Attribute | What it does | Example |
|---|---|---|
id | A unique name for one element, used for links to a section, CSS and JavaScript | <h2 id="prices"> |
class | One or more names to style or select groups of elements | <p class="intro note"> |
style | Inline CSS for this element (better kept in a stylesheet) | style="color: maroon" |
title | Extra advisory information, often shown as a tooltip | title="Opens our price list" |
lang | The language of the content | <p lang="nl">Hallo</p> |
hidden | Hides the element | <div hidden> |
data-* | Your own data for scripts | data-price="19.95" |
tabindex | Controls keyboard focus order (use with care) | tabindex="0" |
Important element-specific attributes
hrefon<a>– the destination of a link. See HTML links and anchors.src,alt,widthandheighton<img>– see images in HTML and the alt attribute for SEO.type,name,value,placeholderandrequiredon form fields – see making forms in HTML.colspanandrowspanon table cells – see using tables in HTML.langon<html>– tells browsers and screen readers the main language of the page.contentandnameon<meta>– see meta elements in HTML.
Old attributes to avoid
Older tutorials use presentational attributes such as bgcolor, align, border on tables and <font color>. They are obsolete in HTML5. Use CSS instead – see what is CSS and using color in HTML.
