HTML Global Attributes
What are Global Attributes?
Most attributes only work on some elements, like href works on <a>, and src works on <img>.
Global attributes are different, they can be used on any HTML element.
You already know some of the global attributes:
classandid, from the Classes and Id chaptersstyle,title, andlang, from the Attributes chapter
This chapter shows some other useful global attributes.
The hidden Attribute
The hidden attribute hides an element.
The element is still in the HTML source code, but the browser does not show it:
Example
<p>This paragraph is visible.</p>
<p hidden>This paragraph is hidden.</p>
<p>This paragraph is also visible.</p>
Try it Yourself »
This can be useful for content that you show later with JavaScript, like an answer to a question.
Note: Never use hidden for secret information.
The data-* Attributes
The data-* attributes let you store your own information on an element.
The name must start with data-, and the rest of the name is up to you, like data-color or data-price.
The browser does not show the value, but JavaScript can read it with the dataset property:
Example
<ul>
<li data-color="red" onclick="showColor(this)">Apple</li>
<li data-color="yellow" onclick="showColor(this)">Banana</li>
<li data-color="orange" onclick="showColor(this)">Orange</li>
</ul>
<p id="result"></p>
<script>
function showColor(fruit) {
document.getElementById("result").innerHTML = fruit.innerHTML + " is " + fruit.dataset.color + ".";
}
</script>
Try it Yourself »
If you click Banana, the page shows:
Banana is yellow.
Tip: data-color becomes dataset.color in JavaScript.
You will learn more about JavaScript in a later chapter.
The contenteditable Attribute
The contenteditable attribute lets the user change the text of an element, right in the page:
Note: The changes only live in the browser, and are gone when the user reloads the page (unless you save them with JavaScript).
The spellcheck Attribute
The spellcheck attribute turns the spelling check on or off for text the user types:
Example
<textarea spellcheck="true">Spellcheck is on</textarea>
<textarea spellcheck="false">Spellcheck is off</textarea>
Try it Yourself »
Tip: You can turn spellcheck off for fields where the words are not normal text, like usernames or code.
The translate Attribute
Translation tools, like the ones built into browsers, can translate a whole web page into another language.
The translate attribute
is used to tell browsers which text to NOT translate:
Example
<p>Use the <code translate="no">print()</code> function to show text.</p>
Use it for names, brands, and code that must not change.
The dir Attribute
Some languages, like Arabic and Hebrew, are written from right to left.
The dir attribute sets the direction of the text.
The values are ltr (left to right), rtl (right to left), and auto
(the browser decides):
Example
<p>This text goes from left to right.</p>
<p dir="rtl">This text goes from right to left!</p>
Try it Yourself »
In the example above, the second paragraph moves to the right side of the page.
The exclamation mark also moves to the left end of the sentence, because that is where a right to left sentence ends.
More Global Attributes
Some global attributes are explained in other chapters:
| Attribute | What it does | Learn more |
|---|---|---|
| tabindex | Sets the order in which elements get keyboard focus | Keyboard Focus |
| draggable | Lets the user drag an element with the mouse | Drag and Drop API |
| popover | Turns an element into a popover | Popover API |
| inert | Makes an element and its content impossible to click or focus | inert reference |
Summary
- Global attributes work on any HTML element
hiddenhides an elementdata-*stores your own information, and JavaScript reads it withdatasetcontenteditablelets the user change the textspellcheckturns the spelling check on or offtranslate="no"stops translation tools from changing the textdirsets the text direction
For a complete list of all global attributes, visit our HTML Global Attributes Reference.