简体   繁体   English

使用CSS显示子菜单而不使用无序列表

[英]Show submenu with CSS without using an unordered list

I got a question regarding showing a submenu with CSS. 我有一个关于显示CSS子菜单的问题。 I have the following HTML code: 我有以下HTML代码:

<div class="navigation">
    <a class="active" href="/">Home</a>
    <a href="/">Test1</a>
    <a href="/">Test2</a>
    <div class="submenu-wrapper">
      <a href="/">Test3</a>
      <div class="submenu">
        <a href="/">Submenu1</a>
        <a href="/">Submenu2</a>
      </div>      
    </div>
    <a href="/">Test4</a>
</div>

Due to implementation restriction I can not change my structure to, for example a <ul> format. 由于实施限制,我无法将结构更改为例如<ul>格式。

I did some research on the web to find out how I could show my submenu by using CSS. 我在网上做了一些研究,以了解如何使用CSS来显示子菜单。 I tried the following thing: 我尝试了以下操作:

.navigation .submenu-wrapper a:hover > .submenu{display:block;}

Can anyone tell my why this does not work and how could I solve this, with respect to my current implementation. 关于我当前的实现,谁能告诉我为什么这行不通以及如何解决这个问题。

Full code here: JSFIDDLE 完整代码在这里: JSFIDDLE

PS. PS。 Any answers like use bootstrap or transform your menu to a <ul> format is not what I am looking ;) 任何使用引导程序或将菜单转换为<ul>格式的答案都不是我想要的;)

Your code: 您的代码:

.navigation .submenu-wrapper a:hover > .submenu{display:block;}

Your .submenu is not inside the a . 您的.submenu不在a You could use the sibling selector: 您可以使用兄弟选择器:

.navigation .submenu-wrapper a:hover + .submenu{display:block;}

But to make the submenu usable, make sure your .submenu-wrapper has the same height as its content (by giving it a fixed height or an :after{clear:both;} and do this: 但是要使子菜单可用,请确保您的.submenu-wrapper高度与其内容高度相同(通过为其设置固定高度或:after{clear:both;}进行设置):

.navigation .submenu-wrapper:hover .submenu{display:block;}

Since your .submenu is absolutely positioned, you also need to position its parent, or else .submenu will fall off the screen (because you gave it top:100% relative to body). 由于您的.submenu是绝对定位的,因此您还需要定位其父级,否则.submenu会从屏幕上掉落(因为您将top:100%相对于正文的top:100% )。 Like this: 像这样:

.navigation .submenu-wrapper {position: relative;}

Updated fiddle: https://jsfiddle.net/xrtjngdr/4/ 更新小提琴: https : //jsfiddle.net/xrtjngdr/4/

You can achieve this by changing 您可以通过更改来实现

.navigation .submenu-wrapper a:hover > .submenu{display:block;}

To .navigation .submenu-wrapper a:hover + .submenu{display:block;} .navigation .submenu-wrapper a:hover + .submenu{display:block;}

You also have to add 您还必须添加

.submenu:hover{
  display:block;
}

Because if you want to click on your submenu, the links will disappear 因为如果您想单击子菜单,链接将消失

Just a few small changes and you're golden. 只需进行一些小更改,您便会变得很黄金。

See the comments in the code below for your changes. 有关更改,请参见下面的代码中的注释。

 .navigation { margin: 0; padding-left: 0; list-style: none; float: left; } .navigation .submenu-wrapper { float: left; display: block; position: relative; /* add relative position */ } .navigation > a, .navigation .submenu-wrapper a { float: left; position: relative; display: block; font-size: 20px; padding-right: 14px; padding-left: 14px; padding-top: 5.5px; padding-bottom: 8.5px; color: #000; text-decoration: none; } .submenu { position: absolute; display: none; /* display none */ top: 100%; left: 0; z-index: 1000; float: left; min-width: 160px; list-style: none; font-size: 18px; text-align: left; background-color: #245d94; border: 1px solid #fff; border-radius: 0; box-shadow: none; border-left: none; border-right: none; } .navigation a:hover { color: #fff; background-color: #245d94; } .navigation a.active { color: #fff; background-color: #e36c0a; } .navigation .submenu-wrapper:hover .submenu { /* As you want the menu to remain open when you move to the submenu */ display: block; } 
 <div class="navigation"> <a class="active" href="/">Home</a> <a href="/">Test1</a> <a href="/">Test2</a> <div class="submenu-wrapper"> <a href="/">Test3</a> <div class="submenu"> <a href="/">Submenu1</a> <a href="/">Submenu2</a> </div> </div> <a href="/">Test4</a> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM