HTML Attributes

Beginner
⏱️ 15 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
210+ attributes

Introduction

HTML attributes are extra properties on elements—written as name="value" pairs in the opening tag. They connect your markup to CSS (class), JavaScript (data-*), accessibility (alt), and browser behavior (href, required).

This hub explains attribute syntax, groups them by purpose, links to individual guides for every attribute in our reference, and walks through six hands-on examples you can edit live.

What You’ll Learn

01

Syntax

Quoted values & booleans.

02

Global attrs

id, class, lang.

03

Form attrs

name, type, required.

04

Links & media

href, src, alt.

05

Boolean attrs

checked, disabled.

06

Examples

Real attribute patterns.

What Are HTML Attributes?

An HTML attribute modifies an element’s behavior, appearance hook, or metadata. Attributes live only in the opening tag (or the single tag of void elements) as name=value pairs separated by spaces.

Elements come from HTML tags; attributes refine them:

html
<a href="/about" class="nav-link" target="_blank">About us</a>

Here href sets the link URL, class hooks CSS, and target opens a new tab. The tag name (a) says “this is a link”; attributes say “where it goes and how it behaves.”

💡
Beginner Tip

Attributes describe properties of an element, not new elements. You cannot put a href on a div and expect link behavior—use the right tag (<a>) plus the right attributes.

Attribute Syntax

Quoted values

Most attribute values are wrapped in double or single quotes. HTML5 allows unquoted values in some cases, but quoting is always safer:

html
<input type="text" name="email" placeholder="you@example.com">
<p title='Use either "double" or \'single\' quotes'>Hover me</p>

Boolean attributes

Boolean attributes are “on” when present and “off” when absent. Do not write ="false":

html
<input type="checkbox" checked>
<button disabled>Submit</button>
<video controls muted autoplay></video>

All of these are valid: checked, checked="", and checked="checked".

Unquoted values (invalid in practice)

Unquoted attributes break when values contain spaces. Always quote:

ValidRisky / invalid
class="btn primary"class=btn primary (parser sees two attributes)
alt="A red rose"alt=A red rose (truncated value)

Multiple class names

The class attribute accepts several space-separated tokens. Order does not matter; all apply:

html
<button class="btn btn-primary is-loading" type="submit">Save</button>

Global vs Element-Specific Attributes

Global attributes work on nearly every HTML element—id, class, lang, dir, hidden, title, tabindex, data-*, and dozens of on* event handlers. See the dedicated global attributes guide for the full list and examples.

Element-specific attributes only apply to certain tags: href on <a>, src and alt on <img>, colspan on <td> and <th>, action and method on <form>.

Quick check

If an attribute appears on one tag and you wonder whether another can use it, look it up in the global attributes page or this hub’s reference tables. When in doubt, validate your HTML.

Attribute Categories

Attributes are easier to learn in groups. Below is how this reference is organized.

📝 HTML Attributes Reference

Quick lookup by category. Each attribute links to its dedicated tutorial.

Global & identity

Full coverage: HTML Global Attributes.

Form & input

Links & navigation

Media & images

Tables

Metadata

Event handlers (on*)

About 70 inline event-handler attributes exist as global attributes. Common ones:

Prefer element.addEventListener() in JavaScript instead of inline handlers for maintainability and Content Security Policy. Browse the A–Z index below for every on* attribute.

Full A–Z Attribute Index

All 210 attributes in this reference, sorted alphabetically. Click any name for the full guide.

⚡ Quick Reference

PatternExample
String attributeclass="btn primary"
Boolean attribute<input required>
Global identityid="main" class="layout"
Link<a href="/page" rel="noopener">
Image<img src="x.jpg" alt="Desc">
Custom datadata-id="42"
Global
id class lang

Any element

Forms
name type required

Input controls

Media
src alt srcset

Images & video

Links
href target rel

Navigation

Examples Gallery

Six examples showing common HTML attribute patterns. Each includes View Output and Try It Yourself links to the live editor.

Example 1 — class on p and span

The class attribute applies one or more CSS class names to any element.

html
<p class="lead">Intro paragraph with larger text.</p>
<p>Highlight a <span class="highlight">word</span> inline.</p>
Try It Yourself

How It Works

Separate multiple classes with spaces. CSS targets .lead and .highlight regardless of which tag carries them.

Example 2 — id + label for

Pair id on a control with for on <label> for accessible forms.

html
<label for="username">Username</label>
<input type="text" id="username" name="username">
Try It Yourself

How It Works

Clicking the label focuses the input because for="username" matches id="username". Screen readers announce the label with the field.

