ID with the first character being a number does not work when I put in css #[example number]

Asked

Viewed 288 times

2

When for example I put in HTML:<div id="1">texto de teste</div> and I’ll put it in CSS: #1 {código de teste}, does not recognize the id=1. Even if I put "class" in place of "id", or if I put another tag, or put another number. Is this a problem of my code? Or is it normal? Why does this happen?

  • @Augusto Vasquesso why can’t I use a number at the beginning of a id? Since there is no restriction. I have already checked numerous times if it was not a problem of lack of ; or { , or anything else. It just doesn’t work. If you know what’s going on help me, please :D. Thanks!

  • It’s just compatibility anyway?

  • @Giovaneps look at the final part of my reply which deals specifically with CSS.

1 answer

3


According to the recommendation W3C of 28 October 2014, HTML5 A vocabulary and associated Apis for HTML and XHTML, is regulating:

3.2.5.1 The attribute id

The attribute id specifies the unique identifier (ID) of its element.

The value must be exclusive among all IDs in the initial tree element and must contain at least one character. The value must not contain space characters.

There are no other restrictions on the form that a ID can assume, in particular, the IDs may consist only of digits, starting with a digit, start with an underscore, consist only of punctuation etc.

The unique identifier of an element can be used for a variety of purposes, mainly as a way to link specific parts of a document using identifiers of fragments, as a way to direct an element when creating scripts and as a way to style a specific element. CSS.

Identifiers are opaque strings. Particular meanings do not shall be derived from the attribute value id.

Which clearly confirms that you did not commit any violations in your HTML code, but taking a look at the documentation MDN on Ids there is a recommended note:

Note: Using characters other than letters and ASCII digits, '_', '-' and '.' can cause compatibility problems since they were not allowed in HTML 4. Although this restriction is suspended in HTML 5, an ID must start with a letter for compatibility purposes.

That recommendation that an ID must start with a letter for compatibility purposes is quite significant because according to the recommendations:

In the CSS, identifiers (including element names, classes and Ids on selectors ) may contain only [a-za-Z0-9] characters and ISO 10646 U + 00A0 and above, in addition to the hyphen (-) and the underlining ( _); they cannot start with one digit, two hyphens or a hyphen followed by a digit. Identifiers may also contain escape characters and any ISO 10646 character as a code numeric (see next item). For example, the identifier "Q&A?" can be written as "B & W ?" or "B 26 W 3F".

Having as a consequence that its id started by a number is compatible with the HTML5 but is not compatible with CSS and so is not recognized.

On the question of being bad practice using explicitly numerical identifiers yes I agree with the recommendation made in the comments of the question Is it bad practice to put numbers as id in HTML elements? If yes why?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.