为什么第二个和第三个子菜单从前两个菜单的另一个位置开始扩展?我希望每个菜单都在同一个地方开始,并在同一个地方结束,宽度为95%。
<html>
<head>
<style>
// css
#menu {
width: 960px;
height: 40px;
clear: both;
}
ul#nav {
float: left;
width: 95%;
margin: 0;
padding: 0;
list-style: none;
background: #0066ff;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
ul#nav li {
display: inline;
}
ul#nav li a {
float: left;
font: bold 1.1em arial,verdana,tahoma,sans-serif;
line-height: 40px;
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #880000;
margin: 0;
padding: 0 30px;
background: #0066ff;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
/* APPLIES THE ACTIVE STATE */
ul#nav .current a, ul#nav li:hover > a {
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #330000;
background: #abcdef;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
/* THE SUBMENU LIST HIDDEN BY DEFAULT */
ul#nav ul {
display: none;
}
/* WHEN THE FIRST LEVEL MENU ITEM IS HOVERED, THE CHILD MENU APPEARS */
ul#nav li:hover > ul {
display: block;
width: 95%;
height: 45px;
position: absolute;
margin: 40px 0 0 0;
background: #abcdef;
-moz-border-radius-bottomright: 10px;
-webkit-border-bottom-right-radius: 10px;
-moz-border-radius-bottomleft: 10px;
-webkit-border-bottom-left-radius: 10px;
}
ul#nav li:hover > ul li a {
float: left;
font: bold 1.1em arial,verdana,tahoma,sans-serif;
line-height: 45px;
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #110000;
margin: 0;
padding: 0 30px 0 0;
background: #abcdef;
}
ul#nav li:hover > ul li a:hover {
color: #120000;
text-decoration: none;
text-shadow: none;
}
</style>
</head>
<body>
<div id="menu">
<ul id="nav">
<li><a href="#">Menu 1</a>
<ul>
<li><a href="#">Menu 1 Submenu item 1</a></li>
<li><a href="#">Menu 1 Submenu item 2</a></li>
<li><a href="#">Menu 1 Submenu item 3</a></li>
</ul>
</li>
<li><a href="#">Menu 2</a>
<ul>
<li><a href="#">Menu 2 Submenu item 1</a></li>
<li><a href="#">Menu 2 Submenu item 2</a>
<ul>
<li><a href="#">Menu 2 Submenu 2 item 1</a></li>
<li><a href="#">Menu 2 Submenu 2 item 2</a>
<ul>
<li><a href="#">Menu 2 Submenu 2 item 1</a></li>
<li><a href="#">Menu 2 Submenu 2 item 2</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Menu 2 Submenu item 3</a></li>
</ul>
</li>
<li><a href="#">Menu 3</a>
<ul>
<li><a href="#">Menu 3 Submenu item 1</a></li>
<li><a href="#">Menu 3 Submenu item 2</a></li>
<li><a href="#">Menu 3 Submenu item 3</a></li>
</ul>
</li>
</ul>
</div>
</body>
</html>
我试图将边距和其他值更改为,但无法找到它。你知道为什么吗?
答案 0 :(得分:0)
由于ul元素的位置是绝对的,我向它添加了左:0,这使它们全部在同一位置打开。
这是我改变的一点:
ul#nav li:hover > ul {
display: block;
left: 0; /* added to keep positioning the same */
max-width: 960px; /* set the max width of the submenu(s) */
width: 100%; /* set to 100% */
height: 45px;
position: absolute;
margin: 40px 0 0 0;
background: #abcdef;
-moz-border-radius-bottomright: 10px;
-webkit-border-bottom-right-radius: 10px;
-moz-border-radius-bottomleft: 10px;
-webkit-border-bottom-left-radius: 10px;
}
这里是jsfiddle。
修改强>:
为了让一切按照您想要的方式工作,您将不得不更改一些东西,您需要将正文中的所有边距设置为0.此外,宽度再次设置为960px如果你想将其调整得更小,则调整像素大小而不是使用百分比。
body {
margin: 0; /* to remove margins */
}
#menu {
width: 960px;
height: 40px;
clear: both;
}
ul#nav {
float: left;
width: 100%; /* this needs to be 100% if you want to adjust the size, adjust above where it says 960px */
margin: 0;
padding: 0;
list-style: none;
background: #0066ff;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
ul#nav li {
display: inline;
}
ul#nav li a {
float: left;
font: bold 1.1em arial, verdana, tahoma, sans-serif;
line-height: 40px;
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #880000;
margin: 0;
padding: 0 30px;
background: #0066ff;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
/* APPLIES THE ACTIVE STATE */
ul#nav .current a,
ul#nav li:hover > a {
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #330000;
background: #abcdef;
-moz-border-radius-topright: 10px;
-webkit-border-top-right-radius: 10px;
-moz-border-radius-topleft: 10px;
-webkit-border-top-left-radius: 10px;
}
/* THE SUBMENU LIST HIDDEN BY DEFAULT */
ul#nav ul {
display: none;
box-sizing: border-box; /* needed to help margins */
}
/* WHEN THE FIRST LEVEL MENU ITEM IS HOVERED, THE CHILD MENU APPEARS */
ul#nav li:hover > ul {
display: block;
left: 0; /* added to keep positioning the same */
max-width: 960px; /* set the max width of the submenu(s) */
width: 100%; /* set to 100% */
height: 45px;
position: absolute;
margin: 40px 0 0 0;
background: #abcdef;
-moz-border-radius-bottomright: 10px;
-webkit-border-bottom-right-radius: 10px;
-moz-border-radius-bottomleft: 10px;
-webkit-border-bottom-left-radius: 10px;
}
ul#nav li:hover > ul li a {
float: left;
font: bold 1.1em arial, verdana, tahoma, sans-serif;
line-height: 45px;
color: #fff;
text-decoration: none;
text-shadow: 1px 1px 1px #110000;
margin: 0;
padding: 0 30px 0 0;
background: #abcdef;
}
ul#nav li:hover > ul li a:hover {
color: #120000;
text-decoration: none;
text-shadow: none;
}
此处是更新后的fiddle更改。
答案 1 :(得分:0)
这是您需要添加/更新的CSS:
body {
margin: 0;
}
ul#nav {
width: 100%;
}
ul#nav ul {
box-sizing: border-box;
}
ul#nav li:hover > ul {
left: 0;
max-width: 960px;
width: 100%;
}