Free, independent guides since 2010

Code

HTML attributes

What HTML attributes are and how to use them: global attributes like id, class and lang, plus href, src, alt and more, with examples.

<page section="Code" type="guide" read="2 min"/>

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:

HTML
<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, required or controls.

Global attributes

These attributes can be used on every HTML element:

AttributeWhat it doesExample
idA unique name for one element, used for links to a section, CSS and JavaScript<h2 id="prices">
classOne or more names to style or select groups of elements<p class="intro note">
styleInline CSS for this element (better kept in a stylesheet)style="color: maroon"
titleExtra advisory information, often shown as a tooltiptitle="Opens our price list"
langThe language of the content<p lang="nl">Hallo</p>
hiddenHides the element<div hidden>
data-*Your own data for scriptsdata-price="19.95"
tabindexControls keyboard focus order (use with care)tabindex="0"

Important element-specific attributes

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.