HTML 下拉菜单从左到右而不是直接向下。显示 :block not working

标签 html css

当我将鼠标悬停在团队导航栏上时,而不是列出的团队直接出现在它的垂直下方,它会向下移动 1,然后从左到右显示它,我不确定为什么,因为在导航栏下 ul li:hover > ul I放置 display:block 理论上应该可以工作

nav ul {
  /*Manages locations of the nav boxes*/
  display: block;
  list-style-image: none;
  list-style-position: outside;
  list-style-type: none;
  margin: 0 0 0 -12px;
  padding: 0px;
}

nav ul li {
  /*Orders the nav boxes from left to right*/
  float: left;
}

nav ul li a {
  /* All the boxes like News, Table etc*/
  display: inline-block;
  padding: 17px 17px 17px 17px;
  background-color: gray;
  border: 1px solid black;
  display: block;
  line-height: 40px;
  font: 95% Helvetica, Arial, sans-serif;
  color: #66ff66;
  text-decoration: none;
  border-radius: 5px;
  font-size: 15px;
}

nav ul li a:hover {
  opacity: .7;
  text-decoration: none;
  display: block;
}

nav ul ul {
  display: none;
  position: absolute;
}

nav ul li:hover>ul {
  display: block;
}
<nav>
  <div class="panel center">
    <ul>
      <li><a href="Home.html">Home</a></li>
      <!--All the pages on the website-->
      <li><a href="SerieA_Tables.html">Table</a></li>
      <li><a href="News.html">News</a></li>
      <li><a href="Teams.html">Teams</a>
        <ul>
          <li><a href="#">Juventus</a></li>
          <li><a href="#">AC Milan</a></li>
          <li><a href="#">Torino</a></li>
        </ul>
      </li>
      <li><a href="About.html">About</a></li>
    </ul>
  </div>
</nav>

最佳答案

float:left 导致它出现在 nav ul li 上。它使导航下的所有 li 都向左浮动。如果将其更改为 nav>div>ul>li,它将解决问题。

nav ul {             /*Manages locations of the nav boxes*/
display:block;
list-style-image: none;
list-style-position: outside;
list-style-type: none;
margin: 0 0 0 -12px;
padding:0px;

}

nav>div>ul>li {   /*Orders the nav boxes from left to right*/
float:left;
}

nav ul li a {                /* All the boxes like News, Table etc*/
display:inline-block;
padding: 17px 17px 17px 17px;
background-color: gray;
border:1px solid black;
display: block;
line-height: 40px;
font: 95%  Helvetica, Arial, sans-serif; 
color: #66ff66;
text-decoration: none;
border-radius: 5px;
font-size: 15px;
}

nav ul li a:hover{ 
opacity: .7;
text-decoration: none;  
display:block;
}

nav ul ul{              
display:none;
position:absolute;  
}

nav ul li:hover > ul{ /*Highlights box when you hover over it*/
display:block;
}
<nav>
        <div class="panel center">
            <ul>    
                <li><a href="Home.html">Home</a></li>                <!--All the pages on the website-->
                <li><a href="SerieA_Tables.html">Table</a></li>
                <li><a href="News.html">News</a></li>
                <li><a href="Teams.html">Teams</a>
                    <ul>
                        <li><a href = "#">Juventus</a></li>
                        <li><a href = "#">AC Milan</a></li>
                        <li><a href = "#">Torino</a></li>
                    </ul>
                </li>
                <li><a href="About.html">About</a></li>
            </ul>
         </div>
    </nav>

关于HTML 下拉菜单从左到右而不是直接向下。显示 :block not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44811744/

相关文章:

javascript - 通过 jquery 从附加的 div 传递函数值

javascript - 如何处理按钮的onclick事件?

javascript - 为什么我的 next() 和 prev() 切换的文本输出不正确?

css - Webpack scss 加载器、css 加载器和 post css

jquery - 长文本自动调整大小列

html - 如何使用CSS将页脚右侧和左侧设为曲线

jquery - Jquery改变后返回原来的背景

html - 使 blockquote 适应图像高度并添加程式化的引用

javascript - 谁能解释为什么 jQuery 附加的内联 block 元素会丢失空间?

javascript - 将父div设置在子元素后面