gpt4 book ai didi

html - 2 列 CSS 布局,但导航栏不会延伸到页面底部

转载 作者:行者123 更新时间:2023-11-28 04:15:16 25 4
gpt4 key购买 nike

目标是设置一个 2 列的 CSS 布局。类里面的教科书使它听起来像通过向左浮动来编辑 CSS Nav 一样简单。但是,它不会像示例所示那样将 Nav 列一直延伸到页面底部,而是 float 在左上角。

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>

最佳答案

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

查看此代码段以了解这一点:

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>

我注意到您在导航中有一个绝对位置。我用它运行并将它相对于 body 定位。我还将页眉和页脚从包装 div 中移出,以便包装仅包含您的导航和主要元素。我将 home hero 移到了主要元素中,这样它也会向右浮动。

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

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

关于html - 2 列 CSS 布局,但导航栏不会延伸到页面底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42514491/

25 4 0