<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;
}

/* Add padding to the parent */
.padding::before,
.padding::after {
  content: ' ';
  margin: -1px 0;
  display: block;
  height: 2px;
}

/* 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;
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.