下拉菜单的定位

问题描述:

我正在尝试创建3个轮胎的下拉菜单。我将第三级子菜单定位在父菜单的左侧,但顶部位置没有正确对齐。下拉菜单的定位

屏幕下方是我得到的结果。

CSS代码:

/* the menu_new_new is the list, you don't need a wrapping div */ 
#menu_new{ 
    Text-Align: Left; 
    /*width:100%;*/ 
    background:#194eb1; 
    /* height:30px;*/ 
} 

/* only mainmenu_new listitems */ 
#menu_new > li{ 
    display:inline-block; 
    position:relative; 
    line-height:35px; 
} 

/* links in mainmenu_new and both submenu_news */ 
#menu_new a{ 
    display:block; 
    text-decoration:none; 
    padding:0px 10px; 
    color:#fff; 
    text-align:Left; 
} 

/* submenu_news of both levels */ 
#menu_new li ul{ 
    position: absolute; 
    top: 35px; 
    left:0; 
    min-width:150px; 
    background: #194eb1; 
    Text-Align: Left; 


} 
/* submenu text */ 
#menu_new li ul li{ 

    Text-Align: Left; 
    line-height:20px; 
    List-style-type:none; 
    border: 1px solid white; 
} 
/* move the second level submenu_new to the right to don't overlap the parent submenu_new */ 
#menu_new ul ul{ 
    position: absolute; 
    top:auto; 
    left:150px; 
    Z-index: 10; 

} 

/* hover effect for submenu_new links */ 
#menu_new li li a:hover{ 
    background:rgba(255,255,255,0.5); 
} 

/* hide all submenu_news for default */ 
#menu_new li ul{ 
    display: none; 
} 

/* show a submenu_new if the direct parent listitem is hovered */ 
#menu_new li:hover > ul{ 
    display:block; 
} 

如何准确地对准顶部位置父菜单的位置?

请参考以下示例代码:http://jsfiddle.net/asovbLqd/

Result screen

+2

您可以使用http://jsfiddle.net/创建代码演示并共享吗?这将会更有帮助! – UID 2014-09-02 14:34:45

+0

我已经在小提琴中创建了代码。 http://jsfiddle.net/asovbLqd/请参阅此并帮助我。 – 2014-09-02 14:39:14

+0

找到我的答案并在 – UID 2014-09-02 14:57:33

请清洁香港URL enter link description here

#menu_new ul ul{ 
    top:0px; 
} 
#menu_new li ul li{ 
    position:relative; 
} 
+0

立即工作。谢谢。 – 2014-09-02 15:01:03

+0

@SandeepReddyK。 WC – 2014-09-02 15:04:42

编辑:还需要添加position: relative#menu_new li ul li

对于#menu_new ul ul,而不是top: auto,请使用top: 0

+1

以下工作提琴然后所有的子菜单都定位在主菜单的顶部。看到这个: 2014-09-02 14:54:26

+1

@SandeepReddyK。我想我忽略了它,因为小提琴只有第一层2条目的第3层菜单。我的错。 – flowstoneknight 2014-09-02 15:10:20

要解决的上衣全部子菜单,您需要提供“postion:relative到它的父元素”

并修复你需要的是左额外的空间从“<ul>”标签

这里删除默认填充是这两个问题的解决菲德尔“顶部位置,左侧POSI重刑“:

http://jsfiddle.net/asovbLqd/14/

我刚换了如下:

新增position: relative;到子菜单<ul>

#menu_new li ul li { 
    Text-Align: Left; 
    line-height:20px; 
    List-style-type:none; 
    border: 1px solid white; 
    position: relative; 
} 

的父元素,并从<ul>移除填充和保证金标签 #menu_new ul ul { position:absol尤特; top:0; left:150px; z-index:10; 填充:0; margin:0; }

#menu_new li ul { 
    display: none; 
    margin: 0; 
    padding: 0; 
} 

希望这会有所帮助!

+0

然后,所有子菜单都位于主菜单的顶部。看到这个:http://jsfiddle.net/asovbLqd/12/ – 2014-09-02 15:00:38

+0

感谢您更新与实际要求的小提琴! – UID 2014-09-02 15:03:18