Skip to content

Commit

Permalink
Correct syntax rules for <ident> (mdn#34340)
Browse files Browse the repository at this point in the history
* Correct syntax rules for

* Update files/en-us/web/css/ident/index.md

* Update files/en-us/web/css/ident/index.md

Co-authored-by: Dipika Bhattacharya <[email protected]>

* Apply suggestions from code review

Co-authored-by: Dipika Bhattacharya <[email protected]>

* Review comments

* Update files/en-us/web/css/ident/index.md

Co-authored-by: Dipika Bhattacharya <[email protected]>

---------

Co-authored-by: Dipika Bhattacharya <[email protected]>
  • Loading branch information
wbamberg and dipikabh authored Jul 3, 2024
1 parent e3468fc commit 4f7063e
Showing 1 changed file with 36 additions and 21 deletions.
57 changes: 36 additions & 21 deletions files/en-us/web/css/ident/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: <ident>
slug: Web/CSS/ident
page-type: css-type
spec-urls: https://drafts.csswg.org/css-values/#css-identifier
spec-urls: https://drafts.csswg.org/css-values/#typedef-ident
---

{{CSSRef}}
Expand All @@ -11,39 +11,54 @@ The **`<ident>`** [CSS](/en-US/docs/Web/CSS) [data type](/en-US/docs/Web/CSS/CSS

## Syntax

The syntax of `<custom-ident>` is similar to CSS identifiers (such as property names), except that it is [case-sensitive](https://en.wikipedia.org/wiki/Case_sensitivity). It consists of one or more characters, where characters can be any of the following:
A CSS identifier consists of one or more characters, which can be any of the following:

- any alphabetical character (`A` to `Z`, or `a` to `z`),
- any decimal digit (`0` to `9`),
- a hyphen (`-`),
- an underscore (`_`),
- an escaped character (preceded by a backslash, `\`),
- a [Unicode](https://en.wikipedia.org/wiki/Unicode) character (in the format of a backslash, `\`, followed by one to six hexadecimal digits, representing its Unicode code point)
- any {{glossary("ASCII")}} character in the ranges `A-Z` and `a-z`
- any decimal digit (`0` to `9`)
- a hyphen (`-`)
- an underscore (`_`)
- any other {{glossary("Unicode")}} character `U+00A0` and higher (that is, any other non-ASCII Unicode character)
- an [escaped character](escaping_characters)

Note that `id1`, `Id1`, `iD1` and `ID1` are all different identifiers as they are [case-sensitive](https://en.wikipedia.org/wiki/Case_sensitivity). On the other hand, as there are several ways to escape a character, `toto\?` and `toto\3F` are the same identifiers.
Additionally, an identifier must not start with an unescaped digit, and must not start with an unescaped hyphen followed by an unescaped digit.

Note that `id1`, `Id1`, `iD1` and `ID1` are all different identifiers because they are [case-sensitive](https://en.wikipedia.org/wiki/Case_sensitivity). On the other hand, since there are several ways to escape a character, `toto\?` and `toto\3F` are the same identifiers.

### Escaping characters

Escaping a character means representing it in a way that changes how it is interpreted by a software system. In CSS, you can escape a character by adding a backslash (`\`) in front of the character. Any character, except the hexadecimal digits `0-9`, `a-f`, and `A-F`, can be escaped in this way. For example, `&` can be escaped as `\&`.

You can also escape any character with a backslash followed by the character's Unicode {{glossary("code point")}} represented by one to six hexadecimal digits. For example, `&` can be escaped as `\26`. In this usage, if the escaped character is followed by a hexadecimal digit, do one of the following:

- Place a space or other whitespace character after the Unicode code point.
- Provide the full six-digit Unicode code point of the character being escaped.

For example, the string `&123` can be escaped as `\26 123` (with a whitespace) or `\000026123` (with the six-digit Unicode code point for `&`) to ensure that `123` is not considered as part of the escape pattern.

## Examples

### Valid identifiers

```plain example-good
nono79 A mix of alphanumeric characters and numbers
ground-level A mix of alphanumeric characters and a dash
-test A dash followed by alphanumeric characters
--toto A custom-property like identifier
_internal An underscore followed by alphanumeric characters
\22 toto A Unicode character followed by a sequence of alphanumeric characters
bili\.bob A correctly escaped period
nono79 /* A mix of alphanumeric characters and numbers */
ground-level /* A mix of alphanumeric characters and a dash */
-test /* A hyphen followed by alphanumeric characters */
--toto /* A custom-property like identifier */
_internal /* An underscore followed by alphanumeric characters */
\22 toto /* An escaped character followed by alphanumeric characters */
\000022toto /* Same as the previous example */
bili\.bob /* A correctly escaped period */
🔥123 /* A non-ASCII character followed by numbers */
```

### Invalid identifiers

```plain example-bad
34rem It must not start with a decimal digit.
-12rad It must not start with a dash followed by a decimal digit.
bili.bob Only alphanumeric characters, _, and - needn't be escaped.
'bilibob' This would be a {{CSSxRef("&lt;string&gt;")}}.
"bilibob" This would be a {{CSSxRef("&lt;string&gt;")}}.
34rem /* Must not start with a decimal digit */
-12rad /* Must not start with a dash followed by a decimal digit */
bili.bob /* ASCII characters apart from alphanumerics must be escaped */
'bilibob' /* Treated as a string */
"bilibob" /* Treated as a string */
```

## Specifications
Expand Down

0 comments on commit 4f7063e

Please sign in to comment.