The Box Model
The Box Model
We could consider all the elements in the website a box. Yes, they are rectangular boxes, nesting in an other box, and lying beside an other box.
Here we use padding
, border
, and margin
to manipulate the size of these boxes, and the space between them.
padding
increases the space between the border of a box and the content of the box.border
adds space (even if it’s only a pixel or two) between the margin and the paddingmargin
increases the space between the borders of a box and the borders of adjacent (/əˈdʒeɪs(ə)nt/ adj.相邻的) boxes.
Be sure to study the diagrams carefully:
Block vs Inline
CSS has two box types: block
and inline
boxes, which determine element behavior and interaction. The display
property controls how HTML elements appear on the webpage.
block boxes:
By default, block elements will appear on the pave stacked atop each other, each new element starting on a new line.inline boxes:
Inline elements, however, do not start a new line. Additionally, padding and margin behave differently on inline elements. In general, you do not want to try to put extra padding or margin on inline elements.
The middle ground inline-block
Inline-block elements behave like inline elements, but with block-style padding and margin. display: inline-block
is a useful tool to know about.
Divs and spans
Divs and spans are special as they give no particular meaning to their content. They are just generic boxes that contains anything.
The normal fow
The normal flow means the default layout of an HTML file when it is rendered by a browser without any CSS code to change the style and position of it.
See this:
1 | <h1>Basic document flow</h1> |
1 | body { |
The difference between inline an block-inline
Compared to display: inline, the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline, top and bottom margins & paddings are not respected, and with display: inline-block they are.
Here we could see the common block-level elements in HTML:
w3school