Fog of details ahead!
Do not 'learn off'.
Look-up when needed.
Every HTML element and run of text generates
zero, one or more CSS boxes
Visually, the tree of elements becomes a tree of boxes.
In CSS, we can style the boxes.
Every box has an outer display type,
either block or inline.
This determines how the box is laid out in flow layout.
display
Property
You can choose the outer and inner display types by setting a value for the display
property.
There are about 20 possible values, e.g.:
CSS | outer | inner |
---|---|---|
display: block |
block | flow |
display: inline |
inline | flow |
display: flex |
block | flexbox |
display: grid |
block | grid |
display: inline
header, nav, main, section, article, aside, footer,
figure, figcaption, div, p, h1, h2, h3, h4, h5, h6,
ul, ol, form, fieldset {
display: block;
}
leaving, e.g., em, i, strong, b, span, a, img,…
as inline.
You can change an element's display type in your own stylesheet, e.g.
img {
display: block;
}
(In a previous lecture, we wrapped an image inside a figure
to make it appear on a new line.)
<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.
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;
}
Top and bottom padding will not always perform as you would expect.
outer display type | type of element | vertical padding |
---|---|---|
block |
any | behaves as expected |
inline |
replaced element, e.g. image | behaves as expected |
inline |
non-replaced element, e.g. em |
will not push other boxes away |
The background-color
property affects the content + padding only, e.g.
p {
background-color: 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.
Top and bottom margins will not always perform as you would expect.
outer display type | type of element | vertical margins |
---|---|---|
block |
any | margin collapsing |
inline |
replaced element, e.g. image | margin collapsing |
inline |
non-replaced element, e.g. em |
will not push other boxes away |
The total width of a box on the screen is:
left margin + left border + left padding +
width of content
+ right padding + right border + right margin
You can set the width of an element, e.g.
p {
width: 50em;
}
But what does this affect?
just the content? the whole box? something else?
It depends on the box-sizing
property.
box-sizing
box-sizing: content-box
(default):
box-sizing: border-box
:
Ignoring margins, how wide will the content of this paragraph be? And what will the total width of the box be?
p {
box-sizing: content-box;
border: 5px solid black;
padding: 10px;
width: 500px;
}
Ignoring margins, how wide will the content of this paragraph be? And what will the total width of the box be?
p {
box-sizing: border-box;
border: 5px solid black;
padding: 10px;
width: 500px;
}
Many web developers believe that
box-sizing: border-box
is more intuitive than the default, especially for web page layout.
Hence, to make a global change, they include this in their CSS:
*, *:before, *:after {
box-sizing: border-box;
}
When setting the width, what values can you use?
auto
: the browser decides;max-content
: as wide as it needs to be;min-content
: as narrow as it can be;fit-content(n)
: min(max-content, max(min-content, n%))
You can also set max-width
and min-width
.
Setting the width will not always perform as you would expect.
outer display type | type of element | width |
---|---|---|
block |
any | behaves as expected |
inline |
replaced element, e.g. image | behaves as expected |
inline |
non-replaced element, e.g. em |
has no effect |
You can also set the height of an element, similarly.
Advice:
Avoid setting heights, if you can.
If you set a height, the browser may need to display horizontal scrollbars — which users dislike.