2列CSS布局,但导航栏不会扩展到页面底部

时间:2017-02-28 16:54:49

标签: html css two-column-layout

目标是设置2列CSS布局。课堂上的教科书让它听起来就像编辑CSS Nav一样容易,让它向左浮动。但是,它不会像示例所示那样将Nav列一直扩展到页面底部,但它确实浮动在左上角。

CSS:

    body{
    background-color: #ffffff;
    color: #666666;
    font: sans-serif;
    background-image: url(background.jpg);
}

header{
    background-color: #000033;
    color: #ffffff;
    font: sans-serif;
}

nav{
    float: left;
    width: 90px;
    position: absolute;
    background-color: #90c7e3;
    text-decoration: none;
    font-weight: bold;
    padding: 5px;
}

main{
    display: block;
}
h1{
    line-height: 200%;
    background-image: url(sunset.jpg);
    background-repeat: no-repeat;
    background-position: right;
    padding-left: 20px;
    height: 72 px;
    margin-bottom: 0;
}

h2
{
    color: #3399cc;
    font: serif;
}

h3
{
    color: #000033;
}

dt
{
    color: #000033;
    font-weight: bold;
}

    #resort
{
    color: #000033;
    font-size: 1.2em;
}

    #footer
{
    margin-left: 100px;
    padding: 10px;
    font-size: .7em;
    font-style: italic;
    text-align: center;
}

    #wrapper
{
    background-color: #ffffff;
    min-width: 700px;
    max-width: 1024px;
    margin-left: auto;
    margin-right: auto;
    width: 80%;
    box-shadow: 5px 5px 5px #000033;
}

    #homehero
{
    height: 300px;
    background-image: url(coast.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

    #yurthero
{
    height: 300px;
    background-image: url(yurt.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

    #trailhero
{
    height: 300px;
    background-image: url(trail.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

HTML:

<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="pacific.css">
    <title>Pacific Trails Resort</title>
    <meta charset="UTF-8">
</head>
<body>
<div id="wrapper">
<header>
    <h1>Pacific Trails Resort</h1>
</header>

<nav>
    <a href="index.html">Home</a> &nbsp; &nbsp; &nbsp;
    <a href="yurts.html">Yurts</a> &nbsp; &nbsp; &nbsp;
    <a href="activities.html">Activities</a> &nbsp; &nbsp; &nbsp;
    <a href="reservations.html">Reservations</a>
</nav>

<div id="homehero">
</div>

<main>
    <h2>Enjoy Nature in Luxury</h2>
    <p><span class="resort">Pacific Trails Resort</span> offers a special     lodging experience on the California North Coast. Relax in serenity with     panoramic views of the Pacific Ocean.</p>

<ul>
    <li>Private yurts with decks overlooking the ocean</li>
    <li>Activities lodge with fireplace and gift shop</li>
    <li>Nightly fine dining at the Overlook Cafe</li>
    <li>Heated outdoor pool and whirlpool</li>
    <li>Guided hiking tours of the redwoods</li>
</ul>
    <div>
        <span class="resort">Pacific Trails Resort</span><br>
        12010 Pacific Trails Road<br>
        Zephyr, CA 95555<br>
        888-555-5555<br>
    </div>
</main>
<footer>
    <div id="footer">
    Copyright &copy; 2016 Pacific Trails Resort<br>
    </div>
</footer>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

正如我在评论中指出的那样,您可以在主元素上添加浮点数,然后为导航设置一个高度。您还需要限制主元素的宽度,使其适合导航。

查看此代码段以查看:

body{
    background-color: #ffffff;
    color: #666666;
    font: sans-serif;
    background-image: url(background.jpg);
    position: relative;
}

header{
    background-color: #000033;
    color: #ffffff;
    font: sans-serif;
}

nav{
    width: 90px;
    position: absolute;
    background-color: #90c7e3;
    text-decoration: none;
    font-weight: bold;
    padding: 5px;
    height: 100%;
}

main{
    display: block;
    float: right;
    width: 80%;
}
h1{
    line-height: 200%;
    background-image: url(sunset.jpg);
    background-repeat: no-repeat;
    background-position: right;
    padding-left: 20px;
    height: 72 px;
    margin-bottom: 0;
}

h2
{
    color: #3399cc;
    font: serif;
}

h3
{
    color: #000033;
}

dt
{
    color: #000033;
    font-weight: bold;
}

    #resort
{
    color: #000033;
    font-size: 1.2em;
}

    #footer
{
    margin-left: 100px;
    padding: 10px;
    font-size: .7em;
    font-style: italic;
    text-align: center;
}

    #wrapper
{
    background-color: #ffffff;
    min-width: 700px;
    max-width: 1024px;
    margin-left: auto;
    margin-right: auto;
    width: 80%;
    box-shadow: 5px 5px 5px #000033;
}

    #homehero
{
    height: 300px;
    background-image: url(coast.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

    #yurthero
{
    height: 300px;
    background-image: url(yurt.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}

    #trailhero
{
    height: 300px;
    background-image: url(trail.jpg);
    background-size: 100% 100%;
    background-repeat: no-repeat;
}
<!doctype html>
<html lang="en">
<head>
    <link rel="stylesheet" type="text/css" href="pacific.css">
    <title>Pacific Trails Resort</title>
    <meta charset="UTF-8">
</head>
<body>
<header>
    <h1>Pacific Trails Resort</h1>
</header>

<div id="wrapper">
<nav>
    <a href="index.html">Home</a> &nbsp; &nbsp; &nbsp;
    <a href="yurts.html">Yurts</a> &nbsp; &nbsp; &nbsp;
    <a href="activities.html">Activities</a> &nbsp; &nbsp; &nbsp;
    <a href="reservations.html">Reservations</a>
</nav>

<main>
    <div id="homehero">
</div>
    <h2>Enjoy Nature in Luxury</h2>
    <p><span class="resort">Pacific Trails Resort</span> offers a special     lodging experience on the California North Coast. Relax in serenity with     panoramic views of the Pacific Ocean.</p>

<ul>
    <li>Private yurts with decks overlooking the ocean</li>
    <li>Activities lodge with fireplace and gift shop</li>
    <li>Nightly fine dining at the Overlook Cafe</li>
    <li>Heated outdoor pool and whirlpool</li>
    <li>Guided hiking tours of the redwoods</li>
</ul>
    <div>
        <span class="resort">Pacific Trails Resort</span><br>
        12010 Pacific Trails Road<br>
        Zephyr, CA 95555<br>
        888-555-5555<br>
    </div>
</main>
</div>
<footer>
    <div id="footer">
    Copyright &copy; 2016 Pacific Trails Resort<br>
    </div>
</footer>
</body>
</html>

我注意到你在导航上有一个绝对位置。我跑了,然后把它放在身体上。我还将页眉和页脚从包装div中移出,这样包装器只包含nav和main元素。我将家庭英雄移动到主要元素中,以便它也可以向右浮动。

通过仅在包装器中包装main和nav元素,我们可以将height: 100%;添加到nav元素以获取包装器的完整高度。如果我们将页眉和页脚保留在包装器中,导航仍然是包装器高度的100%,但现在它太大了,因为它不会折叠页眉和页脚的高度。

另外需要注意的是,我删除了导航栏上的float: left;,因为position: absolute;使其无关紧要。