0

目标是设置 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>
4

1 回答 1

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,以便包装器仅包含您的导航和主要元素。我将 home hero 移动到 main 元素中,这样它也会向右浮动。

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

另一件需要注意的事情是我删除float: left;了导航上的,因为position: absolute;它使它无关紧要。

于 2017-02-28T17:35:31.317 回答