HTML Attributes (Global & Standard)
HTML Attributes (Global & Standard)
HTML attributes provide additional information about an element and control its behavior, styling, or accessibility. Attributes are always specified inside the opening tag and typically follow a name="value" pattern.
Basic Attribute Syntax
<tagname attribute="value">Content</tagname>Example:
<a href="https://www.example.com" target="_blank">Visit Site</a>Here, href and target are both attributes of the <a> element.
Global Attributes
Global attributes can be applied to almost any HTML element, regardless of its type. These are essential for styling, scripting, and accessibility:
class— assigns one or more CSS class namesid— assigns a unique identifierstyle— applies inline CSStitle— adds a tooltip on hoverlang— specifies the language of the contenthidden— hides the element from viewtabindex— controls keyboard focus ordercontenteditable— allows users to edit content directly in the browserdraggable— enables drag-and-drop functionality
Standard (Element-Specific) Attributes
Unlike global attributes, standard attributes only work on specific elements:
href— used only on<a>and<link>src— used on<img>,<script>,<video>,<iframe>alt— used on<img>for accessibility and SEOtype— used on<input>,<button>,<script>placeholder— used on form fields like<input>and<textarea>
data-* Attributes (Trending in Modern JavaScript Development)
Custom data-* attributes let you store extra information directly on HTML elements, which is heavily used in modern JavaScript frameworks and vanilla JS DOM manipulation:
<button data-user-id="482" data-role="admin">Edit User</button>// Accessing data attributes in JavaScript
const btn = document.querySelector('button');
console.log(btn.dataset.userId); // "482"
console.log(btn.dataset.role); // "admin"ARIA Attributes for Accessibility
As web accessibility (a11y) becomes a legal and ethical priority in 2026, ARIA attributes are increasingly essential:
<button aria-label="Close menu" aria-expanded="false">X</button>Boolean Attributes
Some attributes don't need a value at all — their mere presence enables the behavior:
<input type="text" disabled>
<input type="checkbox" checked>
<video controls autoplay muted></video>Best Practices for Writing Attributes
- Always use double quotes around attribute values for consistency.
- Never skip the
altattribute on images — it's critical for SEO and screen readers. - Use
data-*attributes instead of storing custom data in class names. - Combine
classandidthoughtfully:classfor reusable styles,idfor unique JS hooks.
Key Takeaways
- Attributes add extra information or behavior to HTML elements.
- Global attributes work on almost any element; standard attributes are element-specific.
data-*attributes are widely used for JavaScript interactivity.- ARIA attributes are essential for building accessible, modern websites.
Press Run to execute.
Press Run to execute.
Add an alt attribute to the image, a data-id attribute to the button, and an aria-label attribute to the close icon.
Press Run to execute.
This is a self-check — compare your result with the expected output above.
Was this page helpful?