CSS selectors

Derek Bridge

Department of Computer Science,
University College Cork

CSS selectors

Aims:

Cascading Style Sheets (CSS)

h1 
{
    color: red; 
    background-color: blue;
}

Attaching a stylesheet to an HTML document

CSS selectors

Find selectors

HTML: the id attribute

Running example: mojitos.html

Find selectors: by id

HTML: the class attribute

Find selectors: by class

Find selectors: child and descendant

Class exercise

Observation

Find selectors: summary

*Matches all elements
EMatches all elements with tag name E
E#IMatches all E elements with id of I. Omitting E is the same as *#I
E.CMatches all E elements with class of c. Omitting E is the same as *.C
E > FMatches all F elements that are children of E elements
E FMatches all F elements that are descendants of E elements
E + FMatches all F elements that are immediately preceded by sibling E
E ~ FMatches all F elements preceded by sibling E

Filter selectors: by attribute

Filter selectors: by dynamic pseudo-class

Filter selectors: by structural pseudo-class

And there are even more selectors