Topics for today
- Inheritance
- Values & Units
- Shorthands
- DRY
- Pseudo-elements
- Custom properties for cascading variables
!important?Related: Inheritance, that we will cover next week
body {
background: yellow;
}
body {
background: pink !important;
}
button {
border: none;
}
button {
border: 2px outset #777;
}
button {
border: none;
}
button {
border: 2px outset #777 !important;
}
(Unless !important is involved)
#id selectors.classes, :pseudo-classes, [attributes](1, 0, 0) > (0, 100000000, 100000000)
| Selector | Specificity |
|---|---|
style="background: red" |
(∞, ∞, ∞) |
:not(em, strong#foo) |
(1, 0, 1) |
:is(em, #foo) |
(1, 0, 0) |
:where(em, #foo) |
(0, 0, 0) |
.foo.foo.foo:not(#nonexistent)#foo to [id="foo"]:where()p,
em,
mark
elements have the same font family, font size, and color that we set on their ancestor,
body
body? Does it get inherited too?
background
font
border
text-decoration
display
color
padding
white-space
margin
text-shadow
box-shadow
box-sizing
outline
--*
Inherited Not inherited
!important?Where does inheritance fit in?
inherit is an explicit value that makes any property inherit
border: .3em dotted steelblue;border: dotted .3em steelblue;border: steelblue .3em dotted;What happens if we swap the order of these two declarations? Why??
padding: .2em .5em .1em .5em;background: url(foo.png) 50% 50% / 100% auto no-repeat gold;background: gold no-repeat 50% 50% / 100% auto url(foo.png);font: 120%/1.5 Helvetica Neue, sans-serif;
background: url(cat.jpg)
0 10% / 100% 100%
no-repeat
content-box border-box
fixed;
background-image: url(cat.jpg);
background-position: 0 10%;
background-size: 100% 100%;
background-repeat: no-repeat;
background-origin: content-box;
background-clip: border-box;
background-attachment: fixed;
Every piece of knowledge must have a single, unambiguous, authoritative representation within a systemThe Pragmatic Programmer, Andy Hunt and Dave Thomas
def right_triangle_perimeter(a, b, c):
return a + b + c
# ... later on
p = right_triangle_perimeter(3, 4, 5)
from math import sqrt
def right_triangle_perimeter(a, b):
return a + b + sqrt(a**2 + b**2)
# ... later on
p = right_triangle_perimeter(3, 4)
.red-button {
background: hsl(0, 80%, 90%);
}
.primary-button {
background: hsl(0, 80%, 90%);
}
button {
border-radius: 5px;
padding: 5px 12px 6px;
font-size: 24px;
line-height: 24px;
}
button.large { font-size: 46px; }
button {
border-radius: .2em;
padding: .2em .5em .25em;
font-size: 100%;
line-height: 1;
}
button.large { font-size: 200%; }
.tab {
border-radius: .3em .3em 0 0;
padding: .1em .5em .1em .5em;
margin: 0 .1em 0 .1em;
}
.tab {
border-radius: .3em;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
padding: .1em .5em;
margin: 0 .1em;
}
Three strikes and you refactor
Duplication is far cheaper than the wrong abstractionSandy Metz
-- and behave like normal CSS properties.var() function and we can use that anywhere (including inside calc()), except inside url().