Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I safely use unicode characters (e.g. accents) in CSS class names or ids?

Tags:

css

I just encountered a situation where product names are automatically pulled in to my HTML as class names, and one of those product names has an accented letter. It looks like this:

<div class="español">Hola</div>

If I add a CSS declaration with that class, like this:

.español {background:yellow;}

Will it cause any problems? It seems to work so far, but I'm not sure if it's completely cross-browser compatible.

Also, would it be any different if that were an id instead of a class? That seems to work so far, too, but again I am not sure if it'll hold up everywhere.

like image 269
andi Avatar asked Oct 01 '13 18:10

andi


People also ask

What characters are allowed in CSS class names?

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.

How do I use Unicode characters in CSS?

To add a Unicode character to the CSS content property, you can do either of the following: Add a Unicode character directly, or; Use the Character's Hexadecimal Unicode Code Point Value.

Which of the following symbols should you use to target a class name in CSS?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

Can a class and ID have the same name CSS?

Yes, you can use same name for both id and class because both parameters have their own significance.


2 Answers

From the CSS specification:

"In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_)"

The ISO 10646 standard and Unicode standard have synchronised their character sets (ref), so in this aspect they are the same.

The ñ character has the character code U+00F1, so that is safe to use in an identifier.

like image 156
Guffa Avatar answered Oct 07 '22 01:10

Guffa


Apparently yes. In fact, HTML 4.01 already allowed you to use Unicode characters in the class attribute. Now HTML 5 allows them also on the id attribute. The cool thing is that it's been tested with IE 6 and works too, so it's backwards compatible.

Now what you should ask yourself is, do I really need them? In my eyes is just asking for trouble because while the W3 accepts them, some not-major browser might not support them (think browsers for the visually impaired or others).

Read this for more info on the subject: http://mathiasbynens.be/notes/html5-id-class.

like image 43
federico-t Avatar answered Oct 07 '22 00:10

federico-t