css - 页脚颜色颜色整个网页 - 如何解决?

标签 css html

<分区>

我在 Sublime text (mac) 中用 html 5 和 css 制作了一个测试网页,我反复检查了问题所在,但我无法解决问题。我希望橙色只为页脚(和页眉边框底部)着色,但它会填满整个网页(减去页眉)。

我该如何解决这个问题?

另外,我犯了什么错误?

ps - 我曾尝试为旁边和文章着色,但这不起作用,因为 2 的高度不同并且页脚仍然为尚未着色的所有内容着色(除了正文)。在 html 中添加分隔符 (br) 也不起作用,因为它会在页面顶部的颜色中添加分隔符

这是我的 html 代码:

    <!DOCTYPE html>
<html>
<head>
    <title>Task 3b</title>
    <link rel="stylesheet" type="text/css" href="../CSS/hwk.css">
</head>

<body>
    <header>
        <div class="container">
            <h1>
                <span id="highlight">Main</span> Header
            </h1>
            <nav>
                <p><a href="#">HOME</a> <a href="#">ABOUT</a> <a href="#">SERVICES</a></p>
            </nav>
        </div>
    </header>

    <article>
        <div class="container">
            <p class="maintext">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
        </div>
    </article>

    <aside>
        <div class="container">
            <p class="maintext">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            </p>
        </div>
    </aside>

    <footer>
        <div class="container">
            <p>
                -- &copy; 2019 | <a href="#">Contact Us</a><br>
                ----------
            </p>
        </div>
    </footer>
</body>
</html>

这是CSS:

.container{
    width: 80%;
    margin: auto;
    overflow: hidden;
}

a{
    text-decoration: none;
}

body{
    background-color: #f3f3f3;
    font-family: Arial;
    line-height: 1.5em;
    margin: 0;
    padding: 0;
}

/* header */

header{
    background: #35424a;
    margin: 0;
    padding: 20px;
    color: #fff;
    text-align: center;
    border-bottom: 3px #e8491d solid;
}

header h1{
    float: left;
}

header #highlight{
    color: #e8491d;
}

header nav{
    float: right;
    padding-top: 4px;
}

header nav a{
    color: #e2e2e2;
    font-weight: bold;
}

header nav a:hover{
    color: #fff;
}

/* text */

article{
    float: left;
    width: 65%;
}

aside{
    float: right;
    width: 35%;
    border-left: 1px #a8a8a8 solid;
    box-sizing: border-box;
}

footer{
    text-align: center;
    color: #fff;
    background-color: #e8491d;
}

Footer colour (orange) colours whole webpage

提前致谢!

最佳答案

您必须如下更改页脚CSS。只需添加 clear: both;

footer {
    clear: both;
    text-align: center;
    color: #fff;
    background-color: #e8491d;
}

.container{
    width: 80%;
    margin: auto;
    overflow: hidden;
}

a{
    text-decoration: none;
}

body{
    background-color: #f3f3f3;
    font-family: Arial;
    line-height: 1.5em;
    margin: 0;
    padding: 0;
}

/* header */

header{
    background: #35424a;
    margin: 0;
    padding: 20px;
    color: #fff;
    text-align: center;
    border-bottom: 3px #e8491d solid;
}

header h1{
    float: left;
}

header #highlight{
    color: #e8491d;
}

header nav{
    float: right;
    padding-top: 4px;
}

header nav a{
    color: #e2e2e2;
    font-weight: bold;
}

header nav a:hover{
    color: #fff;
}

/* text */

article{
    float: left;
    width: 65%;
}

aside{
    float: right;
    width: 35%;
    border-left: 1px #a8a8a8 solid;
    box-sizing: border-box;
}

footer {
    clear: both;			
    text-align: center;
    color: #fff;
    background-color: #e8491d;
}
<!DOCTYPE html>
<html>
<head>
    <title>Task 3b</title>
    <link rel="stylesheet" type="text/css" href="../CSS/hwk.css">
</head>

<body>
    <header>
        <div class="container">
            <h1>
                <span id="highlight">Main</span> Header
            </h1>
            <nav>
                <p><a href="#">HOME</a> <a href="#">ABOUT</a> <a href="#">SERVICES</a></p>
            </nav>
        </div>
    </header>

    <article>
        <div class="container">
            <p class="maintext">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
                cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
                proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </p>
        </div>
    </article>

    <aside>
        <div class="container">
            <p class="maintext">
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
                quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
                consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            </p>
        </div>
    </aside>

    <footer>
        <div class="container">
            <p>
                -- &copy; 2019 | <a href="#">Contact Us</a><br>
                ----------
            </p>
        </div>
    </footer>
</body>
</html>

有关 clear 属性的更多引用,请参阅此处 => What is a clearfix?

阅读 this article - by Chris Coyer @ CSS-Tricks

关于css - 页脚颜色颜色整个网页 - 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54715635/

上一篇:html - 小屏幕上的 Bootstrap 导航栏没有下拉菜单

下一篇:javascript - 平板设备上的 Web 应用程序只有灰色

相关文章:

php - Wordpress Woocommerce - 标题下方的可变产品价格

CSS background-color 适用于事件状态,但不适用于正常状态

javascript - 如何使用jquery获取变量的最小高度?

css - Selenium 单击具有相同类名的 CSS elmenents

css - 将页脚放在页面的最底部?

css - Twitter Bootstrap 3 导航栏

java - 如何在 Spring Web 应用程序中实现文件夹上传?

HTML 标签索引问题

jquery - 较新版本的 chrome 出现奇怪的 jquery/html 行为

html - 我如何获得一个 div 来包裹 float 的 child ?