html - 如何让主容器背景颜色环绕每个元素?

标签 html css

我正在从 freeCodeCamp 构建一个致敬页面,我正在尝试使用特定颜色 (#ECEAEA) 设计我的主要内容背景。但是,背景颜色并没有按要求覆盖所有主要内容区域。

我模拟了致敬页面,但背景颜色并未包裹所有内容。请参阅代码部分。

* {
    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>

我希望主要内容的背景色能覆盖整个内容。 enter image description here

最佳答案

您需要为您的#tribute-info 选择器删除 top 属性并添加带有 top 属性值的 margin-top,例如:

#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;
}

关于html - 如何让主容器背景颜色环绕每个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364142/

相关文章:

html - 按钮上的数字部分

javascript - 如何在 JavaScript 中从 html 引用 iframe

html - Bootstrap 4 样式选择

html - 如果我在左侧有一个可点击的 Logo ,我仍然可以使用粘性(固定)水平导航栏吗?

css - 如何删除 HTML 电子邮件中图像上方的空白区域?

jquery - 从ajax更新div内容时闪烁

javascript - 加载时显示输入文件对话框?

css - 为什么 Ant 设计子菜单标题具有蓝色?

javascript - 在 mouseover 和 mouseout 中显示 li 中的按钮

html - 进入移动 View 时扩展到父 div 之外的子元素