HTML Attributes
HTML attributes are extra pieces of information written inside an element's opening tag. They change how the element behaves or give the browser details it needs: where a link goes, which image to load, what kind of input to show, or which language the page is in. They are written as name="value" pairs, and some, called boolean attributes, work just by being present.
Writing attributes correctly
<a href="/contact" title="Send us a message">Contact</a>
<img src="team.jpg" alt="Five people around a whiteboard" width="640" height="360">
<input type="email" name="email" id="email" placeholder="you@example.com" required>
<button type="submit" disabled>Sending...</button>
Put attributes only in the opening tag, separate them with spaces, and wrap values in double quotes. Attribute names are not case-sensitive, but lowercase is the convention. Each attribute should appear only once per element.
Global attributes work on any element
| Attribute | Purpose |
|---|---|
id | A unique identifier for one element on the page, used by CSS, JavaScript, labels and in-page links |
class | One or more space-separated names for styling and scripting groups of elements |
style | Inline CSS; handy for quick tests, but hard to maintain |
title | Extra advisory text, often shown as a tooltip |
lang | The language of the content, such as en or hi |
hidden | Hides the element from display and assistive technology |
data-* | Custom data for your scripts, read in JavaScript through element.dataset |
tabindex | Controls whether and how an element can receive keyboard focus |
Element-specific attributes
Many attributes belong to particular elements. href and target belong to links. src, alt, width and height belong to images. Form controls use type, name, value, placeholder, required, min, max and pattern. The for attribute on a <label> must match the id of its input, which makes the label clickable and readable by screen readers.
Boolean attributes
Attributes like required, disabled, checked, readonly and autofocus are on when present and off when absent. Writing disabled="false" does not enable the element; the attribute is still there, so it is still disabled. To turn it off you must remove the attribute entirely.
Custom data attributes in practice
<button class="add-to-cart" data-product-id="42" data-price="499">Add</button>
<script>
document.querySelector('.add-to-cart').addEventListener('click', (e) => {
const { productId, price } = e.currentTarget.dataset;
console.log(productId, Number(price));
});
</script>
Hyphenated names become camelCase in dataset, so data-product-id is read as dataset.productId. Values always come back as strings.
Mistakes worth avoiding
Missing or useless alt text is the most frequent one. Every content image needs an alt that describes it; purely decorative images should have an empty alt="" so screen readers skip them. Leaving the attribute out makes some screen readers read the file name aloud.
Reusing the same id on several elements is another. Browsers will not complain, but getElementById returns only the first, labels attach to the wrong input and in-page links jump to the wrong spot. Use class for anything that repeats.
Also be careful with target="_blank". Modern browsers now treat it as if rel="noopener" were set, but adding rel="noopener" yourself keeps older browsers safe too.
Key points
- Attributes go in the opening tag as quoted name-value pairs.
idmust be unique;classcan repeat.- Boolean attributes work by presence, not by value.
- Give every meaningful image a descriptive
alt.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.