Learn About CSS Margin Visualization

Learn CSS Berkeley

The first thing you might learn in a coding class or CSS tutorial would be the CSS Box Model. One component of the box model is the CSS margin, the area around the box that pushes elements outside of it and provides white space around the box so the elements on your webpage are not too close together. 

The CSS margin might seem fairly simple, especially if you’ve already learned about it through other CSS tutorials. However, today, we’ll look at a few aspects that might make it more difficult if you’re just getting started. 

Collapsing Margins 

In the early days of CSS, the coding language was used to format documents: when a document included a heading with a bottom margin followed by a paragraph with a top margin, there was no gap between them. This feature is still part of the language today, which means that a vertical CSS margin can collapse. This has been frustrating for many users who do not wish for this to occur. 

When your CSS margin collapses, the margins combine. The smaller margin gets placed inside of the larger margin, and the space remaining defaults to the larger of the two items. There are several instances that you might have a collapse, including: 

  • Siblings
  • Empty boxes
  • Parent with either last or first child

Adjacent Siblings

If two elements are displayed in a normal flow, then the first element’s bottom margin will collapse to the next element’s top margin. For example, if you have a few div elements, the first element might have a margin of 50 pixels, the next element might have a bottom and top CSS margin of 20 pixels and the last one might have a CSS margin of about 3 em. 

With CSS margin collapsing, the first margin would be 50 pixels, and the smaller margin would be absorbed by the larger one. However, your second margin would be 3 em because 3 em is larger than 20 pixels.

Empty Boxes 

With an empty box, the bottom and top margins may collapse. For example, if you have an empty object with its bottom and top margins set to 50 pixels, you would have a space of 50 pixels between them instead of 100. On the other hand, if something gets added to your box, the bottom and top margins would not collapse.

Parent With Either Last or First Child

This type of CSS margin collapse often catches people off guard because it’s not very intuitive. For example, let’s say you have the div that is classed as a wrapper. Its child elements might have a 50-pixel margin. However, because the last items and the first objects are flush, you will not have a 50-pixel margin between your wrapper and the element.

So, why does this happen? With a parent margin, the child’s CSS margin collapses, placing it outside of the parent. If you use DevTools to visualize your CSS margin and inspect the child, you can see what this looks like.

Keep in mind, if you are using a writing mode horizontally, you won’t see the right or left vertical margins collapsing. Only a block CSS margin will collapse.

Preventing CSS Margins From Collapsing

A CSS margin will not collapse if it is positioned absolutely or if it floats. One of the first things that you should do is place an item between the two elements, for example, a box without any content. The box won’t collapse if it has either padding or a border, so you could apply 2 pixels of padding to the box and prevent it from collapsing without affecting its appearance.

Remember, an empty box is invisible because the computer processes it as an empty element, but if you add something to the box, gaps will appear. A CSS margin that has children will also behave like this. Adding a border to the parent helps the children stay there as well.

You may know from other CSS tutorials that if there is a wrapping element that does not display visually, it’s best to leave it out to avoid large gaps from forming. While this process worked well when the internet was only text, now that we have designs and other elements on websites, it’s not as useful.

Working With Block Formatting 

A block formatting context (BFC) uses a containing element to prevent your CSS margin from collapsing by using a containing element. Consider the example using the parent and the children: they have their CSS margins outside their wrapper. If you give your wrapper a display of “flow-root”, this can create the new BFC, keeping the CSS margin inside.

Closing Thoughts

Before you continue learning from other tutorials or attend a coding class, let’s review what we’ve covered. Understanding why a CSS margin collapses can help you prevent future problems in your code. For example, some issues can be prevented by simply setting your CSS margin to go in a single direction. Generally, if you know what made your CSS margin collapse, you can determine the best course of action to fix it. It can often be difficult to pinpoint the exact cause, so making sure that your coding team understands how CSS margins work can be vital for helping them better deal with the issue.

*Please note, these articles are for educational purposes and the topics covered may not be representative of the curriculum covered in our boot camp. Explore our curriculum to see what you’ll learn in our program.

Get Program Info


Step 1 of 6

Ready to learn more about Berkeley Coding Boot Camp in San Francisco? Contact an admissions advisor at (510) 306-1218.