Example 3 — img src, alt, width, height

Images use src for the file URL, alt for accessibility, and dimensions to reserve layout space.

html
<img src="logo.png" alt="Site logo"
     width="120" height="60">
Try It Yourself

How It Works

alt is required for informative images. width and height prevent layout shift while the image loads.

Example 4 — form action, method, required

Forms use action for the handler URL, method for get/post, and required on mandatory fields.

html
<form action="/subscribe" method="post">
  <label for="email">Email</label>
  <input type="email" id="email" name="email" required>
  <button type="submit">Subscribe</button>
</form>
Try It Yourself

How It Works

The browser blocks submit until required fields have values. Server-side validation is still essential.

Example 6 — data-* on article

data-* attributes store custom metadata readable via element.dataset in JavaScript.

html
<article data-id="42" data-category="tutorial">
  <h2>HTML Attributes</h2>
  <p>Custom data attributes for scripts.</p>
</article>
Try It Yourself

How It Works

Names after data- become camelCase in dataset (data-user-iddataset.userId). Values must be strings.

💡 Best Practices

✅ Do

  • Quote all attribute values with double quotes
  • Use alt on every informative img
  • Pair label for with matching id
  • Use rel="noopener noreferrer" with target="_blank"
  • Prefer class over inline style for maintainability
  • Store script data in data-* instead of invented attributes

❌ Don’t

  • Put element-specific attrs on wrong tags (e.g. href on div)
  • Use boolean attrs with ="false" to disable them
  • Rely on inline on* handlers for production apps
  • Duplicate id values on one page
  • Omit name on form fields you need server-side
  • Use presentational attrs like bgcolor instead of CSS

Universal Browser Support

Core HTML attributes—id, class, href, src, alt, type, name, and boolean form attributes—have been supported since the earliest browsers. Modern attributes like data-*, srcset, and inputmode work in all current Chrome, Firefox, Safari, and Edge releases.

Baseline · Since HTML

HTML attributes

Standard attributes are among the most stable parts of the web platform. Focus on valid, semantic markup with quoted values and proper accessibility attributes.

100% Core attribute support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
HTML attributes Universal

Bottom line: Attributes rarely break across browsers. Validate your HTML and test form/media behavior in your target environments.

Conclusion

HTML attributes turn generic tags into configured elements—links with destinations, images with descriptions, forms with validation, and any element with identity hooks for CSS and JS. Learn the syntax (quoted values, boolean presence), distinguish global from element-specific properties, and use this hub’s index to dive into individual guides.

Start with global attributes, then explore accept, href, and alt, or return to HTML tags for element structure.

Key Takeaways

Knowledge Unlocked

Six things to remember about HTML attributes

Use these points when writing any HTML document.

6
Core concepts
🌐 02

Global attrs

id, class, lang, data-*.

Scope
📝 03

Form attrs

name, type, required.

Forms
🔗 04

Link & media

href, src, alt.

Media
☑️ 05

Booleans

Present = true.

Syntax
🎯 06

Practice

Try the live examples.

Hands-on

❓ Frequently Asked Questions

HTML attributes are name=value pairs written inside an element's opening tag. They configure behavior (href on a link), provide metadata (alt on an image), or hook into CSS and JavaScript (class and id). Attributes never appear in closing tags.
A tag names the element type—p for paragraph, a for anchor. Attributes modify that element: class for styling, href for the link URL, required for form validation. Tags define structure; attributes add properties.
Global attributes can be used on almost any HTML element. Examples include id, class, lang, dir, hidden, title, tabindex, data-*, and event handlers like onclick. Element-specific attributes such as href only work on certain tags like a.
Boolean attributes are true when present and false when absent. Writing checked, disabled, required, or hidden (with or without ="") enables the property. Omit the attribute entirely to disable it—do not use values like "false".
The required attribute is native HTML form validation—the browser blocks submit until the field has a value. aria-required is an accessibility hint for assistive tech; it does not enforce validation. Use required for real constraints; add aria-required when you need to expose that state to screen readers.
Yes. Most attributes are optional. Omit alt only on decorative images (use alt=""). Omit class when no styling hook is needed. Required attributes are rare—img needs src, a needs href for a useful link, and input benefits from type and name in forms.
Did you know?

The data-* attribute family was standardized in HTML5 so developers could store custom values without breaking validation. Before that, people often misused non-standard attributes like user_id—which still work in browsers but fail HTML validators.

Practice HTML attributes in the editor

Try class names, form validation, links, images, and data-* attributes—then preview live.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful