Box Model
Box Model
In CSS we have several types of boxes that generally fit into the categories of block
boxes and inline boxes. The type refers to how the box behaves in terms of page flow
and in relationship to other boxes on the page. Boxes have an inner display type and
an outer display type.
Some HTML elements, such as <h1> and <p>, use block as their outer display type by
default.
HTML elements, such as <a>, <span>, <em> and <strong> use inline as their outer
display type by default.
Block and inline layout is the default way things behave on the web. By default and
without any other instruction, the elements inside a box are also laid out in normal flow
and behave as block or inline boxes.
You can change the inner display type for example by setting display: flex;.
- This element will still use the outer display type block but this changes the inner
display type to flex.
- Any direct children of the box will become flex items.
To turn on the alternative model for an element, set box-sizing: border-box on it:
.box {
box-sizing: border-box;
}
.box {
width: 350px;
inline-size: 350px;
height: 150px;
block-size: 150px;
margin: 10px;
padding: 25px;
border: 5px solid black;
}
Now, the actual space taken up by the box will be 350px in the inline direction and
150px in the block direction
To use the alternative box model for all of your elements (which is a common choice
among developers), set the box-sizing property on the <html> element and set all other
elements to inherit that value:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
Margin
The margin is an invisible space around your box. It pushes other elements away from
the box. Margins can have positive/negative values. Setting a negative margin on one
side of your box can cause it to overlap other things on the page. Whether you are
using the standard or alternative box model, the margin is always added after the size of
the visible box has been calculated.
We can control all margins of an element at once using the margin property, or each
side individually using the equivalent longhand properties:
- Margin-top
- Margin-right
- Margin-bottom
- Margin-left
Margin Collapsing
Depending on whether two elements whose margins touch have positive or negative
margins, the results will be different:
- Two positive margins will combine to become one margin. Its size will be equal to
the largest individual margin.
- Two negative margins will collapse and the smallest (furthest) from zero value
will be used.
- If one margin is negative, its value will be subtracted from the total.
Borders
The border is drawn between the margin and the padding of a box. If you are using the
standard box model, the size of the border is added to the width and height of the
content box. If you are using the alternative box model then the size of the border
makes the content box smaller as it takes up some of that available width and height of
the element box.
For styling borders, there are a large number of properties — there are four borders,
and each border has a style, width, and color that we might want to manipulate.
You can set the width, style, or color of all four borders at once using the border
property.
● border-top
● border-right
● border-bottom
● border-left
● border-width
● border-style
● border-color
To set the width, style, or color of a single side, use one of the more granular longhand
properties:
● border-top-width
● border-top-style
● border-top-color
● border-right-width
● border-right-style
● border-right-color
● border-bottom-width
● border-bottom-style
● border-bottom-color
● border-left-width
● border-left-style
● border-left-color
Padding
The padding sits between the border and the content area and is used to push the
content away from the border. Unlike margins, you cannot have a negative padding.
Any background applied to your element will display behind the padding.
The padding property controls the padding on all sides of an element. To control each
side individually, use these longhand properties:
- padding-top
- padding-right
- padding-bottom
- padding-left
Where this can be useful is when you want to give a link a larger hit area by adding
padding. <a> is an inline element like <span>; you can use display:
inline-block to allow padding to be set on it, making it easier for a user to click the
link.
You see this fairly frequently in navigation bars. The navigation below is displayed in a
row using flexbox and we have added padding to the <a> element as we want to be able
to change the background-color when the <a> is hovered. The padding appears to
overlap the border on the <ul> element. This is because the <a> is an inline element.
.box {
margin: <margin-top> || <margin-right> || <margin-bottom> ||
<margin-left>
}</margin-left></margin-bottom></margin-right></margin-top>
It should also be pointed out that auto is useful only for horizontal centering, and so
using auto for top and bottom margins will not center an element vertically, which can be
confusing for beginners.
display: inline-block brought a new way to create side by side boxes that collapse and
wrap properly depending on the available space in the containing element. It makes
layouts that were previously accomplished with floats easier to create. No need to clear
floats anymore.
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.
Now, the difference between display: inline-block and display: block is that, with display:
block, a line break happens after the element, so a block element doesn’t sit next to
other elements. Here are some visual examples: