问题
I need the position to be fixed, I can't figure out how to align the menu otherwise with the top of the screen, I always get gaps between the edge of the screen and the menu.
html:
<ul>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Monitor</a>
<div class="dropdown-content">
<a href="Index.html">Tube 1</a>
<a href="#">Tube 2</a>
</div>
</li>
<li><a href="Developer.html">Input</a></li>
<li><a href="Information.html">Information</a></li>
<li style="float:right" id="Credit"><a>Text</a></li>
</ul>
css:
ul {
z-index: 1;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
top: 0;
right: 0;
width: 100%;
font-family: "Arial Black";
font-size: 16px;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: #666;
text-align: center;
padding: 10px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #ddd;
}
li a.active {
color: white;
background-color: #008CBA;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
When I add position: fixed;
to ul, the dropdown menu doesn't work anymore, but when I don't add it, I get ugly gaps between the edge of the screen and the menu.
回答1:
Your body have margin: 8px. Just remove margin. Add in CSS:
body{
margin: 0;
}
回答2:
You should also add "position: fixed;" to dropdown-content.
ul {
z-index: 1;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #e7e7e7;
background-color: #f3f3f3;
top: 0;
right: 0;
width: 100%;
font-family: "Arial Black";
font-size: 16px;
position: fixed;
}
li {
float: left;
}
li a, .dropbtn {
display: inline-block;
color: #666;
text-align: center;
padding: 10px 16px;
text-decoration: none;
}
li a:hover:not(.active), .dropdown:hover .dropbtn {
background-color: #ddd;
}
li a.active {
color: white;
background-color: #008CBA;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<li class="dropdown">
<a href="javascript:void(0)" class="dropbtn">Monitor</a>
<div class="dropdown-content">
<a href="Index.html">Tube 1</a>
<a href="#">Tube 2</a>
</div>
</li>
<li><a href="Developer.html">Input</a></li>
<li><a href="Information.html">Information</a></li>
<li style="float:right" id="Credit"><a>Text</a></li>
</ul>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
<p>p</p>
来源:https://stackoverflow.com/questions/54135333/i-want-a-dropdown-menu-but-when-i-set-the-position-fixed-it-doesnt-work