XML technologies / CSS / Advanced websites with CSS / Behind the scenes / Selectors and CSS rule structure / Element type selector

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

The most common selectors are individual elements without further qualification or extensions. A rule using an element type selector applies to every HTML element of that type unless another rule overrides it.

 

Table: CSS selector types.

Selector type

Effect

Examples in CSS syntax

Universal selector
(CSS2)

The rule applies to every element in the HTML document.

* (or no selector at all)

Element type

The rule applies to all HTML elements named in the selector.

h1
p

Class selector

The rule applies only to HTML elements whose attribute class matches the selector’s class.

.articletitle
h1.important

ID selector

The rule applies only to the HTML element whose attribute id matches the selector’s ID.

#special3
p#special52

Pseudo-element selector (CSS2)

The rule applies only to pseudo-elements.

p:first-letter
p:first-line
h1:first-child

Pseudo-class selector (CSS2)

The rule applies only to pseudo-classes when their state changes through user interaction.

a:hover
a:active
a:focus
a:link
a:visited
body:lang(d)

Descendant selector

The rule applies only to the rightmost HTML element in the selector if, through inheritance, it is a descendant of the element on the left.

p em
p.wide em

Parent-child selector (CSS2)

The rule applies only to the rightmost HTML element if it is an immediate child of the element on the left.

body > p

Adjacent sibling selector (CSS2)

The rule applies only to the rightmost HTML element if it immediately neighbors the element on the left in the document.

h1 + h2
p + h3

Attribute selector (CSS2)

The rule applies only to HTML elements containing the attributes or attribute values specified in the selector.

p[align]
input[type="text"]
img[alt~="none"]
body[lang|="en"]

The following example defines every hyperlink’s text and background colors as white on green:

a {
  color: white;
  background-color: green;
}

  

 

 

 

back to top