🛠️
Notes
  • README
  • ansible
    • Literal curly braces (raw)
  • bash
    • Case statement
    • Change quote style mid-string
    • Comparing versions
    • Hiding credentials on the command line
    • Directory of script
    • Using find to run a command on multiple files
    • Getting the line number of the nth match
    • Getopts
    • Parsing output with long lines using less
    • Print line at number
    • Remove final newline
    • Reading content between markers
    • Determine if a script was sourced or executed
    • Bash substring
    • Run a function on interrupt or error
    • Reference variable by name
  • chrome
    • Bypassing Chrome's NET::ERR_CERT_INVALID page
  • css
    • box-sizing: border-box
    • The currentColor keyword
    • Wrapper taking up at least 100% height
    • Using margin: auto; for centering
    • Margin-collapse
    • Which unit to use?
    • Hiding elements
  • git
    • Conditional git config includes
    • Viewing the evolution of a line or function
    • Name of current branch
    • Get the path to the repository root
    • Replaying a set of changes on a specific branch
    • Hide file from git diff output
    • Listing untracked files with git status
  • github
    • Setting up GitHub Actions
  • groovy
    • Appending items to a list
  • kubernetes
    • Waiting for a pod to be ready
  • make
    • Passing arguments to make rules
    • Running make in a set of subdirectories
  • npm
    • Update a value in a project's .npmrc
  • podman
    • Target last container
  • rust
    • The match operator
    • Unwrapping a Result
  • tmux
    • New window with prompt
  • tools
    • Running ngrok in the background
    • Using entr to react to file changes
  • typescript
    • Inferring the type of elements in an array
  • vim
    • The command-line window
    • Populate quickfix list with eslint errors
    • Visual increment
    • Opening a list of files in split windows
    • Insert line above matched line
    • Spelling
    • The tabular plugin
    • Populate quickfix list with tsc errors
  • yaml
    • Yaml multiline strings
  • zsh
    • Lazy loading command setup
Powered by GitBook
On this page
  • Only vertical margins collapse
  • Margins only collapse in Flow layout
  • Only adjacent elements collapse
  • The bigger margin wins
  • Nesting doesn't prevent collapsing
  • Margins can collapse in the same direction
  • More than two margins can collapse
  • Negative margins

Was this helpful?

  1. css

Margin-collapse

Margin-collapse is tricky.

Only vertical margins collapse

Horizontal margins between columns do not collapse.

Margins only collapse in Flow layout

If a container is using another layout, like flex, the margins of that container's children won't collapse.

Only adjacent elements collapse

Any additional element (be it an invisible and empty <br /> tag) will prevent margins from collapsing:

<p>First</p>
<br />
<p>Second</p>

The bigger margin wins

If elements specify different margins, the distance between them will be at least as large as the biggest number.

Nesting doesn't prevent collapsing

<style>
  p {
    margin-top: 20px;
    margin-bottom: 20px;
  }
</style>

<div>
  <p>First</p>
</div>
<p>Second</p>

Margin is meant to increase the distance between siblings. It is not meant to increase the gap between a child and its parent's bounding box; that's what padding is for.

The margin of the inner paragraph is transferred to the parent element. The effect would have been the same if the margin had been applied to the <div> directly instead of the <p>.

... unless the <div> had some bottom padding. That would block margin-collapse, as would a border. Margins have to be touching to collapse.

Margins can collapse in the same direction

A 0px margin is still a collapsible margin

<style>
  .blue {
    background-color: lightblue;
  }
  p {
    margin-top: 20px;
  }
</style>

<section class="blue">
  <p>Paragraph</p>
</section>

We might expect the blue background to extend above the text, but it does not. Again, the purpose of margin is not to increase the gap between a child and its parent's bounding box. The margin specified is transferred to the parent, overlapping with its 0 px margin.

More than two margins can collapse

With the largest one winning, as seen previously.

Negative margins

Negative margins can pull elements, making neighbours overlap.

PreviousUsing margin: auto; for centeringNextWhich unit to use?

Last updated 4 years ago

Was this helpful?