The CSS Box Model
The primary rules to commit to memory about the CSS box model:
- Block level elements
- Take up the height of their content, plus any margin, padding, or border.
- Take up the full width of their parent's content box.
- Can have an explicit width or height, in which case the above are ignored.
- Appear on their own "line", i.e. no other elements may appear beside them.
- Inline level elements
- Cannot have height or width set on them
- Take up the height of their
line-height
, regardless of any margin, padding, or border. - Take up the width of their content, plus any margin, padding, or border.
- Appear next to any other inline content.
http://learnlayout.com/ outlines a number of things, however here are some general takeaways from the site (and lecture):
There is something called the Box Model. Every element has a Box with
- width and height (content),
- padding,
- border,
- and margin.
EVERY element has a default style, given to it by a default stylesheet. EVERY browser has a default stylesheet.
There are a few main CSS attributes that influence layout:
- position
- display
- width and height
- padding
- border
margin
Everything else in CSS simply colors, changes fonts, font sizes, background images, etc.
All elements have a
position
, which is one of the four following values:static
relative
absolute
fixed
Any element with
static
orrelative
is considered having LAYOUT. This means they affect where other items are rendered. The otherposition
values create a new layer. If an element is either of these, it is rendered above (on a new, 3d layer) and may cover up text or elements beneath it.
All elements have a
display
, which is one of the following values:inline
inline-block
block
table
inline-table
table-cell
table-caption
table-column
table-row
flex
inline-flex
The most common
display
types areinline
,inline-block
, andblock
. We will only be focusing on these (for now).inline
elements flow with text. They typically have only text in them that gives them width and height. Typicalinline
elements:span
,strong
,em
,i
,b
. The defaultdisplay
type forimg
tags is alsoinline
.block
elements create vertical sections. Anything before them in the HTML is rendered on top, anything after them in the HTML is rendered beneath.inline-block
has the same layout properties ofinline
, however it can have padding, border, and margin.