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.
Attribute syntax breakdown
Here's the anatomy of an attribute:
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:
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.
Link and media attributes
Certain elements need specific attributes to function. Links, images, and media elements depend on attributes to work:
Essential link and media attributes:
href(links) โ URL to navigate to. Can be absolute (https://...), relative (/about), or anchor (#section).src(images, video, audio) โ Path to the media file.alt(images) โ Text description of the image. Critical for accessibility and SEO. Always include.width,height(images, video) โ Dimensions in pixels. Helps prevent layout shift.placeholder(inputs) โ Hint text shown in empty input fields.controls(video, audio) โ Shows play/pause buttons without needing JavaScript.
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.
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:
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.
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.
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"].
data-product-id becomes dataset.productId.
Combining multiple attributes
Real-world elements often use multiple attributes together. Here's a practical example:
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.
Task: Create a profile card using multiple attributes:
- A
divwithid="profile-card"andclass="card" - An
<img>withsrc,alt, andwidthattributes - An
<h2>with atitleattribute that shows a tooltip - A link
<a>withhref="#"andtarget="_blank" - An
<input>withplaceholderandrequiredattributes - 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, andlangwork on any element. - Link and media attributes are essential:
href,src,alt,width,height. - Boolean attributes like
required,disabled,checkedare 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
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 โ