CSS position: fixed 在 html 代码中重新放置后不显示 <nav>
CSS position: fixed doesn't show <nav> after re-placing it somewhere else in the html code
在尝试为移动版本构建响应式菜单时,我遇到了一个我无法理解的行为并寻求您的建议。
我附上了两个代码的链接,它们几乎相同。唯一的区别在于导航的位置,一旦用户单击 "open menu" 按钮,导航就会在 "Full Screen" (width: 100vw; height:100vh;)
上打开。
在第一个版本中,当用户单击 "Open Menu" 按钮时,菜单会清晰地打开,如您所见。我对第二个版本所做的更改只是将整个导航部分放在 HTML 代码中的主要属性中。
我不明白的是:为什么将导航放置在页面内部或页面上的其他任何位置?当用户单击 "open menu" 按钮时,#overley css 具有 position: fixed
和 top: 0px;
的属性含义,它应该固定在页面顶部,但在第二个版本,当单击菜单按钮时 - 没有任何反应,应该显示的 window 没有显示。我什至放置了 z-index
以确保它不是 "hidden" 或其他东西。
据我所知,position: fixed with top:0px 应该在最上面,而第二个版本并没有发生这种情况,原因我真的不明白,并且很高兴了解为什么它发生了。
First Version - Working (Menu Opens Up)
Second Version - Not Working (Nothing happens when clicking the menu button)
工作版本代码:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
<main>
<div style="width:100px; height:200px; overflow:hidden">
Text
</div>
</main>
</html>
第二版代码(菜单打不开的版本):
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
</body>
<main>
<div style="width:100px; height:200px; overflow:hidden">
Text
</div>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</main>
</html>
哦天啊天啊...好久没看到这么乱的代码了HTML,两个示例代码都很难看。我不确定如何解释,因为我不知道 CSS 在面对写得不好的 HTML 结构时会如何反应。始终记得确保您的元素正确嵌套!
我已经为你修复了代码,但说真的......我真的认为应该多学习一些关于正确编写基本 HTML 的知识,然后再尝试任何太花哨的东西,比如交互式按钮或(如问题标记)响应式设计。
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
input[type=checkbox]:checked + #overlay-button + main > #overlay {
visibility: visible;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
<main>
<div style="width:100px; height:200px; overflow:hidden">Text</div>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</main>
</body>
</html>
在尝试为移动版本构建响应式菜单时,我遇到了一个我无法理解的行为并寻求您的建议。
我附上了两个代码的链接,它们几乎相同。唯一的区别在于导航的位置,一旦用户单击 "open menu" 按钮,导航就会在 "Full Screen" (width: 100vw; height:100vh;)
上打开。
在第一个版本中,当用户单击 "Open Menu" 按钮时,菜单会清晰地打开,如您所见。我对第二个版本所做的更改只是将整个导航部分放在 HTML 代码中的主要属性中。
我不明白的是:为什么将导航放置在页面内部或页面上的其他任何位置?当用户单击 "open menu" 按钮时,#overley css 具有 position: fixed
和 top: 0px;
的属性含义,它应该固定在页面顶部,但在第二个版本,当单击菜单按钮时 - 没有任何反应,应该显示的 window 没有显示。我什至放置了 z-index
以确保它不是 "hidden" 或其他东西。
据我所知,position: fixed with top:0px 应该在最上面,而第二个版本并没有发生这种情况,原因我真的不明白,并且很高兴了解为什么它发生了。
First Version - Working (Menu Opens Up)
Second Version - Not Working (Nothing happens when clicking the menu button)
工作版本代码:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
<main>
<div style="width:100px; height:200px; overflow:hidden">
Text
</div>
</main>
</html>
第二版代码(菜单打不开的版本):
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
</body>
<main>
<div style="width:100px; height:200px; overflow:hidden">
Text
</div>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</main>
</html>
哦天啊天啊...好久没看到这么乱的代码了HTML,两个示例代码都很难看。我不确定如何解释,因为我不知道 CSS 在面对写得不好的 HTML 结构时会如何反应。始终记得确保您的元素正确嵌套!
我已经为你修复了代码,但说真的......我真的认为应该多学习一些关于正确编写基本 HTML 的知识,然后再尝试任何太花哨的东西,比如交互式按钮或(如问题标记)响应式设计。
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<style>
body {
background: #87cc77;
margin: 0;
font-family: arial;
width: 100vw;
height: 100vh;
animation: bugfix infinite 1s;
-webkit-animation: bugfix infinite 1s;
}
@keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
@-webkit-keyframes bugfix {
from {
padding: 0;
}
to {
padding: 0;
}
}
#overlay-button {
position: absolute;
right: 2em;
top: 3em;
padding: 26px 11px;
z-index: 5;
cursor: pointer;
user-select: none;
}
#overlay-button span {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: relative;
display: block;
transition: all .2s ease-in-out;
}
#overlay-button span:before {
top: -10px;
visibility: visible;
}
#overlay-button span:after {
top: 10px;
}
#overlay-button span:before, #overlay-button span:after {
height: 4px;
width: 35px;
border-radius: 2px;
background-color: white;
position: absolute;
content: "";
transition: all .2s ease-in-out;
}
#overlay-button:hover span, #overlay-button:hover span:before, #overlay-button:hover span:after {
background: #333332;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox]:checked ~ #overlay {
visibility: visible;
}
input[type=checkbox]:checked ~ #overlay-button:hover span, input[type=checkbox]:checked ~ #overlay-button span {
background: transparent;
}
input[type=checkbox]:checked ~ #overlay-button span:before {
transform: rotate(45deg) translate(7px, 7px);
opacity: 1;
}
input[type=checkbox]:checked ~ #overlay-button span:after {
transform: rotate(-45deg) translate(7px, -7px);
}
#overlay {
height: 100vh;
width: 100vw;
background: #ec6451;
z-index: 2;
visibility: hidden;
position: fixed;
top:0px;
}
#overlay.active {
visibility: visible;
}
#overlay ul {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
text-align: center;
height: 100vh;
padding-left: 0;
list-style-type: none;
}
#overlay ul li {
padding: 1em;
}
#overlay ul li a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
#overlay ul li a:hover {
color: #333332;
}
input[type=checkbox]:checked + #overlay-button + main > #overlay {
visibility: visible;
}
</style>
</head>
<body>
<input type="checkbox" id="overlay-input" />
<label for="overlay-input" id="overlay-button"><span></span></label>
<main>
<div style="width:100px; height:200px; overflow:hidden">Text</div>
<nav id="overlay">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</main>
</body>
</html>