Skip to content

Commit

Permalink
Recommend that id and class attributes should be valid CSS identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
wbamberg committed Jul 26, 2024
1 parent a298ce6 commit 597355e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions files/en-us/web/html/global_attributes/class/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The `class` attribute is a list of class values separated by [ACSII whitespace](

Each class value may contain any Unicode characters (except, of course, ASCII whitespace). However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, class attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if a class attribute value is not a valid CSS identifier (for example, `my?class` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters).

For this reason, it's recommended that developers choose values for class attributes that are valid CSS identifiers.

## Specifications

{{Specifications}}
Expand Down
12 changes: 7 additions & 5 deletions files/en-us/web/html/global_attributes/id/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ The **`id`** [global attribute](/en-US/docs/Web/HTML/Global_attributes) defines

## Description

The purpose of the `id` attribute is to identify a single element when linking (using a [fragment identifier](/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#fragment)), scripting, or styling (with {{glossary("CSS")}}).
The purpose of the ID attribute is to identify a single element when linking (using a [fragment identifier](/en-US/docs/Web/HTTP/Basics_of_HTTP/Identifying_resources_on_the_Web#fragment)), scripting, or styling (with {{glossary("CSS")}}).

Elements with `id` attributes are available as global properties. The property name is the `id` attribute, and the property value is the element. For example, given markup like:
Elements with ID attributes are available as global properties. The property name is the ID attribute, and the property value is the element. For example, given markup like:

```html
<p id="preamble"></p>
Expand All @@ -29,11 +29,13 @@ const content = window.preamble.textContent;

### Syntax

An `id`'s value must not contain [ACSII whitespace](/en-US/docs/Glossary/Whitespace#in_html) characters. Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the [`class`](/en-US/docs/Web/HTML/Global_attributes#class) attribute, which allows space-separated values, elements can only have one single ID value.
An ID attribute's value must not contain [ACSII whitespace](/en-US/docs/Glossary/Whitespace#in_html) characters. Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the [`class`](/en-US/docs/Web/HTML/Global_attributes#class) attribute, which allows space-separated values, elements can only have one single ID value.

Technically, the value for an `id` attribute may contain any other Unicode character. However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, ID attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if an ID attribute value is not a valid CSS identifier (for example, `my?id` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters).
Technically, the value for an ID attribute may contain any other Unicode character. However, when used in CSS selectors, either from JavaScript using APIs like {{domxref("Document.querySelector()")}} or in CSS stylesheets, ID attribute values must be valid [CSS identifiers](/en-US/docs/Web/CSS/ident). This means that if an ID attribute value is not a valid CSS identifier (for example, `my?id` or `1234`) then it must be escaped before being used in a selector, either using the {{domxref("CSS.escape_static", "CSS.escape()")}} method or [manually](/en-US/docs/Web/CSS/ident#escaping_characters).

Also, not all valid `id` attribute values are valid JavaScript identifiers. For example, `1234` is a valid attribute value but not a valid JavaScript identifier. This means that the value is not a valid variable name, so you can't access the element using code like `window.1234`. However, you can access it using `window["1234"]`.
For this reason, it's recommended that developers choose values for ID attributes that are valid CSS identifiers.

Also, not all valid ID attribute values are valid JavaScript identifiers. For example, `1234` is a valid attribute value but not a valid JavaScript identifier. This means that the value is not a valid variable name, so you can't access the element using code like `window.1234`. However, you can access it using `window["1234"]`.

## Specifications

Expand Down

0 comments on commit 597355e

Please sign in to comment.