Thuta Learning
HTML
BasicWeb DevelopmentBeginner

Attributes

HTMLLesson 625 min

What you'll walk away with

  • Write attributes inside the start tag with proper quotes
  • Tell global attributes such as id, class and lang from element-specific ones such as href, src and alt
  • Explain that a boolean attribute is switched on simply by being present
  • Keep every id unique on a page and give images meaningful alt text

Explain it simply

Explain it simply

The element says what something is (a link, an image, a button), and its attributes give the details. They are like the labels on a parcel: "deliver to", "fragile", "this side up". Attributes tell a link where to go (href), an image where its file is (src), and what to say to someone who cannot see the image (alt).

Attributes go inside the start tag as name="value", separated by spaces. They never go in the end tag.

Problem → Why → Solution

The problem: disabled="false" and the button is still disabled

On a payment page you want the "Pay now" button enabled, so you write disabled="false". You want the subscribe checkbox unticked, so you write checked="no". In the browser the button is greyed out and cannot be clicked, and the checkbox is already ticked. Customers cannot pay and leave.

checkout.html
<button type="submit" disabled="false">Pay now</button>
<input type="checkbox" checked="no"> Subscribe to offers

Why does this happen?

disabled and checked are boolean attributes. For a boolean attribute the value does not matter: if the attribute is present, it is true; if it is absent, it is false. So disabled="false" means exactly the same as disabled. To switch it off, remove the attribute entirely.

Core concept: attribute rules and kinds

Attributes go in the start tag after the element name, in any order. Write attribute names in lowercase and always put values in double quotes: without quotes, the value ends at its first space. Each attribute may appear only once per element; write class twice and the browser keeps the first and ignores the second. Single quotes also work, which helps when the value itself contains a double quote. id values are case-sensitive: Intro and intro are two different ids, so choose one spelling and stick to it.

AttributeUsed onWhat it does
idany element (global)a name unique in the whole page
classany element (global)group names for CSS and JS, several allowed, space-separated
href<a>the address the link goes to
src / alt<img>the image file and a text description of it
for<label>ties the label to the input with that id
disabled / checkedform controls (boolean)on when present, off when absent

Global attributes (id, class, lang, title, hidden) work on every element. Element-specific attributes only mean something on their own element: <p href="..."> raises no error but does nothing. An id may be used only once per page, while a class can be repeated on as many elements as you like.

alt is the text that replaces the image for people who cannot see it: screen reader users, or anyone whose image failed to load. Describe what the image communicates, not "image" or the file name. A purely decorative image gets alt="" (empty) so screen readers skip it. width and height reserve the space before the image loads, which stops the page from jumping. target="_blank" opens a link in a new tab.

Mental model: an element and its settings
  1. Element says what it is
  2. Attributes add the details
  3. Browser reads each name="value"
  4. Boolean: present on, absent off

You cannot switch a boolean attribute off by changing its value; you remove it.

General pattern

html
<element name="value" other-name="value">content</element>

<element boolean-attribute>content</element>

<voidelement name="value">

Attributes live only in the start tag, separated by spaces. A boolean attribute is written as its name alone, with no value. Void elements take attributes too.

Example: attributes of every kind

This example has global attributes (id, class), link and image attributes, a for/id pair that ties a label to an input, and two boolean attributes (checked, disabled). Put a popa.jpg in the same folder to see the picture; without it you will see a broken-image icon and the alt text instead.

attributes.html
<p id="intro" class="lead">Learn HTML with small, working examples.</p>
<p><a href="https://developer.mozilla.org/" target="_blank">Open MDN Web Docs</a></p>
<p><img src="popa.jpg" alt="Mount Popa at sunrise" width="300" height="200"></p>
<p>
  <input type="checkbox" id="news" checked>
  <label for="news">Email me new lessons</label>
</p>
<p><button type="button" disabled>Send</button></p>

Code breakdown

  1. Line 1<p id="intro" class="lead">

    id="intro" names this one paragraph uniquely, so a URL ending in #intro jumps straight to it. class="lead" can be reused on many elements for styling with CSS later.

  2. Line 2<a href="https://developer.mozilla.org/" target="_blank">

    href is the destination and target="_blank" opens it in a new tab. Modern browsers automatically stop the newly opened page from controlling the original one for links like this.

  3. Line 3<img src="popa.jpg" alt="Mount Popa at sunrise" width="300" height="200">

    src is the image file, alt describes it in words, and width and height reserve its space before it arrives. img is a void element, so there is no end tag.

  4. Lines 5-6<label for="news">Email me new lessons</label>

    for="news" connects the label to the checkbox whose id is news, so clicking the text toggles the box and screen readers announce the box with the label's words. checked is boolean, so it has no value.

  5. Line 8<button type="button" disabled>Send</button>

    Because disabled is present, the button cannot be clicked or reached with Tab. To enable it, delete the disabled attribute.

