我在使用HTML和CSS的导航栏方面遇到了一些问题。我想知道如何让我的导航栏居中,以及如何显示我的下拉内容(在服务下)。此外,我希望将鼠标悬停在"服务"但我似乎无法使它发挥作用。任何人都可以指出我的代码出错了吗?以下是我的代码。
/* Dropdown Button */
.dropbtn {
background-color: red;
color: red;
padding: 16px;
font-size: 16px;
border: none;
}
/* The container needed to position the dropdown content */
.dropdown {
position: relative;
display: inline-block;
}
/* Dropdown Content (Hidden by Default) */
/*CHECK*/
nav ul ul {
display: none;
position: absolute;
min-width: 160px;
z-index: 1;
}
/* Links inside the dropdown */
ul ul a {
padding: 12px 16px;
text-decoration: none;
display: block;
}
/*Show the dropdown menu on hover for services*/
.dropbtn:hover .dropdown_content {
display: block;
}
/* Change color of dropdown links on hover */
.dropdown_content a:hover {
background-color: #ddd;
}

<nav style="height: 60px">
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="about.html">ABOUT</a></li>
<li><a href="services.html" class="dropbtn">SERVICES</a></li>
<ul class="dropdown_content">
<li><a href="beauty_services.html">BEAUTY SERVICES</a></li>
<li><a href="styling_services.html">STYLING SERVICES</a></li>
</ul>
<li><a href="events.html">EVENTS</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="shop.html">SHOP</a></li>
<li><a href="contact.html">CONTACT</a></li>
<ul>
<li><a href="book.html">BOOK</a></li>
</ul>
</ul>
</nav>
&#13;
答案 0 :(得分:1)
我对代码做了一些更改。解释在评论中。
nav ul {
/* Remove list bullets from menu */
list-style-type: none;
}
/* Dropdown Button */
.dropbtn {
background-color: red;
/* cosmetic: you won't see red text on red background :) */
/*color: red;*/
padding: 16px;
font-size: 16px;
border: none;
}
/* The container needed to position the dropdown content */
/* This isn't used, so we don't need it */
/*.dropdown {
position: relative;
display: inline-block;
}*/
/* Dropdown Content (Hidden by Default) */
/*CHECK*/
nav ul ul {
display: none;
position: absolute;
/* move it down to avoid overlapping the button */
margin-top: 16px;
/* align it to the button */
margin-left: -40px;
min-width: 160px;
z-index: 1;
}
/* float the main items (and only those) to create a horizontal bar */
nav>ul>li {
float: left;
margin: 5px;
}
/* Links inside the dropdown */
ul ul a {
padding: 12px 16px;
text-decoration: none;
display: block;
}
/*Show the dropdown menu on hover for services*/
/* + combinator selects adjacent siblings, in this case the dropdown */
.dropbtn:hover+.dropdown_content,
.dropdown_content:hover {
display: block;
}
/* Change color of dropdown links on hover */
.dropdown_content a:hover {
background-color: #ddd;
}
<nav style="height: 60px">
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="about.html">ABOUT</a></li>
<li>
<a href="services.html" class="dropbtn">SERVICES</a>
<ul class="dropdown_content">
<!-- Put the dropdown items into one <li> with the dropbtn, makes hover effect easier -->
<li><a href="beauty_services.html">BEAUTY SERVICES</a></li>
<li><a href="styling_services.html">STYLING SERVICES</a></li>
</ul>
</li>
<li><a href="events.html">EVENTS</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="shop.html">SHOP</a></li>
<li><a href="contact.html">CONTACT</a></li>
<ul>
<li><a href="book.html">BOOK</a></li>
</ul>
</ul>
</nav>
答案 1 :(得分:0)
也许在代码下方可以帮助您根据需要对代码进行一些更改
docs
&#13;
中添加了这四行代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
background-color: red;
/* cosmetic: you won't see red text on red background :) */
/*color: red;*/
font-size: 16px;
border: none;
}
/*Show the dropdown menu on hover for services*/
.dropdown_content {
color: black;
padding: 12px 16px;
text-decoration: none;
display: none;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown_content > li:hover {background-color: #ddd;}
.dropdown:hover .dropdown_content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>
<nav style="height: 60px">
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="about.html">ABOUT</a></li>
<div class="dropdown">
<li><a href="services.html" class="dropbtn">SERVICES</a></li>
<ul class="dropdown_content">
<li><a href="beauty_services.html">BEAUTY SERVICES</a></li>
<li><a href="styling_services.html">STYLING SERVICES</a></li>
</ul>
</div>
<li><a href="events.html">EVENTS</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="shop.html">SHOP</a></li>
<li><a href="contact.html">CONTACT</a></li>
<ul>
<li><a href="book.html">BOOK</a></li>
</ul>
</ul>
</nav>
</body>
</html>
并为下拉列表添加了.dropdown_content > li:hover {background-color: #ddd;}
.dropdown:hover .dropdown_content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
.dropdown_content {
color: black;
padding: 12px 16px;
text-decoration: none;
display: none;
}
元素
答案 2 :(得分:0)
这是另一种可能的帮助:
/*Style for when the container class is displayed*/
.dropdown_content {
display: none;
padding: 5px;
position: absolute;
width: 200px;
text-align: center;
}
/*Style in charge that when we put the mouse on top of him, we show him the hidden*/
/*Show the Service list*/
#dropbtnService:hover .dropdown_content {
display: block;
}
/*Show the Book list*/
#dropbtnBook:hover .dropdown_content {
display: block;
}
/* Change color of dropdown links on hover */
.dropdown_content a:hover {
background-color: #ddd;
}
/*The other code has been deleted due to the number of errors that occurred*/
* {
margin: 0;
padding: 0;
}
#main {
margin: auto;
width: 900px;
height: 100px;
border: 1px solid #444;
text-align: center
}
div > ul {
padding: 20px;
}
div > li {
list-style: none;
display: inline;
margin: 0 20px 0 20px;
}
nav > ul > li, div {
float: left;
padding: 1em;
}
/* Links inside the dropdown */
ul ul a {
padding: 12px 16px;
text-decoration: none;
display: block;
}
nav ul {
/* Remove list bullets from menu */
list-style-type: none;
}
#dropbtnService {
background-color: red;
padding: 16px;
font-size: 16px;
}
&#13;
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/css.css">
</head>
<body>
<div id="main">
<nav style="height:60px">
<ul>
<li><a class="active" href="index.html">HOME</a></li>
<li><a href="about.html">ABOUT</a></li>
<!--Modify and agree div's-->
<div id="dropbtnService">
<li><a href="services.html">SERVICES</a></li>
<div class="dropdown_content">
<ul>
<li><a href="beauty_services.html">BEAUTY SERVICES</a></li>
<li><a href="styling_services.html">STYLING SERVICES</a></li>
</ul>
</div>
</div>
<li><a href="events.html">EVENTS</a></li>
<li><a href="blog.html">BLOG</a></li>
<li><a href="shop.html">SHOP</a></li>
<div id="dropbtnBook">
<li><a href="contact.html">CONTACT</a></li>
<div class="dropdown_content">
<ul>
<li><a href="book.html">BOOK</a></li>
</ul>
</div>
</div>
</ul>
</nav>
</div>
</body>
</html>
&#13;