html - 如何创建嵌套的CSS水平菜单?

标签 html css

我查看了来自 stakoverflow 网站的帖子,但它并没有完全解决我的问题。我遇到的问题是我的导航菜单宽度设置为 100%,我不确定如何控制子或嵌套 UL 菜单。这是 jsFiddle link . “字符”下的子菜单是我现在正在使用的有问题的菜单。如果我调整浏览器窗口的大小,那么子菜单的位置就会改变。

<nav>
  <ul>
    <li><a href="../">HOME</a></li>
    <li><a href="#">CHARACTER</a>
      <ul>
        <li><a href="#" target="_blank">Bill</a></li>
        <li><a href="#" target="_blank">Till</a></li>
        <li><a href="#" target="_blank">Cill</a></li>
        <li><a href="#" target="_blank">Will</a></li>
      </ul>
    </li>
    <li><a href="#">HISTORY</a></li>
    <li><a href="#">STORY</a></li>
  </ul>
</nav>

非常感谢任何帮助。

最佳答案

尝试添加“float: left; width: 100%;”进入你的 ul 在 css 中。所以 HTML 是:

 <nav>
  <ul>
    <li><a href="../">HOME</a></li>
    <li><a href="../exercise/chapter9/">CHARACTER</a>
      <ul class="sub-menu">
        <li><a href="../exercise/chapter9/form1.html" target="_blank">Bill</a></li>
        <li><a href="../exercise/chapter9/form2.html" target="_blank">Till</a></li>
        <li><a href="../exercise/chapter9/form3.html" target="_blank">Cill</a></li>

这是CSS:

/*THIS IS THE NAVATION MENU */
nav {
    list-style:none;
    text-align:center;
    width: 100%;/*margin:20px;*/
    padding: 0;
    margin: 0 auto;
}

nav ul ul {
    display: none;
}
nav ul li:hover > ul {
    display: block;
    z-index: 999;
}
nav ul {
    float: left;
    width:100%;
    background: #efefef;
    background: linear-gradient(top, #1295D8 0%, #005581 100%);
    background: -moz-linear-gradient(top, #1295D8 0%, #005581 100%);
    background: -webkit-linear-gradient(top, #1295D8 0%, #005581 100%);
    box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
    /*padding: 0 20px;
    border-radius: 10px;  */
    list-style: none;
    position: relative;/*display: inline-table;*/
}
nav ul:after {
    content: "";
    clear: both;
    display: block;
    width:100%;
    margin:0 auto;
}
nav ul li {
    /*float: left;*/
    display: inline;
    padding: 13px 20px;
    position: relative;
}
nav ul li:hover {
    background: #4b545f;
    background: linear-gradient(top, #78A4BF 0%, #2E4559 40%);
    background: -moz-linear-gradient(top, #78A4BF 0%, #2E4559 40%);
    background: -webkit-linear-gradient(top, #78A4BF 0%, #2E4559 40%);
}
nav ul li:hover a {
    color: #fff;
}
nav ul li a {
    display: inline-block;
    padding: 15px 20px;
    color: #fff;
    text-decoration: none;
}
nav ul .sub-menu {
    background: #5f6975;
    border-radius: 0px;
    padding: 0;
    position: absolute;
    top: 100%;
    left: 0%;
    float: left;
}
nav ul ul li {
    padding: 13px 0;
    float: none;
    border-top: 1px solid #6b727c;
    border-bottom: 1px solid #575f6a;
    position: relative;
}
nav ul ul li a {
    /*padding: 13px 20px;*/
    color: #fff;
}
nav ul ul li a:hover {
    /*background: #4b545f;*/
    background: #4b545f;
    background: linear-gradient(top, #78A4BF 0%, #2E4559 40%);
    background: -moz-linear-gradient(top, #78A4BF 0%, #2E4559 40%);
    background: -webkit-linear-gradient(top, #78A4BF 0%, #2E4559 40%);
    /*padding: 13px 20px;*/
}
nav ul ul ul {
    position: absolute;
    left: 100%;
    top: 0;
}

关于html - 如何创建嵌套的CSS水平菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20233634/

相关文章:

javascript - ajax加载后 Bootstrap 切换不起作用

JavaScript 正则表达式解释

html - 如何在 css 中正确居中 100% 宽度的 div

html - 如何使 HTML 颜色矩形看起来更好?

javascript - 如何在javascript中制作一个css元素?

php - 页脚中的 float /固定div

javascript - 隐藏多个div onclick

html - 为什么我的图片没有出现 Html?

jquery - 如何创建没有 SCSS /暂停的循环幻灯片

html - bool 玛问题 : overflow when using navbar in columns