--- url: "https://web.dev/learn/html/attributes" titulek: "Attributes | web.dev" h1: "Attributes Stay organized with collections Save and categorize content based on your preferences." popis: "Learn about the different global attributes along with attributes specific to particular HTML elements." jazyk: "en" kanonicka: "https://web.dev/learn/html/attributes" drobecky: ["web.dev", "Resources", "HTML"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 03:30:45" znaku: 24335 slov: 3548 kodovani: "iso-8859-2" sha256: "7873df6d36f1edc55ed9917d895d9a1908c135afcc678cd2e829c11d66d07cdc" --- Attributes Stay organized with collections Save and categorize content based on your preferences. Attributes were briefly discussed in [Overview of HTML](/learn/html/overview#attributes). It's time for a deep dive. Attributes are what make HTML so powerful. Attributes are space-separated names and name/value pairs appearing in the opening tag, providing information about and functionality for the element. Attributes define the behavior, linkages, and functionality of elements. Some attributes are global, meaning they can appear within any element's opening tag. Other attributes apply to several elements but not all, while other attributes are element-specific, relevant only to a single element. In HTML, all attributes except boolean, and to some extent enumerated attributes, require a value. If an attribute value includes a space or special characters, the value must be quoted. For this reason and improved legibility, quotations are always recommended. While HTML isn't case-sensitive, some attribute values are. Values that are part of the HTML specification are case-insensitive. Strings values that are defined, such as class and ID names, are case-sensitive. If an attribute value is case-sensitive in HTML, it's case-sensitive when used as an [attribute selector](https://developer.mozilla.org/docs/Web/CSS/Attribute_selectors) in CSS and in JavaScript; otherwise, it's not. `
` Boolean attributes If a boolean attribute is present, it is always true. Boolean attributes include `autofocus`, `inert`, `checked`, `disabled`, `required`, `reversed`, `allowfullscreen`, `default,` `loop`, `autoplay`, `controls`, `muted`, `readonly`, `multiple,` and `selected`. If one (or more) of these attributes is present, the element is disabled, required, readonly, etc. If not present, it isn't. Boolean values can either be omitted, set to an empty string, or be the name of the attribute; but the value doesn't have to actually be set to the string `true`. All values, including `true`, `false`, and `😀`, while invalid, will resolve to true. These three tags are equivalent: ` ` If the attribute value is false, omit the attribute. If the attribute is true, include the attribute but don't provide a value. For example, `required="required"` is not a valid value in HTML; but as `required` is boolean, invalid values resolve to true. But as invalid enumerated attributes don't necessarily resolve to the same value as missing values, it is easier to get into the habit of omitting values than it is to remember which attributes are boolean versus enumerated and potentially provide an invalid value. When toggling between true and false, add and remove the attribute altogether with JavaScript rather than toggling the value. `const myMedia = document.getElementById("mediaFile"); myMedia.removeAttribute("muted"); myMedia.setAttribute("muted");` Enumerated attributes Enumerated attributes are sometimes confused with boolean attributes. They are HTML attributes that have a limited set of predefined valid values. Like boolean attributes, they have a default value if the attribute is present but the value is missing. For example, if you include `` Try changing the `color` of the `style` to something other than `inherit`. Then try changing the `style` to a `p` selector. Don't remove the display property or the style block will disappear. Custom attributes We've only touched the surface of HTML global attributes. There are even more attributes that apply to only one or a limited set of elements. Even with hundreds of defined attributes, you may have a need for an attribute that isn't in the specification. HTML has you covered. You can create any custom attribute you want by adding the [`data-`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/data-*) prefix. You can name your attribute anything that starts with `data-` followed by any lowercase series of characters that don't start with `xml` and don't contain a colon (`:`). While HTML is forgiving and won't break if you create unsupported attributes that don't start with `data`, or even if you start your custom attribute with `xml` or include a `:`, there are benefits to creating valid custom attributes that begin with `data-`. With custom data attributes you know that you aren't accidentally using an existing attribute name. Custom data attributes are future-proof. While browsers won't implement default behaviors for any specific `data-` prefixed attribute, there is a built-in dataset API to iterate through your custom attributes. Custom properties are an excellent way of communicating application-specific information in JavaScript. Add custom attributes to elements in the form of `data-name` and access these through the DOM using `dataset[name]` on the element in question. `
HAL and EVE could teach a fan to blow hot air.
` You can use `getAttribute()` using the full attribute name, or you can take advantage of the simpler [`dataset`](https://developer.mozilla.org/docs/Web/API/HTMLElement/dataset) property. `el.dataset["machineLearning"]; // workshop e.dataset.machineLearning; // workshop` The `dataset` property returns a `DOMStringMap` object of each element's `data-` attributes. There are several custom attributes on the `
`. The dataset property means you don't need to know what those custom attributes are in order to access their names and values: `for (let key in el.dataset) { customObject[key] = el.dataset[key]; }` The attributes in this article are global, meaning they can be applied to any HTML element (though they don't all have an impact on those elements). Up next, we take a look at the two attributes from the intro image that we didn't address—`target` and `href`—and several other element-specific attributes as we take a deeper look into links. Check your understanding Test your knowledge of attributes. An `id` should be unique in the document. False Try again. True Correct! Select the correctly formed custom attribute. `data-birthday` Correct `birthday` Try again. `data:birthday` Try again [Previous arrow_back Headings and sections](/learn/html/headings-and-sections) [Next Text basics arrow_forward](/learn/html/text-basics) Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2022-12-08 UTC.