The accesskey
attribute specifies a shortcut key to activate/focus an element.
The accesskey
attribute value must be a single character (a letter or a digit).
Using accesskeys is difficult because they may conflict with other key standards in the browser.
To avoid this problem, most browsers will use accesskeys only if pressed together with the Alt key.
Adapting accesskeys to all international languages are difficult.
The accesskey value may not be present on all keyboards.
Because of these concerns, it is advised not to use accesskeys.
The accesskey attribute specifies a shortcut key to activate/focus an element.
Note: The shortcut is varying in different browsers:
Example:
HTML
<a href="https://horje.com" accesskey="h">HTML tutorial</a><br>
<a href="https://horje.com/learn/1012/what-is-html-accesskey-attribute" accesskey="c">CSS tutorial</a>
The class
attribute specifies one or more class names for an element.
The class
attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.
<element class="classname">
Value | Description |
---|---|
classname | Specifies one or more class names for an element. To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.
Naming rules:
|
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1.intro {
color: blue;
}
p.important {
color: green;
}
</style>
</head>
<body>
<h1 class="intro">Header 1</h1>
<p>A paragraph.</p>
<p class="important">Note that this is an important paragraph. :)</p>
</body>
</html>
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1.intro {
color: blue;
text-align: center;
}
.important {
background-color: yellow;
}
</style>
</head>
<body>
<h1 class="intro important">Header 1</h1>
<p>A paragraph.</p>
</body>
</html>
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<div class="example">First div element with class="example".</div>
<div class="example">Second div element with class="example".</div>
<p>Click the button to add a yellow background color to the first div element with class="example" (index 0).</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
let x = document.getElementsByClassName("example");
x[0].style.backgroundColor = "yellow";
}
</script>
</body>
</html>
Click the button to add the "mystyle" class to DIV. I am a DIV element
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<style>
.mystyle {
margin: 20px;
width: 300px;
height: 50px;
background-color: coral;
color: white;
font-size: 25px;
}
</style>
</head>
<body>
<p>Click the button to add the "mystyle" class to DIV.</p>
<button onclick="myFunction()">Try it</button>
<div id="myDIV">
I am a DIV element
</div>
<br>
<script>
function myFunction() {
document.getElementById("myDIV").classList.add("mystyle");
}
</script>
</body>
</html>
The contenteditable
attribute specifies whether the content of an element is editable or not.
Note: When the contenteditable
attribute is not set on an element, the element will inherit it from its parent.
The numbers in the table specify the first browser version that fully supports the attribute.
<element contenteditable="true|false">
Value | Description |
---|---|
true | Specifies that the element is editable |
false | Specifies that the element is not editable |
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p contenteditable="true">This is a paragraph. It is editable. Try to change this text.</p>
</body>
</html>
The data-*
attributes is used to store custom data private to the page or application.
The data-*
attributes gives us the ability to embed custom data attributes on all HTML elements.
The stored (custom) data can then be used in the page's JavaScript to create a more engaging user experience (without any Ajax calls or server-side database queries).
The data-*
attributes consist of two parts:
Note: Custom attributes prefixed with "data-" will be completely ignored by the user agent.
The numbers in the table specify the first browser version that fully supports the attribute.
<element data-*="somevalue">
Value | Description |
---|---|
somevalue | Specifies the value of the attribute (as a string) |
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<script>
function showDetails(animal) {
let animalType = animal.getAttribute("data-animal-type");
alert("The " + animal.innerHTML + " is a " + animalType + ".");
}
</script>
</head>
<body>
<h1>Species</h1>
<p>Click on a species to see what type it is:</p>
<ul>
<li onclick="showDetails(this)" id="owl" data-animal-type="bird">Owl</li>
<li onclick="showDetails(this)" id="salmon" data-animal-type="fish">Salmon</li>
<li onclick="showDetails(this)" id="tarantula" data-animal-type="spider">Tarantula</li>
</ul>
</body>
</html>
The dir
attribute specifies the text direction of the element's content.
<element dir="ltr|rtl|auto">
Value | Description |
---|---|
ltr | Default. Left-to-right text direction |
rtl | Right-to-left text direction |
auto | Let the browser figure out the text direction, based on the content (only recommended if the text direction is unknown) |
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p dir="rtl">Write this text right-to-left!</p>
</body>
</html>
The draggable
attribute specifies whether an element is draggable or not.
Tip: Links and images are draggable by default.
Tip: The draggable
attribute is often used in drag and drop operations. Read our HTML Drag and Drop tutorial to learn more.
The numbers in the table specify the first browser version that fully supports the attribute.
<element draggable="true|false|auto">
Value | Description |
---|---|
true | Specifies that the element is draggable |
false | Specifies that the element is not draggable |
auto | Uses the default behavior of the browser |
Example:
HTML
<p draggable="true">This is a draggable paragraph.</p>
Example:
HTML
<p draggable="true">This is a draggable paragraph.</p>
Example:
HTML
<p draggable="false">This is a draggable paragraph.</p>
Example:
HTML
<p draggable="auto">This is a draggable paragraph.</p>
The enterkeyhint
attribute allows you to change the appearance of the "Enter" key on a virtual keyboard.
Here is the result of the example above, on my phone:
The example below uses "go" as the enter-key:
Example:
HTML
<input type="text" enterkeyhint="search">
Example:
HTML
<input type="text" enterkeyhint="go">
Example:
HTML
<input type="text" enterkeyhint="done">
Example:
HTML
<input type="text" enterkeyhint="enter">
Example:
HTML
<input type="text" enterkeyhint="next">
Example:
HTML
<input type="text" enterkeyhint="previous">
Example:
HTML
<input type="text" enterkeyhint="send">
The hidden
attribute is a boolean attribute.
When present, it specifies that an element is not yet, or is no longer, relevant.
Browsers should not display elements that have the hidden
attribute specified.
The hidden
attribute can also be used to keep a user from seeing an element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the hidden attribute, and make the element visible.
The numbers in the table specify the first browser version that fully supports the attribute.
<element hidden>
Example:
HTML
<p hidden>This paragraph should be hidden.</p>
The id
attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).
The id
attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.
<element id="id">
Value | Description |
---|---|
id | Specifies a unique id for the element. Naming rules:
|
Example:
HTML
<!DOCTYPE HTML>
<html>
<body>
<h1 id="myHeader">Hello World!</h1>
<button onclick="displayResult()">Change text</button>
<script>
function displayResult() {
document.getElementById("myHeader").innerHTML = "Have a nice day!";
}
</script>
</body>
</html>
Example:
HTML
<html>
<body>
<h2><a id="top">Some heading</a></h2>
<p>Lots of text....</p>
<p>Lots of text....</p>
<p>Lots of text....</p>
<a href="#top">Go to top</a>
</body>
</html>
Example:
HTML
<!DOCTYPE HTML>
<html>
<head>
<style>
#myHeader {
color: red;
text-align: center;
}
</style>
</head>
<body>
<h1 id="myHeader">Horje is the best!</h1>
</body>
</html>
The inert
attribute disables an element and all the elements inside.
The elements are still visible, but they have no function: buttons and links cannot be clicked, input fields are disabled etc., and they are ignored by screen readers.
<element inert>
Example:
HTML
<div inert>
<button onclick="alert(42)">Click me</button>
<input type="text">
<a href="https://horje.com">Horje.com</a>
</div>
The inputmode
attribute allows you to change the appearance of the keyboard on a phone or tablet (any device with a virtual keyboard).
Here is the result of the example above, on my phone:
The example below uses inputmode "email":
<element inputmode="decimal|email|none|numeric|search|tel|text|url">
Value | Description |
---|---|
decimal | Numeric only keyboard, usually also a comma key |
Text keyboard, with keys typically for e-mail addresses like [@] | |
none | No keyboard should appear |
numeric | Numeric only keyboard |
search | Text keyboard, usually the [enter] key says [go] |
tel | Numeric only keyboard, usually also with [+], [*], and [#] keys |
text | Default. Text keyboard |
url | Text keyboard, with keys typically for web addresses like [.] and [/], and a special [.com] key, or other domain endings that are typically for the local settings. |
Run the example with a phone or tablet to see the result.
Example:
HTML
<input type="text" inputmode="numeric">
The keys on the virtual keyboard is decimal only.
Example:
HTML
<input type="text" inputmode="decimal">
Some of the keys are switched out with keys used typically for e-mail addresses.
Example:
HTML
<input type="text" inputmode="email">
none | No keyboard should appear |
Example:
HTML
<input type="text" inputmode="none">
search | Text keyboard, usually the [enter] key says [go] |
Example:
HTML
<input type="text" inputmode="search">
The keys on the virtual tel is search only.
Example:
HTML
<input type="text" inputmode="tel">
The keys on the virtual keyboard is text only.
Example:
HTML
<input type="text" inputmode="text">
The keys on the virtual keyboard is url only.
Example:
HTML
<input type="text" inputmode="url">
The lang
attribute specifies the language of the element's content.
Common examples are "en" for English, "es" for Spanish, "fr" for French, and so on.
<element lang="language_code">
Value | Description |
---|---|
language_code | Specifies the language code for the element's content. Language code reference |
Example:
HTML
<p lang="fr">Ceci est un paragraphe.</p>
The popover
attribute defines an element as a popover element, meaning that when it is invoked, it will be placed on top of the content, not interfere with the position of other HTML elements.
A popover element will be invisible until it is invoked by another element. The other element must have a popovertarget
attribute where the value refers to the popover element's id.
The popover element will be placed on top of all other content, and by clicking the popovertarget element, the popover element will toggle between showing and hiding:
The popover element can be a single HTML element as in the example above, or an entire section of HTML elements like in the example below.
<element popover>
The popover
attribute is a boolean attribute. If it is present, it specifies that this element is a popover element.
Example:
HTML
<h1 popover id="myheader">Hello</h1>
<button popovertarget="myheader">Click me!</button>
Example:
HTML
<div popover id="mydiv">
<h2>Popover</h2>
<hr>
<p>A popover is an element that is placed on top of everything else.</p>
<p>It can be used when you want to tell something important.</p>
</div>
The spellcheck
attribute specifies whether the element is to have its spelling and grammar checked or not.
The following can be spellchecked:
The numbers in the table specify the first browser version that fully supports the attribute.
<element spellcheck="true|false">
Value | Description |
---|---|
true | The element is to have its spelling and grammar checked |
false | The element is not to be checked |
Example:
HTML
<p contenteditable="true" spellcheck="true">This is a paragraph.</p>
The style
attribute specifies an inline style for an element.
The style
attribute will override any style set globally, e.g. styles specified in the <style>
tag or in an external style sheet.
The style
attribute can be used on any HTML element (it will validate on any HTML element. However, it is not necessarily useful).
<element style="style_definitions">
Value | Description |
---|---|
style_definitions | One or more CSS properties and values separated by semicolons (e.g. style="color:blue;text-align:center") |
Example:
HTML
<h1 style="color:blue;text-align:center;">This is a header</h1>
<p style="color:green;">This is a paragraph.</p>
The tabindex
attribute specifies the tab order of an element (when the "tab" button is used for navigating).
The tabindex
attribute can be used on any HTML element (it will validate on any HTML element. However, it is not necessarily useful).
<element tabindex="number">
Value | Description |
---|---|
number | Specifies the tabbing order of the element (1 is first) |
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<div tabindex="1">Horje</div><br>
<div tabindex="3">Google</div><br>
<div tabindex="2">Microsoft</div>
<script>
// At start, set focus on the first div
document.getElementsByTagName('div')[0].focus();
</script>
<p tabindex="4"><b>Note:</b> Try navigating the elements by using the "Tab" button on your keyboard.</p>
</body>
</html>
The title
attribute specifies extra information about an element.
The information is most often shown as a tooltip text when the mouse moves over the element.
The title
attribute can be used on any HTML element (it will validate on any HTML element. However, it is not necessarily useful).
<element title="text">
Value | Description |
---|---|
text | A tooltip text for an element |
Example:
HTML
<!DOCTYPE html>
<html>
<body>
<p><abbr title="World Health Organization">WHO</abbr> was founded in 1948.</p>
<p title="Free Web tutorials">Horje.com</p>
</body>
</html>
The translate
attribute specifies whether the content of an element should be translated or not.
Test: Click on the Google translate image (next to the search button at the top of the page) to change to another language, and look what happens to the word "ice cream" below:
Here we use translate="no": ice cream.
<element translate="yes|no">
Value | Description |
---|---|
yes | Specifies that the content of the element should be translated |
no | Specifies that the content of the element must not be translated |
Example:
HTML
<p translate="no">Don't translate this!</p>
<p>This can be translated to any language.</p>
The global attributes are attributes that can be used with all HTML elements.
Attribute | Description |
---|---|
accesskey | Specifies a shortcut key to activate/focus an element |
class | Specifies one or more classnames for an element (refers to a class in a style sheet) |
contenteditable | Specifies whether the content of an element is editable or not |
data-* | Used to store custom data private to the page or application |
dir | Specifies the text direction for the content in an element |
draggable | Specifies whether an element is draggable or not |
enterkeyhint | Specifies the text of the enter-key on a virtual keybord |
hidden | Specifies that an element is not yet, or is no longer, relevant |
id | Specifies a unique id for an element |
inert | Specifies that the browser should ignore this section |
inputmode | Specifies the mode of a virtual keyboard |
lang | Specifies the language of the element's content |
popover | Specifies a popover element |
spellcheck | Specifies whether the element is to have its spelling and grammar checked or not |
style | Specifies an inline CSS style for an element |
tabindex | Specifies the tabbing order of an element |
title | Specifies extra information about an element |
translate | Specifies whether the content of an element should be translated or not |