how can I make the main container background color wrap around every element?

78
August 06, 2019, at 10:00 AM

am building a tribute page from freeCodeCamp and I am trying to style my main content background with a specific color (#ECEAEA). However, the background color is not covering all the main content area as desired.

I have mocked up the tribute page but the background color is not wrapping all content. See the code section.

* { 
    padding: 0; 
    margin: 0; 
    box-sizing: border-box; 
    line-height: 2rem; 
    /*background-color:white; */  
} 
 
/* Global Styles */ 
 
#main { 
    background-color: #ECEAEA; 
    width: 98%; 
    margin: 25px auto; 
    border-radius: 5px; 
    /*position: relative;   
    box-sizing: border-box; */ 
    background-size: cover; 
    min-height: 100%;  
} 
 
#title, #main-para { 
    text-align: center; 
    margin: 20px auto; 
    position: relative; 
    top: 70px; 
} 
 
#img-div { 
    width: 96%; 
    background-color:white;  
    margin: 0 auto; 
    border: 1px solid red;  
    position: relative;  
    top: 60px;  
    padding: 10px; 
    border-radius: 5px; 
} 
 
figure, figcaption { 
    text-align: center; 
    padding-top: 10px; 
} 
 
#tribute-info { 
    /*border: 1px solid green; */ 
    display: flex; 
    flex-flow: column wrap; 
    position: relative; 
    top: 120px; 
    align-items: center;  
    width: 60%; 
    margin: 0 auto; 
    box-sizing: border-box; 
    border: 1px solid green; 
     
} 
 
#cite-para { 
    line-height: 1.5em; 
    text-align: left; 
}   
 
blockquote { 
    font-style: italic; 
} 
 
#link-target { 
    font-size: 1.17em; 
}
<!DOCTYPE html> 
<html lang="en"> 
 
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="ie=edge"> 
    <title>FCC: Tribute Page</title> 
    <link rel="stylesheet" href="style.css"> 
</head> 
 
<body> 
    <main id="main"> 
        <h1 id="title">Dr Norman Bourlaug</h1> 
        <p id="main-para">The man who saved a billion lives</p> 
        <div id="img-div"> 
            <figure> 
                <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg" id="image" 
                    alt="Dr Norman Borlaug seen standing in Mexican wheat field with a group of biologists"> 
                <figcaption id="img-caption">Dr. Norman Bourlaug, third from the left, trains biologists in Mexico on 
                    how to 
                    increase wheat yield - part of his life-long war on hunger. 
                </figcaption> 
            </figure> 
        </div> 
        <section id="tribute-info"> 
            <h3>Here's a time line of Dr.Borlaug's life</h3> 
            <ul id="history"> 
                <li> 
                    <strong>1914</strong> - Born in Cresco Iowa 
                </li> 
                <li> 
                    <strong>1933</strong> - Leaves his family's farm to attend the University of Minnesota, thanks to a 
                    Depression era program known as the &quot;National Youth Administration&quot; 
                </li> 
                <li> 
                    <strong>1935</strong> - Has to stop school and save up more money. Works in the Civilian 
                    Conservation Corps, helping starving Americans. &quot;I saw how food changed them&quot;, he said. 
                    &quot;All of this 
                    left scars on me.&quot; 
                </li> 
                <li> 
                    <strong>1937</strong> - Finishes university and takes a job in the US Forestry Service 
                </li> 
                <li> 
                    <strong>1938</strong> - 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> 
                    <strong>1941</strong> - 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> 
                    <strong>1942</strong> - Receives a Ph.D. in Genetics and Plant Pathology 
                </li> 
                <li> 
                    <strong>1944</strong> - Rejects a 100&percnt; 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> 
                    <strong>1945</strong> - Discovers a way to grown wheat twice each season, doubling wheat yields 
                </li> 
                <li> 
                    <strong>1953</strong> - 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&percnt; of 
                    Mexico's 
                    wheat. 
                </li> 
                <li> 
                    <strong>1962</strong> - 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> 
                    <strong>1970</strong> - receives the Nobel Peace Prize 
                </li> 
                <li> 
                    <strong>1983</strong> - helps seven African countries dramatically increase their maize and sorghum 
                    yields 
                </li> 
                <li> 
                    <strong>1984</strong> - becomes a distinguished professor at Texas A&M University 
                </li> 
                <li> 
                    <strong>2005</strong> - states &quot;we will have to double the world food supply by 2050.&quot; 
                    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 &quot;we've been genetically 
                    modifying plants and animals for a long time. Long before we called it science, people were 
                    selecting the best breeds.&quot; 
                </li> 
                <li> 
                    <strong>2009</strong> - dies at the age of 95. 
                </li> 
            </ul> 
            <blockquote 
                cite="http://news.rediff.com/report/2009/sep/14/pm-pays-tribute-to-father-of-green-revolution-borlaug.htm"> 
                <p id="cite-para"> 
                    &quot;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.&quot; 
                </p> 
                <cite>-- Indian Prime Minister Manmohan Singh</cite> 
            </blockquote> 
        </section> 
        <h3 id="link-target">If you have time, you should read more about this incredible human being on his <a 
                href="https://en.wikipedia.org/wiki/Norman_Borlaug" target="_blank" id="tribute-link"> 
                Wikipedia entry</a></h3> 
 
    </main> 
