Home > Mobile >  The margin does not collapse through the parent's padding
The margin does not collapse through the parent's padding

Time:05-19

I want to show <blockquote> element in a different color, so I added background-color CSS property. But unfortunately the colored box of the <blockquote> element envelops the text too tightly after I've done this change:

the color box is too tight

I added padding: 10px CSS property to the <blockquote> element for adding space around the text. But unfortunately the colored box is too big vertically after I've done this change:

the colored box is too big vertically

This happens because child <p> elements have margins:

It's because of margins of inner elements

Without the padding of the parent these margins collapse with other surrounding margins. But when the padding is added, the margins push off the padding adding extra space because they can't collapse through the padding.

How can I make margins of inner elements collapse through the parent's padding? If it is not possible, how can I remove extra space created by margins of inner elements?

CodePudding user response:

Remove padding (top and bottom one at least) and set overflow: hidden on the <blockquote>.

This is just one of the tricks to contain the margin. There are at least 5 other.

  • Related