HTML global attributes can be added to any HTML element, from a heading or button to an entire section. Knowing this shared toolkit lets you attach styling hooks, store custom JavaScript metadata, manage keyboard focus, and toggle visibility cleanly across your markup.
id and class: Identify unique elements or assign reusable CSS/JS group names.data-*: Store custom application data (such as data-user-id="42" or data-plan="pro") accessible in JavaScript via element.dataset.userId.hidden: Boolean attribute that hides an element visually and removes it from screen readers until JavaScript removes hidden.tabindex: Controls keyboard Tab focus order (tabindex="0" inserts a custom widget into natural order; tabindex="-1" allows programmatic .focus() only).lang and dir: Set text language (lang="es") and directionality (dir="rtl" for Arabic or Hebrew).contenteditable: Turns an element into an inline editable rich-text box.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HTML Global Attributes Demo</title>
</head>
<body style="font-family: system-ui, sans-serif; padding: 20px;">
<button id="buy-btn" class="btn" data-product-id="sku-904" data-price="29">
Buy Starter Kit ($29)
</button>
<p id="cart-msg" hidden>Added item to cart!</p>
<script>
const btn = document.getElementById('buy-btn');
const msg = document.getElementById('cart-msg');
btn.addEventListener('click', () => {
msg.textContent = `Added ${btn.dataset.productId} ($${btn.dataset.price}) to cart.`;
msg.hidden = false;
});
</script>
</body>
</html>
tabindex values like tabindex="5": Positive integers break expected top-to-bottom keyboard tab flow and confuse assistive users. Stick to 0 or -1.hidden with CSS display: flex;: Explicit CSS display rules override the browser's [hidden] { display: none; } style. Add [hidden] { display: none !important; } if needed.data-: Always prefix custom attributes with data- so your HTML stays valid.The dataset object converts hyphens to camelCase automatically, so data-order-status="paid" is read via element.dataset.orderStatus.
Yes. Adding translate="no" on brand names or code blocks instructs browser translation tools not to translate those words.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.