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.)
We may wish to set the width or height of a box or part of a box, e.g.
<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;
}
We can use absolute values or relative values
html {
background-color: white;
}
body {
width: 960px;
margin: 0 auto;
background-color: green;
}
html {
background-color: white;
}
body {
width: 80%;
margin: 0 auto;
background-color: green;
}
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;
}
You can set the width
, min-width
and max-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
: as wide as the available space but no more than max-content
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 |
If you want to set the width of an inline
element, use
display: inline-block;
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.