我正在尝试创建一个带有悬停和下拉菜单链接的导航栏。但是,我想:
赞赏有关如何改进代码的任何建议。
请在下面找到jsbin文件的链接: https://jsbin.com/yokujek/edit?html,css,output
.navigationmenu {
overflow: auto;
background-color: #333;
white-space: nowrap;
}
.navigationmenu a {
display: inline-block;
color: white;
text-align: center;
padding: 14px;
text-decoration: none;
}
.dropdown {
float: left;
width: 20%;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navigationmenu a:hover,
.dropdown:hover .dropbtn {
background-color: 777;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 20%;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
-webkit-box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
-ms-box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
-o-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: center;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content,
.dropdown:focus .dropdown-content {
display: block;
}
<div class="navigationmenu">
<a href="Welcomepage.html">Home</a>
<div class="dropdown">
<a href="Aboutus.html"><button class="dropbtn">About Us</button></a>
<div class="dropdown-content">
<a href="meettheteam.html">Meet the Team</a>
<a href="timeline.html">Timeline</a>
<a href="businessmodel.html">Business Model</a>
</div>
</div>
<div class="dropdown">
<a href="Events.html"><button class="dropbtn">Events</button></a>
<div class="dropdown-content">
<a href="generalpublic.html">Events for General Public</a>
<a href="introductory.html">Introductory Level</a>
<a href="intermediate.html">Intermediate Level</a>
<a href="expert.html">Expert Level</a>
</div>
</div>
<div class="dropdown">
<a href="Projects.html"><button class="dropbtn">Projects</button></a>
<div class="dropdown-content">
<a href="studentproject.html">Individual Projects</a>
<a href="rlabprojects.html">R:Lab Projects</a>
<a href="corporateprojects.html">R:Lab Partner's Projects</a>
</div>
</div>
<div class="dropdown">
<a href="Resources.html"><button class="dropbtn">Resources</button></a>
<div class="dropdown-content">
<a href="http://WWW.MOSTAQBAL.AE">Applied Science Research News (Arabic) </a>
<a href="industry.html">Industry Opportunitiese</a>
<a href="furtherstudy.html">Further Study Opportunities</a>
</div>
</div>
</div>
谢谢!
答案 0 :(得分:1)
由于下拉列表是float: left
,而home是inline-block
元素,因此它将所有其他元素放在home
标签之前。
我已经按照您的要求做了。
.navigationmenu {
overflow: auto;
background-color: #333;
white-space: nowrap;
}
.navigationmenu a {
display: inline-block;
color: white;
text-align: center;
padding: 14px;
text-decoration: none;
}
.dropdown {
float: left;
width: 20%;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navigationmenu a:hover, .dropdown:hover .dropbtn {
background-color: 777;
}
.dropdown-content {
display: none;
position: absolute;
max-width: 20%;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
white-space: pre-line;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.home {
float: left;
padding:28px 0!important;
width: 20%
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div class="navigationmenu">
<a href="Welcomepage.html" class="home">Home</a>
<div class="dropdown">
<a href="Aboutus.html"><button class="dropbtn">About Us</button></a>
<div class="dropdown-content">
<a href="meettheteam.html">Meet the Team</a>
<a href="timeline.html">Timeline</a>
<a href="businessmodel.html">Business Model</a>
</div>
</div>
<div class="dropdown">
<a href="Events.html"><button class="dropbtn">Events</button></a>
<div class="dropdown-content">
<a href="generalpublic.html">Events for General Public</a>
<a href="introductory.html">Introductory Level</a>
<a href="intermediate.html">Intermediate Level</a>
<a href="expert.html">Expert Level</a>
</div>
</div>
<div class="dropdown">
<a href="Projects.html"><button class="dropbtn">Projects</button></a>
<div class="dropdown-content">
<a href="studentproject.html">Individual Projects</a>
<a href="rlabprojects.html">R:Lab Projects</a>
<a href="corporateprojects.html">R:Lab Partner's Projects</a>
</div>
</div>
<div class="dropdown">
<a href="Resources.html"><button class="dropbtn">Resources</button></a>
<div class="dropdown-content">
<a href="http://WWW.MOSTAQBAL.AE">Applied Science Research News (Arabic) </a>
<a href="industry.html">Industry Opportunitiese</a>
<a href="furtherstudy.html">Further Study Opportunities</a>
</div>
</div>
</div>
</body>
</html>
答案 1 :(得分:0)
在下面的代码中,我在导航菜单中使用了一个flexbox。这是因为,可以删除所有浮动元素,并且所有HTML元素都保留在自然文档流中。
.navigationmenu {
background-color: #333;
display: flex;
align-items: center;
/* Vertical alignment */
justify-content: space-around;
/* Horizontal alignment */
}
.navigationmenu>* {
width: 20%; /* Set all direct children of the nav.menu to 20% width */
}
.navigationmenu a {
display: inline-block;
color: white;
text-align: center;
padding: 14px;
text-decoration: none;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
color: white;
padding: 14px;
background-color: inherit;
font-family: inherit;
}
.navigationmenu a:hover,
.dropdown:hover .dropbtn {
background-color: 777;
}
.dropdown-content {
display: none;
position: absolute;
max-width: 20%;
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
white-space: pre-line;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: center;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navigationmenu">
<a href="Welcomepage.html">Home</a>
<div class="dropdown">
<a href="Aboutus.html"><button class="dropbtn">About Us</button></a>
<div class="dropdown-content">
<a href="meettheteam.html">Meet the Team</a>
<a href="timeline.html">Timeline</a>
<a href="businessmodel.html">Business Model</a>
</div>
</div>
<div class="dropdown">
<a href="Events.html"><button class="dropbtn">Events</button></a>
<div class="dropdown-content">
<a href="generalpublic.html">Events for General Public</a>
<a href="introductory.html">Introductory Level</a>
<a href="intermediate.html">Intermediate Level</a>
<a href="expert.html">Expert Level</a>
</div>
</div>
<div class="dropdown">
<a href="Projects.html"><button class="dropbtn">Projects</button></a>
<div class="dropdown-content">
<a href="studentproject.html">Individual Projects</a>
<a href="rlabprojects.html">R:Lab Projects</a>
<a href="corporateprojects.html">R:Lab Partner's Projects</a>
</div>
</div>
<div class="dropdown">
<a href="Resources.html"><button class="dropbtn">Resources</button></a>
<div class="dropdown-content">
<a href="http://WWW.MOSTAQBAL.AE">Applied Science Research News (Arabic) </a>
<a href="industry.html">Industry Opportunitiese</a>
<a href="furtherstudy.html">Further Study Opportunities</a>
</div>
</div>
</div>