What are attributes?

Attributes are extra pieces of information you add to HTML tags. They go inside the opening tag and provide metadata, behavior, styling, or custom data for elements.

Every attribute follows the same pattern: name="value". Some attributes are required (like alt on images for accessibility), while others are optional and add extra functionality.

Key concept:
Attributes live inside the opening tag, never in the closing tag. They modify how the browser interprets or displays the element.

Attribute syntax breakdown

Here's the anatomy of an attribute:

attributes.html The name="value" pattern

Key rules:

  • Attributes go inside the opening tag only, separated by spaces.
  • Most attributes require a value: name="value".
  • Attribute names are case-insensitive, but lowercase is standard.
  • You can have multiple attributes on one element.
  • Boolean attributes (like checked, disabled) can be written with or without a value.

Global attributes (apply to all tags)

Some attributes work on any HTML element. These are called global attributes:

global-attrs.html The five most important global attributes

When to use each:

  • id โ€” Uniquely identify an element. Use sparingly. Perfect for navigation anchors and JavaScript hooks.
  • class โ€” Group similar elements for styling. Reusable. Can add multiple classes: class="button primary large".
  • style โ€” Quick inline styling. Avoid in production code; use external CSS files instead.
  • title โ€” Tooltip hint. Appears on hover on desktop. Use for extra context.
  • lang โ€” Document language. Helps screen readers and search engines. Set on the root <html> tag.

Try it yourself: Global and media attributes

Edit the code below to see how attributes change elements. Try modifying the link href, changing the image, updating classes, or adding a title tooltip.

โœ๏ธ Try It Yourself โ€” Global & Media Attrs
Preview

Boolean attributes

Some attributes are "on or off" โ€” they don't need a value. These are called boolean attributes. They control whether a feature is enabled:

boolean.html Attributes that are true/false

Common boolean attributes:

  • required โ€” Form field must be filled before submitting.
  • disabled โ€” Element is disabled and cannot be interacted with.
  • checked โ€” Checkbox or radio button is selected by default.
  • readonly โ€” Text field displays value but cannot be edited.
  • autoplay โ€” Video/audio starts playing automatically.
  • controls โ€” Show play/pause controls for media.
  • loop โ€” Media repeats when finished.
  • multiple โ€” File input or select allows multiple selections.

Try it yourself: Form and boolean attributes

Interact with form elements using boolean attributes. Try removing required, toggling disabled, or checking the checkbox.

โœ๏ธ Try It Yourself โ€” Form Attrs
Preview

Custom data attributes (data-*)

You can create your own custom attributes using the data-* prefix. These don't affect the browser's default behavior โ€” they're purely for storing custom data that JavaScript can access or that provides semantic meaning.

data-attrs.html Custom data attributes with data-* prefix

Why use data attributes?

  • Store metadata โ€” Keep extra information on elements without affecting display.
  • JavaScript hooks โ€” Access with element.dataset.attributeName.
  • Filter/sort โ€” Use data attributes to filter or categorize elements.
  • Accessible โ€” Valid HTML. More semantic than using random class names.
  • CSS selectors โ€” Target elements with [data-category="tutorial"].
Naming convention:
Data attribute names are lowercase with hyphens. In JavaScript, hyphens become camelCase: data-product-id becomes dataset.productId.

Combining multiple attributes

Real-world elements often use multiple attributes together. Here's a practical example:

combined.html Real elements with multiple attributes

When using multiple attributes:

  • Put each attribute on its own line for readability (optional, but recommended in complex markup).
  • Put required attributes first (like type, name, src).
  • Follow with global attributes (id, class, style).
  • End with data or semantic attributes.
Challenge: Build a Profile Card

Task: Create a profile card using multiple attributes:

  • A div with id="profile-card" and class="card"
  • An <img> with src, alt, and width attributes
  • An <h2> with a title attribute that shows a tooltip
  • A link <a> with href="#" and target="_blank"
  • An <input> with placeholder and required attributes
  • Add CSS styling to make it look nice

Use one of the Try It Yourself editors above to build your card!

Hint: Start with the second editor (Form Attributes) and modify it. Replace the form with your profile card markup. Use https://via.placeholder.com/150x150 for the image source.

Key takeaways

  • Attributes follow the name="value" pattern and go inside opening tags.
  • Global attributes like id, class, style, title, and lang work on any element.
  • Link and media attributes are essential: href, src, alt, width, height.
  • Boolean attributes like required, disabled, checked are on/off switches.
  • Data attributes (data-*) store custom information for JavaScript and semantic meaning.
  • Combine multiple attributes on one element for complete functionality.

Test your knowledge

Question 1 of 5
Where do attributes go in an HTML tag?
Question 2 of 5
Which attribute uniquely identifies an element on a page?
Question 3 of 5
Which is a boolean attribute?
Question 4 of 5
What does the alt attribute do on an image?
Question 5 of 5
How do you create a custom data attribute?
0
/5
Great job! You understand HTML attributes.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

You've mastered attributes.
Your HTML superpowers are growing.

Join the Best AI Programs โ€” three 5-month courses, fully online, with placement assistance. Week 1 is free.

Explore our AI-era courses โ†’