简体   繁体   English

当鼠标悬停在CSS上时,为什么我的第一个子菜单没有出现?

[英]Why does my first sub-menu not appear when the mouse is hovering over it in CSS?

Can someone help me with this problem I'm having please? 有人可以帮我解决这个问题吗? Why does my first sub-menu not appear when the mouse is hovering over it in CSS? 当鼠标悬停在CSS上时,为什么我的第一个子菜单没有出现?

#Menu {
position: absolute;
/*Sets a space allocated between the written word and the edge of the browser.*/
margin:2cm; /*4cm 3cm 4cm;*/
/* Padding moves the links according to the x and y axis*/
padding: 10px 300px;
}

/* This sets the style for all list <li> tags in the HTML code.*/

#Menu li {
/* This sets the style of the bullet points on the list e.g. Roman numerals, circular, square etc. */
list-style: none;
/*This sets the radius of the corners of the background box of the links.*/
/*border-radius: 50px;*/
/*This sets the colour of the background box containing the links.*/
background: #0FF;
}

/* This rule controls the links within the <li> tags.*/

#Menu li a {
/* This tag separates any item/object and tries it like a new paragraph. */
display: block;
/* Padding moves the links according to the x and y axis*/
padding: 3px 8px;
/* This sets all text within its range as all capitals. Other options are none, capitalize 
(which makes the first letter of every word a capital), lowercase and initial 
(which makes the first letter of a word at the beginning of sentence capital and all the rest normal).*/
text-transform: uppercase;
/*This can set the text as underlined, overline, line-through, none, initial and inherit.*/
text-decoration: none; 
/* Sets the default colour of the text.*/
color: #999;
font-weight: bold;
}

#Menu li a:hover {

/*Sets the default colour of the text when the mouse is hovering to get the sub-menu.*/
color: #000;
}

/*This rule hides the <li> from the effects of <ul> unordered list tag. If not implemented the sub-menu will be on constant display.*/

#Menu li ul {
display: none;
}

/*This rule allows the hovering effect to display the sub-menu.*/

#Menu li:hover ul, #Menu li.hover ul {
/*position : fixed will position the element relative to the browser, 
relative means to position relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position,
absolute is positioned relative to its first positioned (not static) ancestor element.*/
position: absolute;
/*display: inline will make the sub-menu appear like a queue of words for example like the way the words of this comment are all queued up.*/
display: inline;
/*Without zeroing the left parameter then the sub-menu would not be directly underneath the main menu headlining. It would of been somewhere to the 
right.*/
left: 0;
/*The width property sets the width of an element. In this case the sub-menu.*/
width: 100%;
/*Sets a space allocated between the written word and the edge of the browser.*/
margin: 0;
/* Padding moves the links according to the x and y axis*/
padding: 10px 300px;
}

#Menu li:hover li, #Menu li.hover li {
/*The float property specifies whether or not an element should float. Without the float it looks like a ordered list without the numbers.*/
float: left;
}

#Menu li:hover li a, #Menu li.hover li a {
/*Sets the default colour of the text when the mouse is hovering to get the sub-menu.*/
color: #000;
}

#Menu li li a:hover {
/*This applies hover colour to the sub-menu.*/
color: #357;
}


#Menu2 {
position: absolute;
/*Sets a space allocated between the written word and the edge of the browser.*/
margin:2cm; /*4cm 3cm 4cm;*/
/* Padding moves the links according to the x and y axis*/
padding: 10px 500px;
}

/* This sets the style for all list <li> tags in the HTML code.*/

#Menu2 li {
/* This sets the style of the bullet points on the list e.g. Roman numerals, circular, square etc. */
list-style: none;
/*This sets the radius of the corners of the background box of the links.*/
/*border-radius: 50px;*/
/*This sets the colour of the background box containing the links.*/
background: #0FF;
}

/* This rule controls the links within the <li> tags.*/

#Menu2 li a {
/* This tag separates any item/object and tries it like a new paragraph. */
display: block;
/* Padding moves the links according to the x and y axis*/
padding: 3px 8px;
/* This sets all text within its range as all capitals. Other options are none, capitalize 
(which makes the first letter of every word a capital), lowercase and initial 
(which makes the first letter of a word at the beginning of sentence capital and all the rest normal).*/
text-transform: uppercase;
/*This can set the text as underlined, overline, line-through, none, initial and inherit.*/
text-decoration: none; 
/* Sets the default colour of the text.*/
color: #999;
font-weight: bold;
}

#Menu2 li a:hover {

