Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.


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;
}

but the settings apply 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

an element that is unique in your document can be attached a unique identifier

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

and you can create combinations, e.g.

selectorapplies to elements
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

in a nutshell :

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 specificity rules is



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 all the CSS rules match, but the style= clause wins

  2. then we drop the style attribute, and the id rule wins

  3. and we drop the id attribute, so the class rule wins

  4. and finally when we drop the class attribute, and 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 we do not style the <p> and <li> elements specifically

so in this case the properies are fetched from their parent (the <div> element)

(note that not all properties behave that way though)


inheritance - what

Not all properties are inherited though
In CSS, typography and color properties usually inherit by default (making it easy to style a whole page at once), while box-model properties (like border, margin, padding, and width) do not. If borders inherited, every single nested element would get its own box, creating a visual nightmare!

some inherited properties

color, font-family, font-size, line-height, text-align, visibility, cursor,

some non-inherited properties

border, margin, padding, width, height, background, position, display, z-index


inheritance & the body rule

so, it is common practice to create a rule for the body element
this way we can tweak the global style of the page

1
2
3
4
body {
    font-family: Times;
    font-size: 18px;
}

see also

here is some further reading on these topics