Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lessmost/6ec49ced2f96a0c5b042 to your computer and use it in GitHub Desktop.
Save lessmost/6ec49ced2f96a0c5b042 to your computer and use it in GitHub Desktop.
Collapsing Margins: Parents and Children Comparison
<h1>Collapsing Child Margins</h1>
<div class="red">
<div class="orange">
<div class="yellow">
<div class="green">
<div class="blue">
<div class="purple">Collapsed</div>
</div>
</div>
</div>
</div>
</div>
<div class="padding red">
<div class="padding orange">
<div class="padding yellow">
<div class="padding green">
<div class="padding blue">
<div class="padding purple">Not Collapsed</div>
</div>
</div>
</div>
</div>
</div>
/* PRESENTATIONAL STYLES */
html {
font-size: 16px;
}
body {
background-color: #333;
color: #fff;
font-family: Helvetica;
padding: 25px;
text-align: center;
}
h1 {
font-size: 2em;
font-weight: 800;
}
/* DEMO STYLES */
/* Parent */
div {
margin: 15px 0;
}
/* Another way to disable Margin Collapse */
div {
/* This will create a new BFC for each div */
overflow: hidden;
}
/* Add padding to the parent */
.padding {
padding: 1px 0;
}
/* Here come the children */
.red {
background-color: #ff6b6b;
}
.orange {
background-color: #ff9e2c;
}
.yellow {
background-color: #eeee78;
}
.green {
background-color: #4ecd9d;
}
.blue {
background-color: #4e97cd;
}
.purple {
background-color: #6c4ecd;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment