In order to create a selector that will match particular elements within an HTML document, the ID selector should be used. Specifically, an ID selector will match only the elements whose ID attribute is set to that ID selector. Furthermore, it is important to note that ID selectors are unique (their values unique) and as such, ID selectors cannot match more than one element in an HTML document. ID selectors use the # sign, and the basic syntax follows:
For example, if a developer wants to define a specific set of CSS properties for the footer, the following piece of code should be used:
Since an ID selector identifies an HTML element uniquely, it should be used “only" when necessary; in other words, a developer should consider carefully when to use an ID selector and when to use a class selector. Examples of “unique" (where the ID should be used instead of the class) HTML elements are the footer, header, content, main, sidebar and similar “core" elements of the website.
Furthermore, it is possible to use an ID selector in the following way:
This line of code will apply only to an unordered list element whose ID attribute value is equal to footer. As it is the case with class selectors, whitespace between # and ID name is not allowed.