我最近做了一个简单的导航栏,它不适合我的屏幕。我想让它正确浮动。这里的问题是navbarList在屏幕外。它需要是position: fixed,否则它会移动下面的元素。我真的需要尽快解决这个问题。
小提琴:https://jsfiddle.net/zen41w7h/
<div class="top">
<div class="navbarOuter">
<div class="hamborgor">≡</div>
<div class="hamborgor2">×</div>
<div class="navbarInner">
<ul class="navbarList">
<li><a href="Sub1.html" class="navbarButton">Sub1</a></li>
<li><a href="Sub2.html" class="navbarButton">Sub2</a></li>
<li><a href="Sub3.html" class="navbarButton">Sub3</a></li>
</ul>
</div>
</div>
</div>
CSS:
@import url('https://fonts.googleapis.com/css2?family=Abril+Fatface&family=Cairo:wght@200;300;400;600;700;900&family=Dancing+Script:wght@400;500;600;700&family=Fraunces:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Lobster&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
li {
list-style-type: none;
}
* {
font-family: Montserrat;
margin: 0;
}
a {
color: #fff;
}
a:hover {
color: #555;
}
.top {
background-color: #dddddd;
}
.navbarOuter {
float: right;
width: 40px;
height: 40px;
background-color: aqua;
position: relative;
display: block;
transition: all 1s linear;
}
.hamborgor2 {
font-size:55px;
line-height: 43px;
width: 40px;
height: 40px;
text-align: center;
position: absolute;
display: none;
}
.navbarOuter:hover .hamborgor2 {
display: block;
}
.navbarOuter:hover .hamborgor {
display: none;
}
.hamborgor {
width: 40px;
height: 40px;
font-size:40px ;
text-align: center;
line-height: 40px;
cursor: default;
position: absolute;
}
.navbarInner {
display: none;
}
.navbarList {
width: 100px;
height: 25px;
padding: 40px 0px 0px 0px;
position: fixed;
}
.navbarButton {
background-color: palegreen;
text-decoration: none;
line-height: 25px;
display: block;
width:100px;
padding:12px 5px ;
}
.navbarOuter:hover .navbarInner {
display:block;
}
我试图理解你在做什么,但从我看到的…不,您不需要position: fixed
....把它去掉,给navbarInner添加absolute
.navbarInner {
display: none;
position: absolute;
right: 10px;
}
你到处使用太多固定位置和固定高度和宽度…只要学会在子元素中尽可能多地使用百分比……无论如何,我只是把navBarInner从navBarOuter中取出,因为它最初会影响它。然后删除一堆固定的高度和宽度,并把navBarInner固定位置右:0;您可以在
下面看到完整的演示
@import url('https://fonts.googleapis.com/css2?family=Abril+Fatface&family=Cairo:wght@200;300;400;600;700;900&family=Dancing+Script:wght@400;500;600;700&family=Fraunces:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Lobster&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&family=Raleway:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
li {
list-style-type: none;
}
* {
font-family: sans-serif;
margin: 0;
}
a {
color: #fff;
}
a:hover {
color: #555;
}
.top {
background-color: #dddddd;
}
.navbarOuter {
float: right;
width: 40px;
height: 40px;
background-color: aqua;
position: relative;
display: block;
transition: all 1s linear;
cursor: pointer;
}
.hamborgor {
width: 40px;
height: 40px;
font-size: 40px;
text-align: center;
line-height: 40px;
cursor: default;
position: absolute;
}
.hamborgor2 {
font-size: 55px;
line-height: 43px;
width: 40px;
height: 40px;
text-align: center;
position: absolute;
display: none;
}
.navbarOuter:hover .hamborgor2 {
display: block;
}
.navbarOuter:hover .hamborgor {
display: none;
}
.navbarList {
padding: 40px 0px 0px 0px;
}
.navbarButton {
background-color: palegreen;
text-decoration: none;
line-height: 25px;
display: block;
width: 100px;
padding: 12px 5px;
}
.navbarInner {
display: none;
height: 100%;
position: fixed;
right: 0;
}
.navbarInner:hover,
.navbarOuter:hover+.navbarInner {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="styles.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
</head>
<body>
<div class="top">
<div class="navbarOuter">
<div class="hamborgor">≡</div>
<div class="hamborgor2">×</div>
</div>
<div class="navbarInner">
<ul class="navbarList">
<li><a href="Sub1.html" class="navbarButton">Sub1</a></li>
<li><a href="Sub2.html" class="navbarButton">Sub2</a></li>
<li><a href="Sub3.html" class="navbarButton">Sub3</a></li>
</ul>
</div>
</div>
</body>
</html>