function ServicesMenu() {
document.getElementsByClassName("services-cont")[0].classList.toggle("showS");
}
@charset "UTF-8";
*{padding:0;margin:0;}
body{min-width:300px; margin: 0px; padding: 0px; background:#f8f8f8 }
.wrapper {
max-width: 980px;
height:2000px;
margin: 0px auto;
position: relative;
background-color: #fff;
}
header{
width:980px;
height:105px;
background: #e60000;
}
ul.navbar {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
top:63px;
right:60px;
width: 560px;
display: block;
position: absolute;
}
ul.navbar li {
display:inline-block;
Margin-left:15px;
background: black;
}
ul.navbar li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 5px 15px;
text-decoration: none;
font-size: 17px;
}
ul.navbar li a:hover {background-color: #660000;}
.services-cont{display: none;}
.services-cont.showS {
list-style-type: none;
display: block;
background-color: #707070 ;
}
.services-cont.showS li {
float: none;
display: inline;
height:0;
border:none;
}
.services-cont.showS li a {
display: block;
text-align: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="menustyle.css">
<meta name="viewport" content="width=device-width, maximum-scale=1.0, minimum-scale=1.0, initial-scale=1" />
<script src="MenuFunc.js"></script>
<Title>Menu</title>
</head>
<body>
<div class="wrapper">
<header>
</header>
<ul class="navbar">
<li><a href="#home">Home</a></li>
<li><a href="javascript:void(0);" onclick="ServicesMenu()">Services</a>
<ul class="services-cont">
<li><a href="#">Service 1</a></li>
<li><a href="#">Service 2</a></li>
<li><a href="#">Service 3</a></li>
</ul>
</li>
<li><a href="#contact">Contact</a></li>
</ul>
</div>
</body>
</html>
但是,父{4}}显示在顶部,下面是嵌套列表,父列表的其余<li>
与子列表的末尾对齐。
如何在直线中水平对齐父项列表项主页,服务和联系?
答案 0 :(得分:0)
您需要为嵌套的<li>
标记添加以下两行CSS代码。
ul li ul {
position: absolute;
top: auto;
}
将位置设置为绝对,将top设置为auto将在父标记下显示嵌套的ul。
按下面的“运行代码段”按钮查看代码输出。
ul li {
background: black;
color: white;
display: inline-block;
width: 100px;
}
ul li ul {
margin: 0px;
padding: 0px;
background: grey;
/*YOU NEED THE TWO LINES BELOW*/
position: absolute;
top: auto;
}
ul li ul li {
color: white;
background: grey;
display: block;
width: 80px;
padding: 10px;
}
<html>
<body>
<ul>
<li>
Home
</li>
<li>
Services
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
Contact
</li>
</ul>
</body>
</html>
答案 1 :(得分:0)
position: absolute
和overflow: hidden
属性存在干扰,不允许下拉列表正常显示。我在代码中对它们进行了评论,以便您可以看到。
您应该避免使用绝对定位,只需简单地嵌套标记即可达到相同的效果。例如,我将您的navbar
嵌套在红色header
内。
按&#39;运行代码段&#39;按钮下方查看代码输出。
* {
padding: 0;
margin: 0;
}
body {
min-width: 300px;
margin: 0px;
padding: 0px;
background: #f8f8f8
}
.wrapper {
max-width: 980px;
height: 2000px;
margin: 0px auto;
/*position: relative;*/
background-color: #fff;
}
header {
padding: 20px;
width: 980px;
height: 30px;
background: #e60000;
}
.navbar {
list-style-type: none;
margin: 0;
padding: 0;
/*overflow: hidden;*/
top: 63px;
right: 60px;
width: 560px;
display: block;
/*position: absolute;*/
}
.navbar li {
display: inline-block;
Margin-left: 15px;
background: black;
}
.navbar li a {
display: inline-block;
color: #f2f2f2;
text-align: center;
padding: 5px 15px;
text-decoration: none;
font-size: 17px;
}
.navbar li a:hover {
background-color: #660000;
}
.navbar li ul {
position: absolute;
top: auto;
list-style-type: none;
display: none;
background-color: #707070;
}
.navbar li ul li {
float: none;
display: inline;
height: 0;
border: none;
}
.navbar li ul li a {
display: block;
text-align: left;
}
.navbar li:hover>ul {
display: block;
}
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
<Title>Menu</title>
</head>
<body>
<div class="wrapper">
<header>
<ul class="navbar">
<li><a href="#home">Home</a>
</li>
<li><a href="#">Services</a>
<ul>
<li><a href="#">Service 1</a>
</li>
<li><a href="#">Service 2</a>
</li>
<li><a href="#">Service 3</a>
</li>
</ul>
</li>
<li><a href="#contact">Contact</a>
</li>
</ul>
</header>
</div>
</body>
</html>
&#13;