Notes

CSS Layout Models

Edit on GitHub

CSS & Sass
2 minutes
  • The Box Model treats every element as if it were in a box. You can position these boxes on the page

  • A float (also referred to as a floated or floating box) is a box that is taken out of normal flow and moved to the far left or right of the containing box, yet still allows content to flow alongside it (although you can prevent text from flowing alongside it using the clear property).

  • You must specify the width for a floating box; otherwise, it will automatically take up the full width of the containing box.

  • Vertically, the floated box is aligned with the top of the containing box or the current line box

  • Sometimes you will want to float an element to the left or right, but not have any text wrap around the floated item. You can prevent anything from appearing next to a floated box using the clear property.

  • This property is used on any box that appears after the floated element, and indicates which sides may not touch the floated box. For example, giving the box following a float a clear property whose value is left means that the box cannot touch the left-hand side of the floated element

Box Model

  • Everything is a box.

  • There is content-box which is default and there is border-box. video: how the box gets it’s size

  • Content box accounts for the content. The size is determined by the content, and margins, paddings and borders would extrude out of that size

  • Border box calculates the set size as a sum of content+padding+border, and the margin than extrudes out.

  • In border-box, the border sets the size. Everything inside the border is going to be the space it takes, plus any margins

content-box = content
border-box = content + padding + border
1/* Content Box */
2.box {
3  width: 100px;
4  padding: 20px;
5  border: 1px solid black;
6  margin: 5px;
7  /* The actual size this box will take will not be 100px as you set the width to,
8  it'll be 152px (width + padding + border + margin)*/
9}
 1/* Border Box */
 2.box {
 3  box-sizing: border-box;
 4  width: 100px;
 5  padding: 20px;
 6  border: 1px solid black;
 7  margin: 5px;
 8  /* The actual size this box will take will not be 100px as you set the width to,
 9  it'll be 110px (width + margin) */
10}