tools = require('../js/tools'); tools.init()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¶
an element that is unique in your document
can be attached a unique identifier
class= : styling elements by class¶
it is also possible to create arbitrary groups of elements
so that they can be styled together
this is simply done by setting a
classattributean element can - and often has - several classes
e.g.
class="one-class another-class"
summary of basic selectors¶
let’s summarize
| selector | applies to elements |
|---|---|
p | any element tagged <p> |
#someident | that have id='someident' |
.someclass | that have someclass in their class attribute |
h1.someclass | tagged <h1> and of class someclass |
h1.someclass#someid | tagged <h1> and of class someclass and with id='someid' |
.yes.no | any element that has class yes and class no |
cascading and inheritance¶
cascading : what happens if several rules define,
say, the ‘color’ property on one element ?inheritance : what happens if no rule defines
the ‘color’ property on an element ?in a nutshell :
| cascading | inheritance |
|---|---|
| the most specific rule wins | if not set on me, take the value from my parent |
cascading & specificity¶
in a nutshell, the intuition behind the actual rules is that
if you have defined a property in a
styleattribute, i.e. in the very node, it means you want this property to applyotherwise if the selector specifies an
id, this means you expect this setting to be valid on that nodeotherwise if you have specified a
class, it should applyotherwise if the rule is based on the element’s tag, it should apply
otherwise, if it is a wildcard rule (you can use
*as the selector)
how to compute specificity
selectors can be more convoluted than what we’ve seen so far,
(more on this later on) but the logic to compare
specificity can be reasonably approximated as follows :
for each property setting, compute a tuple with
1 or 0 whether the property setting is in a
style=attributenumber of applicable
ids in the selectornumber of applicable
classes in the selectornumber of applicable
elements in the selector
compare the tuples - like Python would do
specificity example¶
in the 4 examples below, the CSS is unchanged throughout; we will see
the
<p>element with astyle, anidand aclassattributes
so it would match all the CSS rulesthen we drop the
styleattributeand then the
idattributeand finally when we drop the
classattribute
there is only one rule left to apply
#(1) embedded style= wins¶
#(2) then id= wins¶
#(3) then class= wins¶
#(4) then the element’s tag wins¶
inheritance¶
inheritance - why¶
the point is that
we do not style the
<p>and<li>elements specificallyso in this case the properies are fetched
from their parent (the
<div>element)that is targetted by our CSS rule
note that not all properties behave that way though
inheritance & the body rule¶
however, it is common practice to create a rule
whose selector targets thebodyelement
to tweak the global style (typicallyfont-familyandfont-size)
body {
font-family: Times;
font-size: 18px;
}see also¶
here is some further reading on these topics
list of properties and terms
reference (hard to read) : detailed description of cascading and inheritance
more readable explanations on specificity :