Can You Start a Class Name with a Numeric Digit

Can you start a class name with a numeric digit?

Rules for identifier names in C++ are:

  1. It can not start with a number
  2. Can be composed of letters, numbers, underscore, universal character names1 and implementation defined characters
  3. Can not be a keyword.

The sections in the C++ draft standard that cover this are 2.11 Identifiers which includes the following grammar:

identifier:
identifier-nondigit <- Can only start with a non-digit
identifier identifier-nondigit <- Next two rules allows for subsequent
identifier digit <- characters to be those outlined in 2 above
identifier-nondigit:
nondigit <- a-z, A-Z and _
universal-character-name
other implementation-defined characters
[...]

and 2.12 Keywords explains all the identifier reserved for use as keywords.

Finally, the following names are also reserved:

  1. Names that contain a double underscore __, or start with either an underscore followed by an uppercase letter (like _Apple) in any scope,
  2. Names that start with an underscore in the global namespace (like _apple in the global namespace) are reserved.

The section that covers this in the draft standard is 17.6.4.3.2. We can find a rationale for why these are reserved from Rationale for International Standard—Programming Languages—C which says:

[...]This gives a name space for writing the numerous behind-the-scenes non-external macros and functions a library needs to do its job properly[...]

In C++ this also applies to name mangling as this example shows.


Footnotes

  • 1. Allowed universal characters

The universal characters that are allowed is covered in Annex E.1:

E.1 Ranges of characters allowed [charname.allowed]

00A8, 00AA, 00AD,

00AF, 00B2-00B5, 00B7-00BA, 00BC-00BE, 00C0-00D6, 00D8-00F6, 00F8-00FF

0100-167F, 1681-180D, 180F-1FFF 200B-200D, 202A-202E, 203F-2040, 2054,

2060-206F 2070-218F, 2460-24FF, 2776-2793, 2C00-2DFF, 2E80-2FFF

3004-3007, 3021-302F, 3031-303F

3040-D7FF F900-FD3D, FD40-FDCF,

FDF0-FE44, FE47-FFFD

10000-1FFFD, 20000-2FFFD, 30000-3FFFD,
40000-4FFFD, 50000-5FFFD, 60000-6FFFD, 70000-7FFFD, 80000-8FFFD,
90000-9FFFD, A0000-AFFFD, B0000-BFFFD, C0000-CFFFD, D0000-DFFFD,
E0000-EFFFD

Java - class name starts with numbers

The first character needs to be a "Java letter", which includes letters, underscore and dollar sign.

See https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.8

An identifier is an unlimited-length sequence of Java letters and Java
digits, the first of which must be a Java letter.
[...]

The "Java letters" include uppercase and lowercase ASCII Latin letters
A-Z (\u0041-\u005a), and a-z (\u0061-\u007a), and, for historical
reasons, the ASCII underscore (_, or \u005f) and dollar sign ($, or
\u0024). The $ character should be used only in mechanically generated
source code or, rarely, to access pre-existing names on legacy
systems.
[...]

Letters and digits may be drawn from the entire Unicode character set,
which supports most writing scripts in use in the world today,
including the large sets for Chinese, Japanese, and Korean. This
allows programmers to use identifiers in their programs that are
written in their native languages.

Is there a workaround to make CSS classes with names that start with numbers valid?

There are no CSS classes. The question logically splits to two questions: can you start a class name with a digit in HTML, and (assuming the answer is “yes”, as it is) if it does, how do you use the corresponding class selector in CSS?

Various HTML specifications impose various restrictions on class names, but in browser practice, and according to HTML5, there are no limitations, except that a class name cannot contain whitespace characters. So class=000000-8 is valid.

By CSS syntax rules, a CSS identifier cannot being with an unescaped digit. Therefore, a selector like .000000-8 is invalid. But the digit can be escaped, by CSS escaping rules: the selector

.\30 00000-8

or, equivalently,

.\00003000000-8 

is valid and matches an element with class=000000-8.

Needless to say, class names starting with a digit are best avoided, but if you have to work with them (e.g., because some HTML documents have them and you cannot change the markup), this is the way.

Can I use only numbers for naming a CSS class

No, it's not valid.
But the more important fact is that you shouldn't define classes with no meaning as that doesn't make any sense when looking at writing semantic markup.

In CSS1, a class name could start with a digit (".55ft"), unless it
was a dimension (".55in"). In CSS2, such classes are parsed as unknown
dimensions (to allow for future additions of new units) To make "2x" a
valid class, CSS2 requires the first digit to be escaped ".\32x" [2x]

CSS Validator

Starting Class or Method Names with Numbers, Why Not?

Besides what Jeffrey Hantin said, there are numeric constants such as

3e7  // 3x10^7
40L // C, C++, etc for a long integer
0x88 // hexadecimal

The general convention for identifiers which is used widely in most languages, is [S except for 0-9][S]* where S is some set of valid characters (A-Z, a-z, 0-9, sometimes _, $, or -) -- so the first character can't be a digit but the rest can.

Is it acceptable / correct to use integers as class names in this case

No. Class names cannot start or be digits. An alternative would be to make class names that end with digits to signify their meaning.

.column12 {
width: 100%;
}

.column6 {
width: 50%;
}

CSS class starting with number is not getting applied

For classes starting with numbers, you'll need to write

.\32 00-200 {
width: 200px;
height: 200px;
}

You'll probably want to avoid doing that.

The \32 represents the digit "2". The space following it is necessary to terminate the escape sequence.

The reason for this is that "CSS identifiers" may not start with numbers. In CSS, class names used in selectors are considered "CSS identifiers". Therefore, the leading number must be escaped in the way shown above. Note that there is no restriction from the HTML perspective on class names, other than they may not contain spaces. So you could write <div class="%^*+lt;", as long as you were willing to figure out how to write that in escaped form in your CSS file.

See the question suggested as a duplicate for more information.

is there a rule about not putting numbers in your class names?

You're not allowed to have numbers at the start, but otherwise you should be right.

class H2O
end

H2O.new # Works fine

class 2Extreme
end

SyntaxError: compile error
(irb):5: trailing `E' in number
class 2Extreme
^
(irb):5: syntax error, unexpected tIDENTIFIER, expecting tCOLON2 or '[' or '.'
from (irb):6
from :0


Related Topics



Leave a reply



Submit