</body> 
 
</html>

I expect the main content background color to cover the entire content.enter image description here

Answer 1

You should remove the "top" property from #tribute-info since it pushes the section out from the main...

Working snippet:

* { 
    padding: 0; 
    margin: 0; 
    box-sizing: border-box; 
    line-height: 2rem; 
    /*background-color:white; */  
} 
 
/* Global Styles */ 
 
#main { 
    background-color: #ECEAEA; 
    width: 98%; 
    margin: 25px auto; 
    border-radius: 5px; 
    /*position: relative;   
    box-sizing: border-box; */ 
    background-size: cover; 
    min-height: 100%;  
} 
 
#title, #main-para { 
    text-align: center; 
    margin: 20px auto; 
    position: relative; 
    top: 70px; 
} 
 
#img-div { 
    width: 96%; 
    background-color:white;  
    margin: 0 auto; 
    border: 1px solid red;  
    position: relative;  
    top: 60px;  
    padding: 10px; 
    border-radius: 5px; 
} 
 
figure, figcaption { 
    text-align: center; 
    padding-top: 10px; 
} 
 
#tribute-info { 
    /*border: 1px solid green; */ 
    display: flex; 
    flex-flow: column wrap; 
    position: relative; 
/*    top: 120px;*/ 
    align-items: center;  
    width: 60%; 
    margin: 0 auto; 
    box-sizing: border-box; 
    border: 1px solid green; 
     
} 
 
#cite-para { 
    line-height: 1.5em; 
    text-align: left; 
}   
 
blockquote { 
    font-style: italic; 
} 
 
