HTML Global Attributes
In this page:
<tagname id="value" class="value" style="value" title="value" hidden>content</tagname>
The class and id Attributes
The class attribute groups multiple elements together for group styling, while the id attribute assigns a completely unique identifier to a single, specific element on the page.
- Each ID must be entirely unique across your entire HTML page structure.
- Accepted attributes:
- class — space-separated class names
- id — unique identifier, no spaces
Example: The class and id Attributes
<p class="highlight">Grouped</p>
<p id="intro">Unique</p>
The style Attribute
The style attribute applies inline CSS styles directly to a single element. It has a high specificity rating, meaning it will override rules in external stylesheets.
- Use inline styles for quick testing, but move to external stylesheets for larger projects to keep your code clean.
- Accepted values:
- style — semicolon-separated CSS declarations, e.g. color: red; margin: 0
Example: The style Attribute
<p style="color: blue;">Inline styled text</p>
The title Attribute
The title attribute defines some extra information about an element. In most desktop browsers, this information appears as a small tooltip when you hover your mouse over the element.
- Keep title tooltips brief and helpful.
- Accepted values:
- title — advisory text, usually shown as a tooltip
Example: The title Attribute
<p title="Extra information">Hover over me</p>
The lang and dir Attributes
The lang attribute declares the language of your content, and the dir attribute specifies the text direction (ltr for left-to-right, or rtl for right-to-left), which is essential for accessibility and search engine optimization.
- Always declare the primary language of your page inside your opening html tag.
- Accepted attributes:
- lang — BCP 47 language code, e.g. en | fr | ar | zh-CN
- dir — ltr | rtl | auto
Example: The lang and dir Attributes
<html lang="ar" dir="rtl">
</html>
The hidden and tabindex Attributes
The hidden attribute hides an element from the page layout entirely. The tabindex attribute includes or excludes elements from the page's keyboard tab navigation sequence.
- Set tabindex="0" to make custom, styled elements focusable using the keyboard.
- Accepted attributes:
- hidden — boolean | until-found
- tabindex — 0 (focusable, in order) | -1 (focusable by script only) | positive integer (avoid)
Example: The hidden and tabindex Attributes
<p hidden>You cannot see this</p>
<div tabindex="0">Focusable div</div>
- Using the same ID attribute on multiple elements on the same webpage.
- Relying on title tooltips to display critical instructions that mobile users will miss.
- Hiding elements using CSS visibility: hidden while expecting screen readers to read them.
- Global attributes (like class, id, and style) can be applied to any HTML element on your webpage.
- Use class for group styles, id for unique identifiers, and style to apply quick inline CSS overrides.
- Use title for tooltips, lang and dir to define language settings, and hidden or tabindex to control element visibility and keyboard focus.
Standard HTML5 global attributes are supported natively by all web browsers.
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: