Skip to article frontmatterSkip to article content
tools = require('../js/tools'); tools.init()
Loading...

the need for selectors

there is a need for more accurate / selective settings
remember when we styled our first hyperlink ? our CSS clause was something like this:

a {
    color: red;
    font-family: times;
}

however it will apply the settings on ALL <a> elements
that may be what we want, but in some cases we also need more selective mechanisms

id= : assign a unique identifier

Loading...
Loading...

class= : styling elements by class

Loading...

summary of basic selectors

let’s summarize

selectorapplies to elements
pany element tagged <p>
#someidentthat have id='someident'
.someclassthat have someclass in their class attribute
h1.someclasstagged <h1> and of class someclass
h1.someclass#someidtagged <h1> and of class someclass and with id='someid'
.yes.noany element that has class yes and class no

cascading and inheritance

cascadinginheritance
the most specific rule winsif not set on me, take the value from my parent

cascading & specificity

in a nutshell, the intuition behind the actual rules is that

specificity example

in the 4 examples below, the CSS is unchanged throughout; we will see

  1. the <p> element with a style, an id and a class attributes
    so it would match all the CSS rules

  2. then we drop the style attribute

  3. and then the id attribute

  4. and finally when we drop the class attribute
    there is only one rule left to apply

#(1) embedded style= wins

Loading...

#(2) then id= wins

Loading...

#(3) then class= wins

Loading...

#(4) then the element’s tag wins

Loading...

inheritance

Loading...

inheritance - why

the point is that

inheritance & the body rule

body {
    font-family: Times;
    font-size: 18px;
}

see also

here is some further reading on these topics