我正在使用下拉菜单设置菜单栏,但问题是您可以在下面的代码中看到
我已经尝试过通过更改边距和填充来进行操作,但是没有任何作用
.topnav {
overflow: hidden;
background-color: white;
position: relative;
margin: -10px -126px;
}
.topnav #myLinks {
display: visible;
background-color: white;
margin: 10px 20px;
}
.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
.topnav a.icon {
background: black;
display: block;
position: absolute;
top: 0;
margin: 5px 166px;
}
.topnav a:hover {
background-color: #ddd;
}
.active {
background-color: #4CAF50;
color: white;
float: right;
margin: 0px 81px;
}
}
div#myLinks {
margin: 0px 20px 0px 107px;
}
<div class="topnav">
<a href="#home" class="active">
<img src="Images/logo-black.png" alt="AudiLogo" />
</a>
<div id="myLinks">
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars">Menu</i>
</a>
</div>
</div>
function myFunction() {
var x = document.getElementById("myLinks");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
我希望有一个下拉菜单,但它在同一菜单行内 但是我想要的实际输出是单击菜单时在菜单下方的下拉菜单。
在此处输入代码
答案 0 :(得分:1)
尝试此代码
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
background-color: red;
}
.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 {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="navbar">
<a href="#home">Home</a>
<a href="#news">News</a>
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</div>
</div>
</div>
<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>
</body>
</html>
答案 1 :(得分:0)
我编辑了您的代码,以显示您想要显示的子导航。它仍然需要一些样式,我假设您想像在原始代码中一样使用javascript。
function myFunction() {
var x = document.getElementById("myLinks");
if (x.style.display === "block") {
x.style.display = "none";
} else {
x.style.display = "block";
}
}
.topnav {
overflow: hidden;
background-color: white;
position: relative;
margin: -10px -126px;
}
.topnav #myLinks {
display: visible;
background-color: white;
margin: 10px 20px;
}
.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
.topnav a.icon {
background: black;
display: block;
position: absolute;
top: 0;
margin: 5px 166px;
}
.topnav a:hover {
background-color: #ddd;
}
.active {
background-color: #4caf50;
color: white;
float: right;
margin: 0px 81px;
}
div#myLinks {
margin: 0px 20px 0px 107px;
}
div#myLinks > a:first-child {
margin-top: 52px;
}
div#myLinks > a {
color: black;
margin-left: 100px;
padding-left: 60px;
}
<div class="topnav">
<a href="#home" class="active">
<img src="Images/logo-black.png" alt="AudiLogo" />
</a>
<div id="myLinks">
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars">Menu</i>
</a>
</div>
</div>
答案 2 :(得分:-2)
请尝试使用此代码
<html>
<head>
<style>
.topnav {
overflow: hidden;
background-color:white;
position: relative;
/*margin: -10px -126px;*/
}
.topnav #myLinks {
display: none;
background-color:white;
margin:48px 0px;
}
.topnav #myLinks a {
color: #000;
}
.topnav a {
color: white;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
display: block;
}
.topnav a.icon {
background: black;
display: block;
position: absolute;
top: 0;
/*margin: 5px 166px;*/
}
.topnav a:hover {
background-color: #ddd;
}
.active {
background-color: #4CAF50;
color: white;
float:right;
margin: 0px;
}
}
div#myLinks {
margin: 0px 20px 0px 107px;
}
</style>
<script>
function myFunction(){
var menu = document.getElementById("myLinks");
if(menu.style.display == "block"){
menu.style.display = "none";
}
else{
menu.style.display = "block";
}
}
</script>
</head>
<body>
<div class="container-fluid">
<header>
<!-- Top Navigation Menu -->
<div class="topnav">
<a href="#home" class="active">
<img src="Images/logo-black.png" alt="AudiLogo">
</a>
<div id="myLinks">
<a href="#news">News</a>
<a href="#contact">Contact</a>
<a href="#about">About</a>
</div>
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars">Menu</i>
</a>
</div>
</header>
</div>
</body>
</html>