We can set the color
of an element:
affects the colour of the text.
We can set the background-color
of an element too.
There are 140 named colours that browsers will understand.
W3School's list of colour names
RGB specifes colours by giving values for three channels.
p {
color: rgb(255 0 0);
}
p {
color: #FF0000;
}
p {
color: rgb(100% 0% 0%);
}
rgb(0 255 0)
rgb(255 255 255)
rgb(0 0 0)
rgb(255 255 0)
rgb(255 250 0)
rgb(169 169 169)
rgb(211 211 211)
A fourth channel, the alpha channel, can specify transparency.
rgb(137 33 242 / 0.8) |
rgba(137 33 242 / 0.8) |
rgb(137 33 242 / 80%) |
rgba(137 33 242 / 80%) |
HSL specifies colours in a different way:
color(display-p3 1 0 0.331) |
vibrant pink color |
lab(40% 83 -104) |
a shade of purple |
lch(69% 56 244) |
a shade of blue |
You'll probably pick a colour scheme: a small number of colours that you use throughout your web site.
To change the colour scheme, you may have to make changes to many CSS rules.
This is a case where CSS custom properties can help.
header, nav, aside, footer {
color: black;
background-color: lightgray;
}
main {
color: white;
background-color: green;
}
table {
border: 1px solid green;
}
They must be defined within a CSS rule, e.g.:
html {
--base-color: black;
--base-bgcolor: lightgray;
--main-color: white;
--main-bgcolor: green;
}
Their name must begin with --
and, unlike the rest of CSS, their name is case-sensitive.
The var
function retrieves the value of the custom property, e.g.:
header, nav, aside, footer {
color: var(--base-color);
background-color: var(--base-bgcolor);
}
main {
color: var(--main-color);
background-color: var(--main-bgcolor);
}
table {
border: 1px solid var(--main-bgcolor);
}
font-size
property, e.g.:
header {
font-size: 2em;
}
These values do not depend on any others.
Pixels | e.g. 16px
|
---|---|
Points = (1/72th inch) | e.g. 12pt
|
On the web, we might use px
occasionally;
otherwise, avoid absolute font sizes.
These values depend on other values,
e.g. in some cases, the parent's value.
em | e.g. 2em |
relative to parent's font size |
---|---|---|
rem (root em) | e.g. 2rem |
relative to root's font size |
You can also use percentages, e.g. 80%
, which are relative to the parent's font size, but this
isn't common with font sizes.
…
<section>
<h2>Did you know?</h2>
<p>
Badgers are nocturnal.
<p>
…
</section>
…
:root {
font-size: 16px;
}
section {
font-size: 2em;
}
h2 {
font-size: 1.5em;
}
p {
font-size: 1.5rem;
}
The text in the h2
is 48px and the text in the p
is 24px.
em
or rem
for all other rules.
:root {
font-size: 16px;
}
h1 {
font-size: 2em;
}
section h2 {
font-size: 1.5em;
}
section section h3 {
font-size: 1.17em;
}
section section section h4 {
font-size: 1em;
}
p {
width: 50em;
}
font-size |
width |
|
---|---|---|
em | relative to the parent's font size | relative to this element's font size |
% | relative to the parent's font size | relative to the parent's width |
Consider using custom properties again.
:root {
--base-font-size: 16px;
--base-bottom-margin: 2em;
}
main {
font-size: var(--base-font-size);
}
header {
font-size: calc(2.5 * var(--base-font-size));
margin-bottom: calc(1.5 * var(--base-bottom-margin))
}
p {
margin-bottom: var(--base-bottom-margin);
}