A simple selector is a query used to target one or more DOM elements.
Complex selectors can be created using multiple simple selectors, separated by
combinators (white-space, greater-than (>) or plus-sign (+)).
The elements of the DOM that match a selector are called
subjects.
The
specificity of a selector determines which selector is used when multiple selectors target the same subject
(for details, refer to:
The CSS 2 Specification,
MDN Specificity and
Specifics on CSS Specificity).
Specificity is determined in the following order and weight:
- An element's inline style attribute (highest precendence)
- The number of ID attributes
- The number of other attributes and pseudo-classes
- The number of element names and pseudo-elements
The Universal Selector ( * )
Selects any element. The following example selects all elements and sets their color to green.
Be aware that using this as a CSS reset will eliminate parent-child inheritance on all selected elements.
Here are some good examples of Things It Might Be Fun/Useful to Try the Universal Selector
On.
The Element Selector
Selects all elements of the specified type. The following example selects all paragraphs.
The ID Selector ( # )
Selects the single element that has the specified ID.
The following example selects the element with the "bar" ID.
The Class Selector ( . )
Selects all elements with the specified class name.
The example below selects all elements that have the "foo" class.
The primary reason that class and ID selectors exist is to give the page content semantic meaning.
When used in this way, it also makes it easier to style.
Jeffrey Zeldman coined the term classitis to describe the CSS bloat that occurs when classes are used on every element that requires styling.
For details, see Chronic Divitis And Classitis, by Adam Kahtava.
Multiple classes may be combined to select elements that include all of the specified classes.
To achieve this, separate each class with a period.
The example below selects all elements that include both a "ribbon" and a "right" class.
-
- .ribbon.right {
- right: -50px;
- transform: rotate(45deg);
- }
- <div class="ribbon right green">
-
<i>Happy Birthday!</i>
- </div>
The Descendent Selector ( space )
Selects elements that are descendants of a specified parent element.
The descendants can appear at any level.
The example below selects every element, with a 'bar' class, nested anywhere inside any element with a 'foo' class.
-
- .foo .bar {
- color: #f90;
- }
-
<div class="foo">
-
Some text. <b class="bar">Descendent bold text</b>.
-
<div>Some <b>more</b> text.</div>
- <div>
-
<cite class="bar">Descendent cite.</cite>
- </div>
-
<cite>Another cite.</cite>
- </div>
The following example uses the universal selector to require at least one element be present between the .foo and .bar elements:
- .foo * .bar {
- color: #f90;
- }
-
<div class="foo">
-
Some text. <b class="bar">Too shallow to be selected</b>.
-
<div>Some <b>more</b> text.</div>
- <div>
-
<cite class="bar">Descendent cite.</cite>
- </div>
-
<cite>Another cite.</cite>
- </div>
The Child Selector ( > )
Selects elements that are
direct descendants of a specified element (first-level children).
This is similar to the descendent selector, except it applies only to first-level descendents.
The example below selects every <cite> element, nested at the first-level, inside any <div> element that has the "foo" class.
Note: The "This inherits from cite" text, inside the <b> tag, is also affected, but only because of inheritance.
If a color reset was used (e.g., "* {color:black;}"), then the text would remain black.
-
- div.foo > cite {
- color: #f90;
- }
-
<div class="foo">
- Some text <b>before</b> the first cite.
-
<cite>A first-level child cite. <b>This inherits from cite.</b></cite>
- <section>
- <cite>Not a first-level child cite.</cite>
- </section>
-
<cite>Another first-level child cite.</cite>
- </div>
The Adjacent Selector ( + )
Selects the specified element
immediately following the specified
sibling element.
The example below selects the first <cite> element after any <div> element that has the "foo" class.
-
- div.foo + cite {
- color: #f90;
- }
-
<div class="foo">
-
<cite>Not an adjacent cite.</cite>
- </div>
- Some text *before* the first cite (no elements allowed).
- <cite>An adjacent cite. <b>This inherits from cite.</b></cite>
-
<cite>Not an adjacent cite.</cite>
The Attribute Selector ( [] )
Selects all elements that have the specified attribute or matches the atrribute query, as shown below.
- [attribute-name] Matches all elements that have the exact attribute name:
-
- [data-foo] {
- color: #f90;
- }
-
<div data-foo>Matches (data-foo)</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
- <div data-foo-bar>Does not match (data-foo-bar)</div>
- [attribute-name="attribute-value"] Matches all elements that have the exact attribute name and value.
-
- [data-foo="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Does not match (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="foo bar">Does not match (data-foo="foo bar")</div>
- <div data-foo="foobar">Does not match (data-foo="foobar")</div>
- [attribute-name~="attribute-value"] Matches all elements that have the exact attribute name plus
the exact value as a whitespace separated substring.
-
- [data-foo~="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Does not match (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="foo bar">Matches (data-foo="foo bar")</div>
- <div data-foo="foobar">Does not match (data-foo="foobar")</div>
- [attribute-name|="attribute-value"] Matches all elements that have the exact attribute name plus
the exact value or the exact value followed by a dash (-) with optional trailing characters.
-
- [data-foo|="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Does not match (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="bar-">Matches (data-foo="bar-")</div>
- <div data-foo="bar-car">Matches (data-foo="bar-car")</div>
- [attribute-name^="attribute-value"] Matches all elements that have the exact attribute name and start with the specified value.
-
- [data-foo^="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Matches (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="foobar">Does not match (data-foo="foobar")</div>
- <div data-foo="bar-car">Matches (data-foo="bar-car")</div>
- [attribute-name$="attribute-value"] Matches all elements that have the exact attribute name and end with the specified value.
-
- [data-foo$="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Does not match (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="foobar">Matches (data-foo="foobar")</div>
- <div data-foo="bar-car">Does not match (data-foo="bar-car")</div>
- [attribute-name*="attribute-value"] Matches all elements that have the exact attribute name and contains the specified value.
-
- [data-foo*="bar"] {
- color: #f90;
- }
-
<div data-foo="bart">Matches (data-foo="bart")</div>
- <div data-foo="bar">Matches (data-foo="bar")</div>
-
<div data-foo="foobar">Matches (data-foo="foobar")</div>
- <div data-foo="bar-car">Matches (data-foo="bar-car")</div>
The Pseudo Selector ( : / :: )
The pseudo selector (pseudo-classes and pseudo-elements) matches useful facets in a DOM, based on information that's not directly represented
(e.g., the first character in the content of a <div>).
Below are a few sample pseudo selectors (see
Meet the Pseudo Class Selectors for more examples):
Pseudo-Class Selectors (:)
- :link pseudo-class - applies to links that have not been visited.
- :visited pseudo-class - applies to links that have been visited.
- :hover pseudo-class - applies to the element under the pointing device.
- :active pseudo-class - applies to the element under the pointing device as it's being selected (e.g., while the mouse-button is down).
- :focus pseudo-class - applies to the element that accepts keyboard events.
Pseudo-Element Selectors (::)
- ::first-letter pseudo-element - applies to the first letter of a block.
- <article>
- <p>The first paragraph.</p>
-
<p>Followed by another paragraph.</p>
- </article>
- article::first-letter {
- font-size: 18px;
- color: #f90;
- }
An example using multiple blocks.
- article p::first-letter {
- font-size: 18px;
- color: #f90;
- }
- ::before and ::after pseudo-elements -
inserts and formats additional content, within a targeted element, before or after the existing content of the element.
- header i::after {
- content: "\2666";
- color: #f90;
- font-style: normal;
- }
-
<header><i>After</i> - pseudo-element Example</header>
Selector Grouping ( , )
At times, the same CSS will need to be applied to a variety of elements using different selectors.
For example, if you wanted to set the color of every <header> and <footer> to peru, you might be tempted to write the CSS as follows:
- header {
- color: peru;
- border-bottom: thin solid #ddd;
- }
-
- footer {
- color: peru;
- }
Selector grouping can often provide a solution that's simple and easier to maintain:
- header {
-
border-bottom: thin solid #ddd;
- }
-
- header, footer {
- color: peru;
- }