height
width
block
— 100%
inline
/ inline-block
— content widthwidth
& height
propertiesmin-
and max-
versionsmargin
border
padding
margin
margin
pushes elements away from other elements
Neighboring margin
s collapse.
E.g., margin-bottom: 10px
followed by margin-top: 20px
creates a 20px
gap.
margin
.margin
is pushing me in from my parent's edges.padding
padding
pushes an element's contents away from its edges
Note: there's a caveat with padding
coming later…
padding
.padding
is pushing my contents away from my edges.border
border
adds to an element's height and width (like margin
); it doesn't force its contents in (like padding
).
border
adds 20px
to my width & height.border
adds 10px
to my width & height.Element Contents
box-sizing
content-box
: padding can affect element size (browser default, ugh)
border-box
: padding doesn't affect element size (every reset/normalize CSS)
.box {
padding: 20px;
height: 140px;
width: 500px;
}
box-sizing: content-box
540px
widebox-sizing: border-box
500px
widedisplay
If an element is inline
, its padding
won't force it off the baseline, and it won't respond to vertical margin
.
If an element is inline-block
(or block
), its padding
will affect its baseline, and it will respond to vertical margin
.
em {
padding: 10px;
margin: 10px 40px;
background-color: rgba(0,0,0,.5);
}
float
float
makes an element's width
exactly what its contents need, even if it's a block
element.
All other elements (that aren't explicitly cleared) flow around a float
ed element.
display: table
display: flex
display: grid
display: table
Children behave like display: table-row
…
… unless they're set to be display: table-cell
.
display: block
(default)display: block
(default)display: table-cell
display: table-cell
display: flex
Children are sized according to flexbox rules applied to parent…
… and flexbox rules applied to children.
display:block
display:block
display:block
display:block; align-self: flex-end
display: grid
Children are sized according to grid rules applied to parent…
… and grid rules applied to children.
That's another talk though…
margin
border
padding
Element Contents
box-sizing
display
float
jdsteinbach