You should see
You see the first paragraph; the link "Open MDN Web Docs", which opens in a new tab; a 300 by 200 image (or the alt text "Mount Popa at sunrise" if popa.jpg is missing); a ticked checkbox with the label "Email me new lessons", where clicking the label also toggles the box; and a greyed-out "Send" button that cannot be clicked.

data-* attributes for your own data

To store information that no standard attribute covers, use a name starting with data-, such as data-product-id="42". The browser neither shows these nor gives them any meaning; they are there for JavaScript to read. Inventing your own names, such as price="25", fails validation and could clash with a future standard attribute.

Do not rely on the title attribute

title="..." shows a small tooltip when the mouse rests on an element. Touch screens have no mouse to hover, though, and keyboard users and many screen readers do not get it reliably either. Put important information in visible text on the page instead.

In real projects

How this is used in real projects

In real projects, class is the main bridge between HTML and CSS: design systems combine many classes, as in class="btn btn-primary". id is used for links to a section of the page (#pricing), for tying labels to inputs and for JavaScript to find one specific element. In React you write className instead of class, but it still becomes a class attribute in the browser.

In accessibility audits, images without alt and inputs without a connected label are among the most frequent findings, and tools such as Lighthouse check for them automatically. In online shops, disabling the "Add to cart" button when an item is out of stock, and storing a product id in a data-* attribute for JavaScript to add to the cart, are everyday patterns.

Common mistakes

Common mistakes

1. Using the same id twice

Copy a card and you may end up with two id="email" elements. Both labels then point to the first input, and #email links and getElementById() also find only the first one, so clicking the second label focuses the wrong field. Keep every id unique on the page.

2. Leaving a value unquoted

With <img alt=Mount Popa at sunrise>, the alt becomes just "Mount", and Popa, at and sunrise turn into three meaningless attributes. Put every value in double quotes.

3. Filling alt with useless text

alt="image", alt="IMG_2031.jpg" or a long pasted caption tells a screen reader user nothing. Write briefly what the image says on this page, or use alt="" for a decorative image.

Best practices

Best practices

  • Name ids and classes after meaning, not looks: class="error-message" beats class="red-text".
  • Write multi-word names in lowercase with hyphens (contact-form) and never with spaces.
  • Put each attribute of a long tag on its own line so diffs and reviews stay readable.
  • When writing alt, ask yourself what you would say if you had to read this image aloud over the phone.

Mini challenge

Mini challenge: a product card

Write a product card for a Bagan lacquer bowl that uses several kinds of attributes correctly.

  • A class on the article and a page-unique id on the h2
  • An img with src, meaningful alt, width and height
  • A price paragraph with two classes, separated by a space
  • A link that opens in a new tab and a disabled "Sold out" button
product.html
<!-- TODO: article with a class -->
  <!-- TODO: h2 with a unique id -->
  <!-- TODO: img with src, alt, width and height -->
  <!-- TODO: price paragraph with two classes -->
  <!-- TODO: link that opens in a new tab -->
  <!-- TODO: button that is disabled -->
<!-- TODO: close the article -->

Expected: You see a card with a heading, an image (or its alt text), a price, a link that opens in a new tab and a "Sold out" button that cannot be clicked.

Show hint

When a tag with many attributes gets too long, you can put each attribute on its own line. Write disabled without a value.

Show solution
product.html
<article class="product">
  <h2 id="lacquer-bowl">Lacquer bowl from Bagan</h2>
  <img
    src="lacquer-bowl.jpg"
    alt="Red and black lacquer bowl with a fine gold pattern"
    width="320"
    height="240">
  <p class="price on-sale">25,000 Ks</p>
  <p>
    <a href="https://en.wikipedia.org/wiki/Burmese_lacquerware" target="_blank">
      Learn about Burmese lacquerware
    </a>
  </p>
  <button type="button" disabled>Sold out</button>
</article>

Quick quiz 1 / 3

Can you click <button disabled="false">Pay</button>?

Quick quiz 2 / 3

In <img src="popa.jpg" alt=Mount Popa at sunrise>, what alt value does the browser get?

Quick quiz 3 / 3

Which attribute is meant to be repeated on many elements in one page?

What to remember

  • Write attributes in the start tag as name="value", always with double quotes.
  • Global attributes (id, class, lang) work everywhere; href, src and alt only on their own elements.
  • Boolean attributes (disabled, checked) are on when present; remove them to switch them off.
  • An id is unique per page, a class can repeat, and every image needs meaningful alt text.

HTML Living Standard — HTML Syntax — WHATWG

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Attributes | Thuta Learning