Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:28:48+00:00 2026-06-11T15:28:48+00:00

I have been running some tests to find out why some CSS margins collapse

  • 0

I have been running some tests to find out why some CSS margins collapse and why some don’t. I have the following test code:

<div id="seconddiv" style="margin-top:10px; margin-bottom:10px;">
    <p style="height:200px; margin-top:5px; margin-bottom:5px;">This is the first paragraph in  the second div!This paragraph is 200px tall.</p>
    <p style="height:300px; margin-top:5px; margin-bottom:5px;">This is the second paragraph in the second div!This paragraph is 300 px tall.</p>
    <p style="height:400px; margin-top:5px; margin-bottom:5px;">This is the third paragraph in the second div!This paragraph is 400px tall.</p>
</div>

I am trying to accurately get the height of the div, but scrollHeight returns “910px”. Why is that? I expected “900px” as the scrollHeight, as it does not include margins.

Did some of the <p> margins collapse and get counted in the height? Why some and not others. I tried many different combinations of margin heights and no values reveal what is going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:28:49+00:00Added an answer on June 11, 2026 at 3:28 pm

    I do not think you’re understanding what “margin-collapse” means.

    I’m going to use this simplified markup in the following examples:

    HTML:

    <div>
       <span>A</span>
       <span>B</span>
       <span>C</span>
    </div>
    

    CSS:

    span {
        display: block;
        height: 100px;
        margin: 5px 0;
    }
    

    Instead of displaying each margin as separate spacing, top and bottom margins on elements will merge with their sibling (or if no prev/next sibling exists, their parent*) elements so that the spacing between is the largest margin.

    If there were no margin collapse, the above markup would appear as:

    +div-----+
    | margin |
    |+span--+|
    ||A     ||
    |+------+|
    | margin |
    | margin |
    |+span--+|
    ||B     ||
    |+------+|
    | margin |
    | margin |
    |+span--+|
    ||C     ||
    |+------+|
    | margin |
    +--------+
    

    With margin-collapse, the markup is displaying as:

      margin
    +div-----+
    |+span--+|
    ||A     ||
    |+------+|
    | margin |
    |+span--+|
    ||B     ||
    |+------+|
    | margin |
    |+span--+|
    ||C     ||
    |+------+|
    +--------+
      margin
    

    What this means for the height of the div is that it includes the height of each of its elements and the margins between them.

    In my example the height is 100 + 5 + 100 + 5 + 100 = 310.

    In your example the height is 200 + 5 + 300 + 5 + 400 = 910.


    * There is some additional complexity involving padding, positioning and floating, which is described by the specification.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using this code, but have been running into some memory issues:
I have some code which is been running by a backgroundworker I'd like some
I am reading up on subrepos, and have been running some tests locally, seems
I have some code like the following that I'm running in a debugger in
I've been running some tests to see if I can find an efficient selector
I have been having some problems with the test debugger in VS10. All tests
I have been trying to get the code coverage of some test cases I
I have been trying to learn Erlang and have been running into some problems
I have a running website on wordpress which has been hacked, some file were
I have an issue running some moles tests within Visual Studio 2010. Within a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.