Jump to content

A tricky question about margin


kayut

Recommended Posts

Hi,

 

Consider a web page with two adjacent paragraphs:

<p>  first paragraph</p><p>  second paragraph</p>

With this CSS code applied to a web page:

p {  background-color: red;  margin-top: 50px;  margin-bottom: 50px;}

What will be the distance between the two red rectangles displayed for the background color of these paragraphs?

 

Answer: I thought it should be 100px but NO, the distance between two paragraphs would be only 50px.

 

Can some one please explain WHY??

 

Tahnks

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

I just want to understand it.

I mean if the first <p> element 50px margin-bottom and the second <p> element 50px margin-top has then the distance between two paragraphs should be 100px. Why is it still 50px ????

Link to comment
Share on other sites

It's something called collapsing margins. When two elements' margins are adjacent, the larger of the two margins is used while the other is ignored.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...