/*Sets the default colour of the text when the mouse is hovering to get the sub-menu.*/
color: #000;
}

/*This rule hides the <li> from the effects of <ul> unordered list tag. If not implemented the sub-menu will be on constant display.*/

#Menu2 li ul {
display: none;
}

/*This rule allows the hovering effect to display the sub-menu.*/

#Menu2 li:hover ul, #Menu2 li.hover ul {
/*position : fixed will position the element relative to the browser, 
relative means to position relative to its normal position, so "left:20" adds 20 pixels to the element's LEFT position,
absolute is positioned relative to its first positioned (not static) ancestor element.*/
position: absolute;
/*display: inline will make the sub-menu appear like a queue of words for example like the way the words of this comment are all queued up.*/
display: inline;
/*Without zeroing the left parameter then the sub-menu would not be directly underneath the main menu headlining. It would of been somewhere to the 
right.*/
left: 0;
/*The width property sets the width of an element. In this case the sub-menu.*/
width: 100%;
/*Sets a space allocated between the written word and the edge of the browser.*/
margin: 0;
/* Padding moves the links according to the x and y axis*/
padding: 10px 500px;
}

#Menu2 li:hover li, #Menu2 li.hover li {
/*The float property specifies whether or not an element should float. Without the float it looks like a ordered list without the numbers.*/
float: left;
}

#Menu2 li:hover li a, #Menu2 li.hover li a {
/*Sets the default colour of the text when the mouse is hovering to get the sub-menu.*/
color: #000;
}

#Menu2 li li a:hover {
/*This applies hover colour to the sub-menu.*/
color: #357;
}

As you can see the second sub-menu does appear when the mouse is hovering. 如您所见,当鼠标悬停时,第二个子菜单确实出现。 Thank you for your help in that matter. 多谢您的协助。

It is because the second menu is atop the first one. 这是因为第二个菜单位于第一个菜单的顶部。

This is because you use padding to position absolute positioned menus. 这是因为您使用padding来定位absolute定位的菜单。 You get something like this: 您得到的是这样的:

在此处输入图片说明

Here MENU 2 is above MENU 1 because both are positioned absolute, placed at the "same spot" in the document and MENU 2 looks like it is further to the right due to the padding. 此处MENU 2位于MENU 1上方,因为两者均位于文件的“相同位置” ,并且位置绝对,并且由于填充, MENU 2 看起来更靠右。

If you add a border around the menus, or use Inspect element in your browser, you should most likely be given a more clear picture of what is happening. 如果在菜单周围添加边框,或在浏览器中使用Inspect元素 ,则很可能会更清楚地了解正在发生的事情。

MENU 2 is also above MENU 1 , and not the other way around, because you have the second menu after the first in the document (DOM) – and you have not set z-index . 菜单2也在菜单1上方,而不是菜单1上方,因为文档(DOM)中的第二个菜单位于第二个菜单之后–并且您尚未设置z-index


From your CSS: 从您的CSS:

#Menu {
    position : absolute;
    margin   : 2cm;
    padding  : 10px 300px;
}

#Menu2 {
    position : absolute;
    margin   : 2cm;
    padding  : 10px 500px;
}

A more complete example with HTML markup would make it easier to help. 带有HTML标记的更完整的示例将使其更易于帮助。 (As would a lessened amount of comments in the CSS.) (减少CSS中的注释数量。)

For a quick fix this should make it work, though I would most likely have used a different approach: 为了快速解决此问题,尽管我很可能会使用其他方法,但这应该可以使其工作:

#Menu {
   z-index  : 200;
}

#Menu2 {
   z-index  : 100;
}

You also have a lot of redundant styling. 您也有很多多余的样式。 Instead of attaching everything to ID's you should make use of class names and only separate out what differs in the styling between the menus. 而不是将所有内容附加到ID,您应该使用类名,并且仅将菜单之间样式上的区别分开。 Giving you something like this in your markup: 在标记中给您这样的内容:

<ul id="menu_1" class="menu">
  ...

<ul id="menu_2" class="menu">
  ...

And in your CSS, for the common styles: 在您的CSS中,常见样式如下:

.menu {
    position        : absolute;
    margin          : 10px;
}
.menu li {
    list-style      : none;
    background      : #0FF;
}
.menu li a {
    display         : block;
    padding         : 3px 8px;

... and so on.

Then for each individual menu: 然后针对每个单独的菜单:

#menu_1 {
    left: 10px;
}

#menu_2 {
    left: 100px;
}

... etc.

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

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