Tribute page - margin doesn't work for <p> in this case

I was working on the Tribute page and found that the margin of p tag didn’t work in these cases:

Below css styling didn’t work:

header p,
section p,
footer p {
margin-top: 20px;
margin-bottom: 20px;
}

but this worked:

padding-top: 20px;
padding-bottom: 20px;

Regarding box model, it should have worked. I wonder why? Thank you.

Actually, in the above uploaded image, the margin styling didn’t work of h1 tag:

header h1 {
margin-top: 20px; // not works
}

but padding works well:
header h1 {
padding-top: 20px;
}

H1 & p elements are displayed as blocks by default, so they should have visible margin values. But in these cases, changing the margin values (eg. margin-top = 20px or 100px) doesn’t have any visible effect at all.

We need to see your complete raw code, a link to the question would be another great option.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="A tribute to Dr. Norman Borlaug">
    <link rel="stylesheet" href="styles.css">
    <title>A tribute to Dr. Norman Borlaug</title> 
  </head>
  <body>
    <div class="container">
      <header>
        <h1>Dr. Norman Borlaug</h1>
        <p>The man who saved a billion lives</p>
      </header>
      
      <main>
        <section class="photo">
          <img src="https://cdn.freecodecamp.org/testable-projects-fcc/images/tribute-page-main-image.jpg" alt="Dr. Norman Borlaugtrains biologists in Mexico on how to increase wheat yields">
          <p>Dr. Norman Borlaug, third from the left, trains biologists in Mexico on how to increase wheat yields - part of his life-long war on hunger.</p>
        </section>

        <section class="bio">
          <h2>Here's a time line of Dr. Borlaug's life:</h2>
          <ul>
            <li><span>1914</span> - Born in Cresco, Iowa</li>
            <li><span>1933</span> - Leaves his family's farm to attend the University of Minnesota, thanks to a Depression era program known as the "National Youth Administration"</li>
            <li><span>1935</span> - Has to stop school and save up more money. Works in the Civilian Conservation Corps, helping starving Americans. "I saw how food changed them", he said. "All of this left scars on me."</li>
            <li><span>1937</span> - Finishes university and takes a job in the US Forestry Service</li>
            <li><span>1938</span> - Marries wife of 69 years Margret Gibson. Gets laid off due to budget cuts. Inspired by Elvin Charles Stakman, he returns to school study under Stakman, who teaches him about breeding pest-resistent plants.</li>
            <li><span>1941</span> - Tries to enroll in the military after the Pearl Harbor attack, but is rejected. Instead, the military asked his lab to work on waterproof glue, DDT to control malaria, disinfectants, and other applied science.</li>
            <li><span>1942</span> - Receives a Ph.D. in Genetics and Plant Pathology</li>
            <li><span>1944</span> - Rejects a 100% salary increase from Dupont, leaves behind his pregnant wife, and flies to Mexico to head a new plant pathology program. Over the next 16 years, his team breeds 6,000 different strains of disease resistent wheat - including different varieties for each major climate on Earth.</li>
            <li><span>1945</span> - Discovers a way to grown wheat twice each season, doubling wheat yields</li>
            <li><span>1953</span> - crosses a short, sturdy dwarf breed of wheat with a high-yeidling American breed, creating a strain that responds well to fertilizer. It goes on to provide 95% of Mexico's wheat.</li>
            <li><span>1962</span> - Visits Delhi and brings his high-yielding strains of wheat to the Indian subcontinent in time to help mitigate mass starvation due to a rapidly expanding population</li>
            <li><span>1970</span> - receives the Nobel Peace Prize</li>
            <li><span>1983</span> - helps seven African countries dramatically increase their maize and sorghum yields</li>
            <li><span>1984</span> - becomes a distinguished professor at Texas A&M University</li>
            <li><span>2005</span> - states "we will have to double the world food supply by 2050." Argues that genetically modified crops are the only way we can meet the demand, as we run out of arable land. Says that GM crops are not inherently dangerous because "we've been genetically modifying plants and animals for a long time. Long before we called it science, people were selecting the best breeds."</li>
            <li><span>2009</span> - dies at the age of 95.</li>
          </ul>
        </section>
        <section class="testimonial">
           <p>"Borlaug's life and achievement are testimony to the far-reaching contribution that one man's towering intellect, persistence and scientific vision can make to human peace and progress."</p>
          <p>-- Indian Prime Minister Manmohan Singh</p>
        </section>
      </main>

      <footer>
        <p>If you have time, you should read more about this incredible human being on his <a href="https://en.wikipedia.org/wiki/Norman_Borlaug">Wikipedia entry</a>.</p>
      </footer>
    </div>
  <body>
</html>

styles.css

body {
  font-family: Tahoma, Arial, san-serif;
  font-size: 16px;
  line-height: 1.5rem;
  color: #333333;
}

.container {
  background-color: #EEEEEE;
  width: 99%;  
  margin: 30px auto;
}

header h1 {
  font-size: 35px;
  text-align: center;
  
  /* padding-top: 30px; */
  margin-top: 200px; 
  /* 
  What I want: adding a margin from h1 to the top of the header.
  What it does: adding a margin from h1 to the top of the page.  
  */
}


header p {  
  text-align: center;  
}


.photo {
  width: 98%;
  background-color: white;
  margin: 10px auto;

}

.photo img {
  display: block;
  margin: 30px auto 20px;
}

.photo p {
  text-align: center;
  /* padding-bottom: 20px; */
  margin-bottom: 100px; 
}

section h2 {
  font-size: 18px;
  font-weight: bold;
  text-align: center;
  padding: 40px;  
}


.bio {
  width: 50%;
  margin: auto;
}

.bio ul li {
  padding: 10px 0;
  
}


.bio span {
  font-weight: bold;
  
}

.testimonial {
  width: 50%;
  margin: 30px auto; 
  
}

.testimonial p {
  font-style: italic;
  padding-left: 20px;
}

footer {
  padding: 10px 0;
}

footer p {
  font-weight: bold;
  text-align: center;
  padding-bottom: 30px;  
  
}

footer a {
  color: #477CA7;
}

I seem to confuse the section, header, footer tags with the div tag, isn’t it?

Within a div container, the margin of h1 will be the space between itself and the border of the div container.

But it’s not so with section, header, footer?

The h1 margin-top is collapsing and going through the parent header container.

  1. If you add display: flow-root to the parent container the margin will not collapse.

  2. If you add padding to the parent container the margin will not collapse.

  3. If you use padding on the child it will push on the parent (padding does not collapse).

This CSS does work…

header p,
section p,
footer p {
  margin-top: 20px;
  margin-bottom: 20px;
}

…with a but. The margin-top on the p element has a lower value than the default margin-bottom on the h1. When two sibling elements have a top and bottom margin the one with the larger value will win. They do not combine into one final value.

As an example:

<h1>Heading</h1>
<p>Body text</p>
* {
  margin: 0;
}

h1 {
  margin-bottom: 20px
}

p {
  margin-top: 10px;
}

The space between the h1 and p elements is not going to be 30px but 20px which is the h1 margin-bottom (it is the larger of the two values).

This again is part of how margin and margin collapse works. If they had been using padding the space would be combined.

1 Like

So margins can have negative values, and now can be collapsing in case of adjacent siblings and empty containers like in this case (container has no border, padding, height…).

Got it now! Very clear explanation. Thank you, Lasjorg!

The html elements are very stricky sometimes. It’s great to learn to have more control of them!