Menu

HTML Attributes: Syntax, class, id, data-* and Boolean Ones

Attributes add information to an HTML element: where a link goes, what an image shows, which CSS class applies. Learn the syntax, the global attributes every element accepts, class and id, boolean attributes and data-* attributes.

This page includes runnable editors - edit, run, and see output instantly.

What HTML attributes are

An attribute adds information to an element. It goes inside the opening tag, written as a name, an equals sign and a value in quotes: name="value". An element can have any number of attributes, separated by spaces.

href tells the link where to go, src and alt describe the image, type, placeholder and required configure the input. Each element has its own set of attributes, and a few (the global attributes below) work on every element.

Syntax rules

<input type="text" name="city" value="Paris" disabled>
  • Name, equals sign, quoted value. Double quotes are the convention; single quotes work too (title='Say "hi"' is how you put a double quote inside a value).
  • Names are case insensitive. CLASS and class are the same attribute. Values are not: class="Box" and class="box" are different classes.
  • Quotes are optional without spaces, but quote anyway. class=big box gives the element the class big and a second, meaningless attribute called box.
  • Order does not matter. <img alt="" src="a.png"> is the same as <img src="a.png" alt="">.
  • Each name once per element. If an attribute appears twice, the browser keeps the first and silently drops the second.

Boolean attributes

Some attributes are on or off. For these, presence means true, and the value is ignored. disabled, checked, required, readonly, hidden, multiple, autofocus and open all work this way.

The third button is disabled even though the value says false, because the browser only checks whether the attribute is there. To turn a boolean attribute off, remove it. In JavaScript, button.disabled = false does exactly that.

The class attribute

class gives an element one or more names that CSS and JavaScript use to find it. Several elements can share a class, and one element can have several classes, separated by spaces:

Each extra class adds its rules on top. In CSS a class is selected with a dot (.card); in JavaScript, element.classList.add('featured') and classList.remove change them. Classes are the normal way to style things: most of your CSS will target classes.

The id attribute

id gives an element a name that must be unique on the page. Use it when you need exactly one element: a link target, a <label for>, or document.getElementById.

Clicking the label focuses the input, because for="email" points at id="email". The link scrolls to #pricing because the fragment in the href matches the id.

classid
How many elements can share itAny numberOne (unique)
Values per elementSeveral, space separatedOne
CSS selector.name#name
Typical useStyling groups of elementsLink targets, labels, one specific element in JavaScript

An id cannot contain spaces. Start it with a letter to keep it easy to use in CSS: #2col is not a valid CSS selector without escaping.

Global attributes

These attributes work on any element:

AttributeWhat it does
idUnique name for the element
classSpace separated list of class names
styleInline CSS, like style="color: red"
titleTooltip text shown on hover
langLanguage of the content, like lang="fr"
dirText direction: ltr, rtl or auto
hiddenHides the element (boolean)
tabindexMakes an element focusable with Tab, or sets its order
contenteditableLets the user edit the element's text
data-*Your own data, read with JavaScript

The lang attribute has its own page because it affects screen readers, translation and search.

data-* attributes

When you need to store your own value on an element, prefix the name with data-. Any name works after the prefix, and JavaScript reads the values through element.dataset, with dashes turned into camelCase:

data-product-id becomes dataset.productId. Values always come back as strings, so convert with Number() before doing arithmetic. CSS can select on them too: [data-price="4.50"].

Common mistakes

  • Writing class twice. <div class="a" class="b"> keeps only a. Put both in one attribute: class="a b".
  • Reusing an id. Two elements with the same id break #links, labels and getElementById, which returns only the first. Use a class for anything repeated.
  • disabled="false" or checked="false". The attribute is still present, so it is still on. Remove it instead.
  • Unquoted values with spaces. alt=My photo sets alt to My. Quote every value.
  • Inventing attributes without data-. <div price="4"> is invalid HTML and may clash with a future standard attribute. Use data-price.

Frequently Asked Questions

What is an HTML attribute?

An attribute is extra information written inside an opening tag as name="value". It configures the element: href sets where a link goes, src and alt describe an image, class and id let CSS and JavaScript find the element.

What is the difference between class and id in HTML?

An id must be unique on the page and identifies one element, for #fragment links, <label for> and getElementById. A class can be shared by many elements, and one element can have several classes separated by spaces. Use classes for styling.

How do I add multiple classes to an HTML element?

Put them in one class attribute separated by spaces: <div class="card featured dark">. Writing class twice does not work: the browser keeps only the first one.

Do HTML attributes need quotes?

Quotes are optional when the value has no spaces or special characters, but always quoting is the safe habit. class=big box gives the element the class big plus a separate attribute named box.

Why is my button still disabled with disabled="false"?

disabled is a boolean attribute: its presence means true, whatever the value says. To enable the element, remove the attribute, or set button.disabled = false in JavaScript, which removes it.

Can I make up my own HTML attributes?

Use the data- prefix: data-price="12" or data-user-id="7" are valid on any element and readable in JavaScript through element.dataset. Invented names without the prefix are invalid HTML.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED