【问题标题】:Displaying third tier submenus properly with css only menu仅使用 css 菜单正确显示第三层子菜单
【发布时间】:2012-12-29 03:59:49
【问题描述】:

我正在开发一个新网站,但 css 菜单有问题。通过例子更容易解释:这里是网站的链接:http://www.webau.net/CSFF/index.asp

在“Home”父菜单项下,我有以下配置:

Top level menu (parent)
    Submenu 1 (child 1)
        Submenu 2 (grandchild 1)
    Submenu 3 (child 2)
    Submenu 4 (child 3)

相反,它出现在这样的页面上,孙子 1 看起来取代了子 2 菜单:

Top level menu (parent)
    Submenu 1 (child 1)
    Submenu 2 (grandchild 1)
    Submenu 4 (child 3)

我可以看到子菜单 2 的列表略有偏移。这让我认为这是试图不恰当地显示孙子。

所以我假设我有两个问题..

首先,当您将鼠标悬停在父菜单项上时,第三层菜单(孙子菜单 2)会同时显示第二层菜单(子子菜单 1、3、4)。

其次,由于某种原因,孙子菜单 2 项正在取代子子菜单 3(位于其上)。

我认为当我更正孙子菜单的显示时,问题二会得到解决。

谁能帮我弄清楚如何添加新的css代码来处理第三层或多层菜单..所以它们在他们自己的父级悬停之前是隐藏的,然后显示在它的父级子菜单的右侧列?

再次感谢您的帮助。 SunnyOz

为了您的方便: HTML 代码:

<div id="navcontainer">
    <div id="navsection">  
        <ul>
            <li id="navactive"><a class="current" href="#">Home</a>
                <ul>
                    <li><a href="#">submenu 1</a>
                        <ul>
                            <li><a href="#">submenu 2</a></li>
                        </ul>
                    </li>
                    <li><a href="#">submenu 3</a></li>
                    <li><a href="#">submenu 4</a></li>
                </ul>
            </li>
            <li><... rest of menu items not needed for example>
            </li>
        </ul>
    </div>
</div>

CSS 代码:

