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:staticrelativeabsolutefixedAny element with
staticorrelativeis considered having LAYOUT. This means they affect where other items are rendered. The otherpositionvalues 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:inlineinline-blockblocktableinline-tabletable-celltable-captiontable-columntable-rowflexinline-flexThe most common
displaytypes areinline,inline-block, andblock. We will only be focusing on these (for now).inlineelements flow with text. They typically have only text in them that gives them width and height. Typicalinlineelements:span,strong,em,i,b. The defaultdisplaytype forimgtags is alsoinline.blockelements create vertical sections. Anything before them in the HTML is rendered on top, anything after them in the HTML is rendered beneath.inline-blockhas the same layout properties ofinline, however it can have padding, border, and margin.