31

Is this HTML valid? Or is the id 'a' the same as the id 'A'?

<div id="a">alpha</div>
<div id="A">Alpha</div>
Josh Gibson
  • 21,808
  • 28
  • 67
  • 63

5 Answers5

38

Yes. It is case-sensitive. Attribute values are always case-sensitive. Different browsers seem to be doing different things though.

Handling document.getElementById is different across browsers:

  1. Mozilla performs case-sensitive search.

  2. Internet Explorer: IE 8 and later performs case-sensitive search, while IE 7 and earlier performs case-insensitive search.

notnoop
  • 58,763
  • 21
  • 123
  • 144
  • 6
    UPDATE – 2019: IDs in all modern browsers are CASE-SENSITIVE ("ABC" != "abc"); tested in: Mozilla Firefox 64, Google Chrome 71, Internet Explorer 11, Microsoft Edge (Chakra) 41 and Opera 57. – Oleg Zarevennyi Jan 01 '19 at 18:11
20

Bit of clarification here since all the above answers are only partially correct. In the context of the DOM and Java Script yes, ID's are case sensitive. In CSS they are not, as CSS is entirely case insensitive.

http://www.w3.org/TR/css3-selectors/#casesens

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. For example, in HTML, element names are case-insensitive, but in XML, they are case-sensitive. Case sensitivity of namespace prefixes is defined in [CSS3NAMESPACE].

Because of this it is a bad idea to have two id's in different cases since you won't be able to style them independently by id.

Michael Morris
  • 531
  • 5
  • 7
  • 1
    Note that CSS **itself**, e.g. `wiDth: 100%` is case-insensitive, but the HTML selectors used in CSS are actually case-sensitive. So this is not correct. See https://stackoverflow.com/questions/12533926/are-class-names-in-css-selectors-case-sensitive Still it would probably not be a good idea to use mixed case ids – xji Aug 17 '18 at 15:42
  • 2
    @xji one possible use case is for randomly generated Ids to avoid collisions. Being able to use upper case and lower case letters allow to reduce length. – chmike Feb 05 '19 at 14:39
2

Well, you can test this pretty easily... But yes, they are case-sensitive.

Shog9
  • 156,901
  • 35
  • 231
  • 235
2

Consider the following element:

<div id="Example"></div>

In modern browsers, most JavaScript methods used to obtain an Element object by id are case-sensitive:

document.getElementById('Example')         // <div id="Example">
document.getElementById('example')         // null
document.querySelector('#Example')         // <div id="Example">
document.querySelector('#example')         // null
document.querySelector('[id="Example"]')   // <div id="Example">
document.querySelector('[id="example"]')   // null

On the other hand, you can use the case-insensitive attribute selector to select an element by id regardless of capitalization:

document.querySelector('[id="Example" i]') // <div id="Example">
document.querySelector('[id="example" i]') // <div id="Example">

The method above will work for all HTML attributes values within the ASCII range.

Though not recommended, you can also use the case-insensitive search regular expression flag to obtain an element by id independently of the case. This method has the potential to be used for more than just case-insensitive pattern matching:

[...document.querySelectorAll('[id]')].find(e => /^Example$/i.test(e.id)) // <div id="Example">
[...document.querySelectorAll('[id]')].find(e => /^example$/i.test(e.id)) // <div id="Example">

In regards to CSS, the ID selector (#example) is case-insensitive, while the ID attribute selector ([id="example"]) is case-sensitive, unless you use the case-insensitive attribute selector ([id="example" i]):

#Example         { /* ... */ } /* Match */
#example         { /* ... */ } /* Match */
[id="Example"]   { /* ... */ } /* Match */
[id="example"]   { /* ... */ } /* No Match */
[id="Example" i] { /* ... */ } /* Match */
[id="example" i] { /* ... */ } /* Match */
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
1

It is valid on all modern browsers (IE 8+) but I do not recommended it because CSS is case-insensitive. It's better to stick to one case to avoid any possible confusion or errors with CSS.

Dan Bray
  • 7,242
  • 3
  • 52
  • 70