Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paragraph is not respecting margin of heading

Tags:

html

css

So I am trying to set up vertical rhythm on a page. But p is not respecting bottom margin of h1. Can someone explain me why?

Here is my code (em units would be better but for the sake of simplicity I have used pixels ):

html {
  font - size: 18px;
  line - height: 1.5;
}

h1 {
  font - size: 30px;
  line - height: 1.5;
  margin: 4.5px 0 4.5px;
}

p {
  margin: 27px 0;
}
<body>
  <h1>blaasdfasf</h1>
  <p>...</p>
</body>

Edit:

Here you can see what i mean:

h1 preview

p preview

The margin of h is ignored as you can se.

like image 957
David Novák Avatar asked May 31 '26 11:05

David Novák


1 Answers

It's called "Collapsing margin", and there are a lot of topic about it. Basically the larger margin will count, it's very common. All you can do is increased the larger margin or change you HTML elements. You might like to read:

  • http://www.sitepoint.com/web-foundations/collapsing-margins/
  • CSS Margins Overlap Problem
  • Margin doesn't work? Need space between two elements

A good solution is you create a container to the p elements into a div and put it in a padding-top property. With it, the content will have their own margin:

html {
  font-size: 18px;
  line-height: 1.5;
}

h1 {
  font-size: 30px;
  line-height: 1.5;
  margin: 4.5px 0 15px;
    overflow:auto;
}

#margin {
    padding-top:15px;
}
<h1>blaasdfasf</h1>
<div id="margin">
    <p>...</p>
</div>

Or simply apply the padding directly into the p element:

p {
    padding-top:15px;
}
like image 52
Rafael Almeida Avatar answered Jun 03 '26 00:06

Rafael Almeida



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!