Fog of details ahead!
Do not 'learn off'.
Look-up when needed.
<p>
To a child, a cardboard box is a place to
hide, a place to sleep. A racing car, a
ship on the high but dry seas. Upturned,
it is a table, a drum. A platform for the
imagination. But, made of card, it is
soon discarded.
<p>
p {
margin: 1em;
border: 1px solid black;
padding: 2em;
width: 30em;
}
This is the content of the element, e.g. the text, images, etc.
You can set the width — but see previous lecture!
p {
width: 50%;
}
You can set the color
— this is the colour of the text
p {
color: red;
}
Four properties whose values can be in ems, pixels or other units.
p {
padding-top: 0.5em;
padding-right: 1em;
padding-bottom: 0.5em;
padding-left: 1em;
}
If you use percentages, they are based on the width of the element's parent.
Various shorthand versions, including:
p {
padding: 0.5em 1em 0.5em 1em;
}
p {
padding: 0.5em 1em;
}
p {
padding: 0.5em;
}
The color
property affects the text
The background-color
property affects the content + padding only, e.g.
p {
color: white;
background-color: blue;
}
What colours are allowed?
There are 148 named colours that browsers will understand.
W3School's list of colour names
RGB specifies 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%) |
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 |
The background-image
also affects the content + padding only, e.g.:
p {
background-image: url("background.png");
}
Background colours display behind background images, which display behind content.
background-repeat
background-attachment
background-position
background-size
You must specify colour, width and style
(e.g. solid, dashed, double,…):
p {
border-width: 1px;
border-style: solid;
border-color: black;
}
p {
border: 1px solid black;
}
If you don't set the border style, you will see nothing!
For rounded corners, specify the border-radius
:
p {
border: 1px solid blue;
border-radius: 16px;
}
To obtain a simple shadow:
p {
box-shadow: 2em 3em gray;
}
The values are the horizontal offset of the shadow, its vertical offset and its colour.
Margins are done similarly to padding, with the same shorthand.
p {
margin-top: 1em;
margin-right: 2em;
margin-bottom: 1em;
margin-left: 2em;
}
p {
margin: 1em 2em 1em 2em;
}
Again, if you use percentages, they are based on the width of the element's parent.
Adjacent bottom and top margins will not perform as you would expect: margin collapsing
Consider these three:
outer display type | type of element |
---|---|
block |
any |
inline |
replaced element, e.g. image |
inline |
non-replaced element, e.g. em |
In the case of the last of these three:
display: inline-block
to change this