I keep reading everywhere that CSS is not case sensitive, but I have this selector
.holiday-type.Selfcatering
which when I use in my HTML, like this, gets picked up
<div class="holiday-type Selfcatering">
If I change the above selector like this
.holiday-type.SelfCatering
Then the style does not get picked up.
Someone is telling lies.
And as HTML is case-sensitive for (most) attributes, the selectors are processed in a case-sensitive way.
CSS syntax is case-insensitive within the ASCII range (i.e., [a-z] and [A-Z] are equivalent), except for parts that are not under the control of CSS. For example, the case-sensitivity of values of the HTML attributes "id" and "class", of font names, and of URIs lies outside the scope of this specification.
What is a class selector in CSS? In CSS, a class selector is formatted as a period (.) character followed by the name of the class. It selects all elements with that class attribute so that unique CSS declarations can be applied to those specific elements without affecting other elements on the page.
CSS selectors are generally case-insensitive; this includes class and ID selectors.
But HTML class names are case-sensitive (see the attribute definition), and that's causing a mismatch in your second example. This has not changed in HTML5.1
This is because the case-sensitivity of selectors is dependent on what the document language says:
All Selectors syntax is case-insensitive within the ASCII range (i.e. [a-z] and [A-Z] are equivalent), except for parts that are not under the control of Selectors. The case sensitivity of document language element names, attribute names, and attribute values in selectors depends on the document language.
So, given an HTML element with a Selfcatering
class but without a SelfCatering
class, the selectors .Selfcatering
and [class~="Selfcatering"]
will match it, while the selectors .SelfCatering
and [class~="SelfCatering"]
would not.2
If the document type defined class names as case-insensitive, then you would have a match regardless.
1In quirks mode for all browsers, classes and IDs are case-insensitive. This means case-mismatching selectors will always match. This behavior is consistent across all browsers for legacy reasons, and is mentioned in this article.
2For what it's worth, Selectors level 4 contains a proposed syntax for forcing a case-insensitive search on attribute values using [class~="Selfcatering" i]
or [class~="SelfCatering" i]
. Both selectors will match an HTML or XHTML element with either a Selfcatering
class or a SelfCatering
class (or, of course, both). However there is no such syntax for class or ID selectors (yet?), presumably because they carry different semantics from regular attribute selectors (which have no semantics associated with them), or because it's difficult to come up with a usable syntax.
Perhaps not a lie, but need for clarification.
The actual css itself is not case sensitive.
For example
wiDth:100%;
but the names, they must be case sensitive to be unique identifiers.
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With