#link-target { 
    font-size: 1.17em; 
}
    <main id="main"> 
        <h1 id="title">Dr Norman Bourlaug</h1> 
        <p id="main-para">The man who saved a billion lives</p> 
        <div id="img-div"> 
            <figure> 
                <img src="https://c2.staticflickr.com/4/3689/10613180113_fdf7bcd316_b.jpg" id="image" 
                    alt="Dr Norman Borlaug seen standing in Mexican wheat field with a group of biologists"> 
                <figcaption id="img-caption">Dr. Norman Bourlaug, third from the left, trains biologists in Mexico on 
                    how to 
                    increase wheat yield - part of his life-long war on hunger. 
                </figcaption> 
            </figure> 
        </div> 
        <section id="tribute-info"> 
            <h3>Here's a time line of Dr.Borlaug's life</h3> 
            <ul id="history"> 
                <li> 
                    <strong>1914</strong> - Born in Cresco Iowa 
                </li> 
                <li> 
                    <strong>1933</strong> - Leaves his family's farm to attend the University of Minnesota, thanks to a 
                    Depression era program known as the &quot;National Youth Administration&quot; 
                </li> 
                <li> 
                    <strong>1935</strong> - Has to stop school and save up more money. Works in the Civilian 
                    Conservation Corps, helping starving Americans. &quot;I saw how food changed them&quot;, he said. 
                    &quot;All of this 
                    left scars on me.&quot; 
                </li> 
                <li> 
                    <strong>1937</strong> - Finishes university and takes a job in the US Forestry Service 
                </li> 
                <li> 
                    <strong>1938</strong> - 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> 
                    <strong>1941</strong> - 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> 
                    <strong>1942</strong> - Receives a Ph.D. in Genetics and Plant Pathology 
                </li> 
                <li> 
                    <strong>1944</strong> - Rejects a 100&percnt; 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> 
                    <strong>1945</strong> - Discovers a way to grown wheat twice each season, doubling wheat yields 
                </li> 
                <li> 
                    <strong>1953</strong> - 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&percnt; of 
                    Mexico's 
                    wheat. 
                </li> 
                <li> 
                    <strong>1962</strong> - 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> 
                    <strong>1970</strong> - receives the Nobel Peace Prize 
                </li> 
                <li> 
                    <strong>1983</strong> - helps seven African countries dramatically increase their maize and sorghum 
                    yields 
                </li> 
                <li> 
                    <strong>1984</strong> - becomes a distinguished professor at Texas A&M University 
                </li> 
                <li> 
                    <strong>2005</strong> - states &quot;we will have to double the world food supply by 2050.&quot; 
                    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 &quot;we've been genetically 
                    modifying plants and animals for a long time. Long before we called it science, people were 
                    selecting the best breeds.&quot; 
                </li> 
                <li> 
                    <strong>2009</strong> - dies at the age of 95. 
                </li> 
            </ul> 
            <blockquote 
                cite="http://news.rediff.com/report/2009/sep/14/pm-pays-tribute-to-father-of-green-revolution-borlaug.htm"> 
                <p id="cite-para"> 
                    &quot;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.&quot; 
                </p> 
                <cite>-- Indian Prime Minister Manmohan Singh</cite> 
            </blockquote> 
        </section> 
        <h3 id="link-target">If you have time, you should read more about this incredible human being on his <a 
                href="https://en.wikipedia.org/wiki/Norman_Borlaug" target="_blank" id="tribute-link"> 
                Wikipedia entry</a></h3> 
 
    </main>

Answer 2

You're setting top property for both your #img-div and #tirbute-info and that pushes the content outside of your main container so you should use margin-top instead, like so:

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    line-height: 2rem;
    /*background-color:white; */ 
}
/* Global Styles */
#main {
    background-color: #ECEAEA;
    width: 98%;
    margin: 25px auto;
    border-radius: 5px;
    /*position: relative;  
    box-sizing: border-box; */
    background-size: cover;
    min-height: 100%; 
}
#title, #main-para {
    text-align: center;
    margin: 20px auto;
    position: relative;
    top: 70px;
}
#img-div {
    width: 96%;
    background-color:white; 
    margin: 0 auto;
    border: 1px solid red; 
    position: relative; 
    margin-top: 100px; 
    padding: 10px;
    border-radius: 5px;
}
figure, figcaption {
    text-align: center;
    padding-top: 10px;
}
#tribute-info {
    display: flex;
    flex-flow: column wrap;
    position: relative;
    margin-top: 120px;
    align-items: center; 
    width: 60%;
    margin: 120px auto 100px;
    box-sizing: border-box;
    border: 1px solid green;
}
#cite-para {
    line-height: 1.5em;
    text-align: left;
}  
blockquote {
    font-style: italic;
}
#link-target {
    font-size: 1.17em;
}

Also just to point out, you should use classes for your elements and not IDs everywhere. It's best to not use the IDs at all.

Answer 3

You need to delete top property for your #tribute-info selector and add margin-top with top property value instead, like:

#tribute-info {
    display: flex;
    flex-flow: column wrap;
    position: relative;
    align-items: center; 
    width: 60%;
    margin: 120px auto 0;
    box-sizing: border-box;
    border: 1px solid green;
}
READ ALSO
Solving alternative for $this-&gt;getMock and other php unit test class methods into Python&#39;s unittest.TestCase

Solving alternative for $this->getMock and other php unit test class methods into Python's unittest.TestCase

I am porting a library written in PHP, so far I managed to port the whole source code, but I stucked at the unit tests

89
Form auto calculate answer to php

Form auto calculate answer to php

I have an autocalc running on a form and the total is shown through a div IDI need to be able to somehow render the div ID answer into a php form mailer

57
RuntimeError: dictionary changed size during iteration while deleting the key from the dictionary

RuntimeError: dictionary changed size during iteration while deleting the key from the dictionary

I am trying to delete a key from the dictionaryWhile doing I got RuntimeError: dictionary changed size during iteration

120