gpt4 book ai didi

html - 为什么我的 'position: fixed' 导航栏不会保留在文档的中心?

转载 作者:行者123 更新时间:2023-11-28 01:12:49 24 4
gpt4 key购买 nike

我正在 freeCodeCamp 上做一个关于产品放置页面的元素。该页面现在要求我修复视口(viewport)顶部的 #navbar。虽然这适用于 position: fixed,但在我在 CSS 中添加固定位置后,导航栏不再保持在中心并向左移动。如果你删除 position: fixed;,你就会明白我的意思了。为什么会这样?

在添加固定位置之前,导航栏通过 margin-left: center;margin-right: center; 居中(英式拼写 ;-})。我的视口(viewport)也有 800 像素宽和自动高度(即 BBC 风格)。

重要说明:我创建了一个 Logo ,随后将其添加到文档中,但这似乎没有显示(它由 tinypic 托管)。如果您看不到此 Logo ,它位于标题和视频之间。

附加信息:在我进行更改之前,我的视频居中。如果你也能帮助我,我将不胜感激。但这不是这个问题的重点。

#mainbody {
width: 800px;
height: auto;
margin-right: auto;
margin-left: auto;
font-family: garamond, serif;
}

#h1title {
text-align: center;
text-shadow: 16px 8px 16px;
font-size: 38px;
padding-top: 10px;
}

#header-img {
display: block;
width: auto;
height: auto;
margin-left: auto;
margin-right: auto;
}

#nav-bar {
text-align: center;
position: fixed;
right: 0;
left: 0;
}

.nav-link {
text-decoration: none;
color: black;
font-weight: 900;
}

.nav-link:hover {
color: black;
}

.nav-button {
-webkit-border-radius: 50%;
background-color: orange;
height: auto;
width: 100px;
transition: opacity 1s linear;
}

.nav-button:hover {
background-color: white;
opacity: 0.30;
}

#video {
display: block;
margin-right: auto;
margin-left: auto;
}

#form {

}

legend {
font-weight: 700;
}

#submit {
margin-top: 5px;
}
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<title>Quality Prints</title>
</head>
<body id="mainbody">
<a name="home-link"></a>
<header id="header">
<nav id="nav-bar">
<button class="nav-button"><a class="nav-link" href="#home-link">Home</a></button>
<button class="nav-button"><a class="nav-link" href="#">Products</a></button>
<button class="nav-button"><a class="nav-link" href="#contact-link">Contact</a></button>
</nav>
<h1 id="h1title">Quality Prints ™</h1><hr />
<img id="header-img" src="http://i66.tinypic.com/157ltle.jpg">

</header>
<iframe id="video" width="560" height="315" src="https://www.youtube.com/embed/CqtLKw7cJaY?rel=0&amp;showinfo=0&amp;start=5" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br />
<form action="https://www.freecodecamp.com/email-submit" id="form">
<a name="contact-link"></a>
<fieldset>
<legend>Information Pack by Email</legend>
<label for="emailme">Email Address:</label><br />
<input name="emailme" type="email" id="emailmeinput" placeholder="Your email address"><br />
<input type="submit" value="Send" id="submit" formaction="https://www.freecodecamp.com/email-submit">
</fieldset>
</form>
</body>
</html>

我已经编辑以更正语法。正如 Andriy 所说,'.nav-button:hover' 之后缺少一个 '}'。我还添加了“左:0;”和“右:0;”到“#nav-bar”。

最佳答案

尝试添加零值的左右规则:

#nav-bar {
text-align: center;
position: fixed;
right: 0;
left: 0;
}

#mainbody {
width: 800px;
height: auto;
margin-right: auto;
margin-left: auto;
font-family: garamond, serif;
}

#h1title {
text-align: center;
text-shadow: 16px 8px 16px;
font-size: 38px;
padding-top: 10px;
}

#header-img {
display: block;
width: auto;
height: auto;
margin-left: auto;
margin-right: auto;
}

#nav-bar {
text-align: center;
position: fixed;
right: 0;
left: 0;
}

.nav-link {
text-decoration: none;
color: black;
font-weight: 900;
}

.nav-link:hover {
color: black;
}

.nav-button {
-webkit-border-radius: 50%;
background-color: orange;
height: auto;
width: 100px;
transition: opacity 1s linear;
}

.nav-button:hover {
background-color: white;
opacity: 0.30;
}

#video {
display: block;
margin-right: auto;
margin-left: auto;
}

#form {

}

legend {
font-weight: 700;
}

#submit {
margin-top: 5px;
}
<!DOCTYPE html>
<html lang="en-GB">
<head>
<meta charset="utf-8">
<title>Quality Prints</title>
</head>
<body id="mainbody">
<a name="home-link"></a>
<header id="header">
<nav id="nav-bar">
<button class="nav-button"><a class="nav-link" href="#home-link">Home</a></button>
<button class="nav-button"><a class="nav-link" href="#">Products</a></button>
<button class="nav-button"><a class="nav-link" href="#contact-link">Contact</a></button>
</nav>
<h1 id="h1title">Quality Prints ™</h1><hr />
<img id="header-img" src="http://i66.tinypic.com/157ltle.jpg">

</header>
<iframe id="video" width="560" height="315" src="https://www.youtube.com/embed/CqtLKw7cJaY?rel=0&amp;showinfo=0&amp;start=5" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe><br />
<form action="https://www.freecodecamp.com/email-submit" id="form">
<a name="contact-link"></a>
<fieldset>
<legend>Information Pack by Email</legend>
<label for="emailme">Email Address:</label><br />
<input name="emailme" type="email" id="emailmeinput" placeholder="Your email address"><br />
<input type="submit" value="Send" id="submit" formaction="https://www.freecodecamp.com/email-submit">
</fieldset>
</form>
</body>
</html>

关于html - 为什么我的 'position: fixed' 导航栏不会保留在文档的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52019600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com