如何使嵌套列表显示在父列表项下方?

How to make nested list appear below the parent list item?

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>

但是,父 <li> 出现在顶部,嵌套列表在下方,父列表的其余 <li> 在下方与子列表的末尾对齐。

如何让父列表项主页、服务和联系人水平对齐直线

说明

您需要为嵌套的 <li> 标签添加以下两行 CSS 代码。

ul li ul {    
    position: absolute;
    top: auto;
}

将位置设置为绝对位置并将顶部设置为自动将在父标签下显示嵌套的 ul。

代码

按下面的'Run code snippet'按钮查看代码输出。

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>

说明

position: absoluteoverflow: hidden 属性造成干扰,导致下拉列表无法正常显示。我已经在代码中注释了它们,所以你可以看到。

您应该避免使用绝对定位,因为您可以通过简单地正确嵌套标签来达到相同的效果。例如,我将你的 navbar 嵌套在红色 header.

代码

按下面的'Run code snippet'按钮查看代码输出。

* {
  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;
}
<!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>