Is CSS case-sensitive?
Introduction
CSS itself is not case-sensitive, but it can be if you are using an XHTML document type.
Let’s dive deeper and learn about all the nuances.
The case-sensitiveness of CSS in detail
-
Selectors: The HTML elements, classes, and IDs that you reference in your CSS file are case-sensitive based on the document type.
For example, in HTML, the element names written in the HTML are case-insensitive, meaning<div>
and<DiV>
are considered the same. However, in XHTML (an XML-based version of HTML), case matters, so<div>
and<DiV>
would be considered different elements. Similarly, class and ID selectors are case-sensitive in all document types. So, if you have an element withid="example"
, you must reference it in your CSS file as#example
and not#Example
. -
Properties: Property names defined by the W3C standards are case-insensitive. So,
background-color
andBackGround-COLOR
are considered the same. That being said, it is recommended to use lowercase for property names to maintain readability and consistency. -
Property Values: In general, property values are case-insensitive, except for font names and URLs. So,
red
andRED
are considered the same color. However, the values of thefont-family
,url
, andattr
are case-sensitive.
Conclusion
In summary, it’s best practice to always use lowercase for HTML elements, attributes, and CSS properties and values (except for font names and URLs) to ensure consistency and avoid any potential issues.