Within a CSS rule, the selector specifies which HTML elements will be affected by the rule. There are several ways to specify a CSS selector:
a { color: white; }
This rule will apply to all anchor (a) elements within the linked HTML code.
p.footer { font-style: italic; }
This rule will apply to any paragraph (p) element that has the attribute class="footer". It will not apply to other p elements. It will not apply to other HTML elements, even if they have the attribute class="footer".
If no HTML element name is specified, the rule will apply to all HTML elements with the appropriate class. An example is shown below:
.figure { margin-left: auto; margin-right: auto; }
This rule will apply to any HTML element that has the attribute class="figure".
p#footer { font-style: italic; }
This rule will apply to the paragraph (p) element that has the attribute id="footer". There can only be one such element within a piece of HTML code because the id attribute must be unique for all elements. This means that the HTML element name is redundant and can be left out. The rule below has the same effect as the previous rule:
#footer { font-style: italic; }
Paul Murrell
This document is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 License.