XML technologies / ePUB / CSS essentials / Selectors

(Excerpt from “E-Books mit ePUB ─ Von Word zum E-Book mit XML” by Dr. Victor Wang)

A selector defines the context in which a CSS instruction applies. The simplest is an element selector matching the element name. Other types follow in the table.

Selector

Example

Meaning

Standard selectors

Element selector

h1 {color: #0000FF;} h1, h2 {color: #0000FF;}

All h1 elements, or h1 and h2, are colored red

Universal selector

* {margin: 0; padding: 0;}

All elements have spacing set to 0

Class selector

h2.Teil {color: #0000FF;}

All h2 elements with class attribute class="Teil" are colored red

ID selector

name#froschkoenig {color: #0000FF;}

The name element with ID attribute id="froschkoenig" is colored red

 

#kopf {color: #0000FF;}

All elements with ID attribute id="kopf" are colored red

Attribute selector

h2[typ] {color: #ff0000;}

All h2 elements with an attribute typ are colored red

 

h2[typ ~="Abschnitt"] {color: #ff0000;}

All h2 elements whose attribute typ contains the substring "Abschnitt" are colored red

Advanced selectors

Descendant selector

#kopf p {color: #ff0000;}

All p elements inside an element with ID attribute id="kopf" are colored red

 

ul a {text-decoration: none;}

All a elements within <ul> have no link underline

Child selector

div > a {color: #ff0000;}

All a elements that are children of <div> are colored red

Adjacent sibling selector

div + a {color: #ff0000;}

All a elements immediately following <div> are colored red

Table: CSS selectors

   

 

 

 

back to top