#navcontainer 
{
    width: 711px;
    height: 25px;
    text-align: center;
    margin: 0px auto; /*Center container on page*/
    clear: both;
    background-color: #129F9F;
    border: 3px solid #FFFFFF;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 

    /* IE10 Consumer Preview */ 
    background-image: -ms-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Mozilla Firefox */ 
    background-image: -moz-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Opera */ 
    background-image: -o-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* Webkit (Safari/Chrome 10) */ 
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #16ACAC), color-stop(1, #0D6F6F));

    /* Webkit (Chrome 11+) */ 
    background-image: -webkit-linear-gradient(top, #16ACAC 0%, #0D6F6F 100%);

    /* W3C Markup, IE10 Release Preview */ 
    background-image: linear-gradient(to bottom, #16ACAC 0%, #0D6F6F 100%); 
}
#navsection
{
    height: 24px;
    line-height: 24px;
    font-size: 12px;
    position: relative;
}
#navsection ul
{
    padding: 0px 0px 0px 35px;  /* padding on left to get nav menu to center.. since it has a float left to make it display properly*/
    list-style: none;
}
#navsection ul li
{
    padding: 0;
    margin: 0;
    border-right: 2px solid #129F9F;
    float: left;
}
#navsection ul li.navcontact  /* to stop right border at end of nav line */
{
    padding: 0;
    margin: 0;
    border-right: none;
    float: left;
}
#navsection ul li a
{
    color: #FFF;
    display: block;
    text-decoration: none;
    padding: 0 15px;
}
#navsection > ul > li > a:hover, #navsection  > ul > li:hover > a  
{  
    text-decoration: none;
    color: #EAA339;
    /* IE10 Consumer Preview */ 
    background-image: -ms-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Mozilla Firefox */ 
    background-image: -moz-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Opera */ 
    background-image: -o-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Webkit (Safari/Chrome 10) */ 
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #0D6F6F), color-stop(1, #16ACAC));

    /* Webkit (Chrome 11+) */ 
    background-image: -webkit-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* W3C Markup, IE10 Release Preview */ 
    background-image: linear-gradient(to bottom, #0D6F6F 0%, #16ACAC 100%); 
}

#navsection > ul > li > a.current:hover, #navactive a.current:link, #navactive a:visited, #navactive > ul  li  a:hover, #navsection a:hover
{
    text-decoration: none;
    color: #EAA339;
    /* IE10 Consumer Preview */ 
    background-image: -ms-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Mozilla Firefox */ 
    background-image: -moz-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Opera */ 
    background-image: -o-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* Webkit (Safari/Chrome 10) */ 
    background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #0D6F6F), color-stop(1, #16ACAC));

    /* Webkit (Chrome 11+) */ 
    background-image: -webkit-linear-gradient(top, #0D6F6F 0%, #16ACAC 100%);

    /* W3C Markup, IE10 Release Preview */ 
    background-image: linear-gradient(to bottom, #0D6F6F 0%, #16ACAC 100%); 
}

#navsection ul li ul
{
    display: none;
    width: auto;
    position: absolute;
    padding: 0px;
    margin: 0px;
}
#navsection ul li:hover ul
{
    display: block;
    position: absolute;
    margin: 0;
    padding: 0;
}
#navsection ul li:hover li
{
    float: none;
    list-style: none;
    margin: 0px;
}
#navsection ul li:hover li
{
    font-size: 12px;
    height: 24px;
    background: #54C4C4;
    border: 1px solid #FFFFFF;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 
}
#navsection ul li:hover li a
{
    font-size: 11px;
    color: #fff;
    padding: 0px;
    display: block;
    width: 150px;
}
#navsection ul li li a:hover
{
    font-size: 11px;
    height: 24px;
    color:#EAA339;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    -o-border-radius: 5px;
    -ms-border-radius: 5px;
    border-radius: 5px; 
}

【问题讨论】:

    标签: css hover position submenu


    【解决方案1】:

    这里有 3 个问题。

    1. 您正在显示存在于悬停的&lt;li/&gt; 下方的所有&lt;ul/&gt; 元素。 将 #navsection ul li:hover ul 更改为 #navsection ul li:hover &gt; ul 以仅选择直接子节点

    2. 您将每个掉落级别的位置设置为相同。 尝试添加#navsection ul ul ul { top: 1em; left: 140px; } 之类的内容,这将阻止孙子遮挡孩子。

    3. 您的 title 属性会对您的导航产生负面影响。 我会完全删除它们,因为它们不会在链接中添加任何真正有用的信息并且模糊了菜单项,使导航难以使用。

    希望对你有帮助:)

    【讨论】:

    • 谢谢,谢谢!!我用了你所有的建议,现在效果很好!我不敢碰运气,但我想知道你是否可以帮我处理另外两个项目。我已经再次将更改上传到 [link] (webau.net/CSFF/index.asp),但现在将下拉菜单移至“What about CSFF”选项卡。我添加了 3 层子菜单。第一个问题:如您所见,如果您将鼠标悬停在子菜单 2 上,则会显示第三级子菜单,但它位于前一个子菜单列顶部下方 1em 处,而不是其父级位置下方 1em 处。可以解决吗?
    • 第二个问题:有没有办法使子菜单的大小与其自己的子菜单组中的最大内容相同?我将所有子菜单的大小设置为 200 像素,它适用于第一个子菜单。但在我的示例中,对于其余的子菜单来说太长了。 (另请参阅“捐款”中的子菜单。)我显然希望子菜单的大小与它们自己的子菜单组一致,但它们可以与其他子菜单组不同。再次感谢您的帮助!
    • 不用担心 :)。第一个新问题:从#navsection ul li:hover &gt; ul 中删除margin: 0 并将#navsection ul ul ul { top: 1em; } 更改为#navsection ul ul ul { margin-top: -1em; }。第二个问题:你可以,但它会涉及更多的标记。为导航中的每个第一级项目添加一个类,并使用该类设置该项目的子菜单宽度和偏移量(例如left:205px)。您需要知道菜单宽度以设置偏移量,这就是您需要这些类的原因。希望对您有所帮助:) 如果您还有其他问题,请开始一个新线程;我们希望其他用户可以发现这些问题
    • 再次感谢您的帮助。我成功地移动了我的子菜单以完美地与他们自己的父母相关。我仍然无法弄清楚如何修复适合文本大小的统一大小..但我会继续努力。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2013-11-23
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多