InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS

    CSS selectors

    • CSS selectors

    CSS properties

    • CSS properties

    CSS cookbooks

    • !important annotation
    • Align div in center horizontally
    • Align div in center vertically
    • Align multiple divs horizontally
    • Align text in center horizontally
    • CSS - drop cap effect
    • CSS - enlarge image on hover
    • CSS - round toggle switch using checkbox and label
    • CSS automatic table row numbering
    • CSS circle and oval
    • CSS clearfix - clear float automatically using ::after
    • CSS counters
    • CSS nested counters
    • CSS3 gradients
    • Create CSS triangles
    • First letter capital and bigger
    • How to display text on image using css
    • Install sass on Mac or Linux using ruby gem
    • Place a div in bottom right corner of browser
    • Test site css, javascript, html in old IEs
    • csslint
    • inherit value
    • initial value
    • inline-block and baseline alignment
    • margin collapsing
    • node-sass quick start tutorial on Linux and Mac
    • unset value
    • web typography
     
    • Home
    • > Tutorials
    • > CSS

    CSS – margin collapsing

    on Feb 24, 2016

    In CSS adjoining vertical margins of two or more boxes (these may or may not be siblings), can combine to form a single margin. These boxes must be in-flow block level boxes.

    CSS margin collapsing example between siblings

    Bottom margin 50px (box1) + top margin 25px (box2) => collapsed margin 50px (higher)

    <style type="text/css" media="screen">
    body {margin:0;}
    .box1 {
      background:lightgreen;
      width:50px; height: 50px;
      margin-bottom: 50px;
    }
    .box2 {
      background:lightgray;
      width:50px; height:50px;
      margin-top: 25px;
    }
    </style>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
    refresh done
    try it online

    CSS margin collapsing example between parent and child

    This is somewhat counter intuitive can surprise people initially.
    Top margin 0 (parent) + top margin 25px (child) => collapsed margin 25px applied to both parent and child.

    <style type="text/css" media="screen">
    body {margin:0;}
    .parent {
      background:lightgreen;
      width:100px; height: 100px;
    }
    .child {
      background:lightgray;
      width:50px; height:50px;
      margin-top: 20px;
    }
    </style>
    <div class="parent">
    <div class="child">
    </div>
    </div>
    refresh done
    try it online

    How to avoid CSS margin collapsing example between parent and child

    We can add a padding-top of 1px to parent. This way margins will no longer be adjoining and won’t collapse.

    <style type="text/css" media="screen">
    body {margin:0;}
    .parent {
      background:lightgreen;
      width:100px; height: 100px;
      padding-top:1px;
    }
    .child {
      background:lightgray;
      width:50px; height:50px;
      margin-top: 20px;
    }
    </style>
    <div class="parent">
    <div class="child">
    </div>
    </div>
    refresh done
    try it online

    Related links

    • CSS 2 collapsing margin spec

    Suggested posts:

    1. Javascript – remove first item from array
    2. CSS :nth-of-type
    3. WordPress – write custom php log to separate file
    4. CSS selector specificity
    5. CSS :not – negation pseudo-class
    6. CSS – align multiple divs horizontally
    7. CSS :root
    8. jQuery – check if element is visible in viewport ofter scroll
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS cookbook, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress