Menu

CSS Selectors

Selectors

A selector is the definition of a formatting style that can be applied to an HTML element. A rule has three basic parts:

Element {Declaration: Value}

The declaration and value are surrounded by {} braces, and the declaration is separated from the value by a colon : and a space.

The selector’s relation to common HTML is similar to this tag structure that is used to define the color of a font:

<font color="orange">

Where font is the element, color is the declaration and orange is the value of the declaration.

Type Selectors

Type selectors are used to set the definition of a certain type of element. If you wanted to assign a property to all of the <h1> elements in your document you would use the following:

h1 {color: #000fff}

The syntax is: element {declaration: value}

Multiple Declarations

If you would like to define more than one property for your element you can use a semicolon ; to separate declarations:

h1 {
    color: #000fff;
    font-weight: bold;
    font-size: 12pt;
}

The syntax is: element {declaration: value; declaration: value}

Grouping Elements

If you would like to apply the same properties to different elements you can use a comma , between the elements and use the same declarations and values:

h1, td, p {color: #000fff}

The syntax is: element, element, element {declaration: value}

Descendant Selectors

Contextual selectors are applied only if a certain element pattern is used in the HTML. If you wanted all italic text to be blue, but only if it is contained inside a bold element, you could define the selector like this:

b i {color: #000fff}

The syntax is: parent-element child-element {declaration: value}

In the following, the text would be bold, and the italic “e” would be rendered both italic and blue. Italic text that is not contained inside a b element will be rendered just plain italic and not blue.

<b>T<i>e</i>xt</b>

Adjacent Sibling Selectors

When you want to define a selector that is applied only if one element directly follows another element, you can use the following format:

p + i {color: #000fff}

The syntax is: first-element + second-element {declaration: value}

And it would be applied as follows:

<p>Chickens</p>
<i>happy chickens</i>

ID Selectors

If you would like to define a declaration and value that you can apply to elements in your document, but you do not want to apply it to all tags of one type, you can use an ID selector and then apply that ID to your tags as you wish:

<style>
#abc123 {color: #000fff}
</style>

<p id="abc123">text</p>

The syntax is: #id {declaration: value}

The HTML tag referenced: <tag id="id">

If you would like to set up optional declarations for a single element type, you can combine the element with an ID. This rule applies only to an h1 with that ID:

<style>
h1#abc123 {color: #000fff}
</style>

<h1 id="abc123">text</h1>

The syntax is: element#id {declaration: value}

The HTML tag referenced: <tag id="id">

Class Selectors

Class selectors are defined in a similar way to ID selectors. They can be applied to various HTML elements, or they can define a subset class of an element. Unlike an ID, a class can be used on many elements on the same page.

<style>
.chicken {color: #000fff}
</style>

<h1 class="chicken">text</h1>

The syntax is: .class {declaration: value}

The HTML tag referenced: <tag class="classname">

Pseudo-Classes

The properties of an element can change based on the state of the element by using a pseudo-class.

The anchor <a> element can have different rendering properties applied to it based upon its current state. The possible variations include:

  • link – unvisited links
  • visited – visited links
  • hover – user hovers
  • active – active links
<style>
a:hover {color: orange}
</style>

<a href="file.html">click here</a>

The syntax is: element:pseudo-class {declaration: value}