html - 使用 CSS 创建下拉菜单时遇到问题
问题描述
我正在尝试仅为一个 li a 元素创建一个下拉菜单,但下拉菜单水平显示而不是垂直显示。它基于我之前编写的另一个 html 代码,它可以工作,但我不知道这个代码有什么问题。我只需要最后一个 li 作为下拉列表,并且孔代码是响应式的。
这是html代码:
<header id=main_header>
<nav><ou id="header_items">
<li id="first"><a> LOGO </a></li>
<li><a href="."> HABITACIONES </a></li>
<li><a href="."> SPA </a></li>
<li><a href="."> CONFORT </a></li>
<li><a href="."> UBICACIÓN </a></li>
<li><a href="."> IDIOMA </a>
<ul class="submenu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ou></li>
</ol></nav>
</header>
和 CSS:
body {
background-color: #e8ebea;
width: auto;
height: 2000px;
}
/*header*/
#main_header {
width: auto;
height: 60px;
margin: 0;
}
nav ul #header_items {
width: 100%;
height: 100%;
margin: 0;
}
#header_items {
list-style-type: none;
}
#header_items li {
display: inline-block;
position: relative;
width: 16%;
height: 100%;
margin-right: -4px;
}
#header_items li#first {
width: 20%;
}
#header_items li a {
display: block;
line-height: 60px;
text-align: center;
font-size: auto;
text-decoration: none;
color: #000;
}
#header_items li a:hover {
background-color: #dcdedd;
}
.submenu {
background-color: #e8ebea;
width: 100%;
height: auto;
display: block;
position: absolute;
}
谢谢!
解决方案
首先,您需要更正 HTML 中的 dom。在关闭子菜单</ou>
内部之前,您不会关闭。<li>
此外,您没有关闭<ul>
,并且您有一个</ou>
没有开始标签的关闭
至于菜单的CSS,你需要先把子菜单隐藏起来,只有在你过字的时候才显示出来。我为最后一个对象添加了一个 ID <li>
,因此我们可以将其定位为悬停。
考虑一下:
<style>
body {
background-color: #e8ebea;
width: auto;
height: 2000px;
}
/*header*/
#main_header {
width: auto;
height: 60px;
margin: 0;
}
nav ul #header_items {
width: 100%;
height: 100%;
margin: 0;
}
#header_items {
list-style-type: none;
}
#header_items li {
display: inline-block;
position: relative;
width: 16%;
height: 100%;
margin-right: -4px;
}
#header_items li#first {
width: 20%;
}
#header_items li a {
display: block;
line-height: 60px;
text-align: center;
font-size: auto;
text-decoration: none;
color: #000;
}
#header_items li a:hover {
background-color: #dcdedd;
}
.submenu {
background-color: #e8ebea;
width: 100%;
height: auto;
display: block;
position: absolute;
margin:0;
display:none;
}
#submenu:hover > .submenu{
display:inline-block;
}
</style>
<header id=main_header>
<nav>
<ou id="header_items">
<li id="first"><a> LOGO </a></li>
<li><a href="."> HABITACIONES </a></li>
<li><a href="."> SPA </a></li>
<li><a href="."> CONFORT </a></li>
<li><a href="."> UBICACIÓN </a></li>
<li id="submenu"><a > IDIOMA </a>
<ul class="submenu">
<li><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
</ul>
</li>
</ou>
</nav>
</header>
推荐阅读
- powershell - PowerShell - 如何从 PowerShell 获取我的 exe 程序的内存消耗峰值
- python - Kivy 标签未更新
- java - 在Java中,特别是浮点数,float和double有什么区别?
- flutter - 类型'string'不是flutter firebase数据库中'index'类型'int'的子类型
- ssl - 如何以及在哪里在 vapor 4 应用程序上安装 SSL 证书?
- c++ - 如何在C++中一一读取目录的所有文件
- ubuntu - Nginx 配置文件更新问题
- python - 如何在manim中配置轴长?图=轴()不工作
- javascript - 将 npm 包(概念 js sdk)转换为可在 iOS 脚本中使用的普通 js
- amazon-web-services - 将 AWS APIGATEWAY 同步调用到 Step Function?