我不能让ul以div元素为中心
<div class="top-menu">
<ul class="topnav dropdown" a>
<li><a href="#">Home</a></li>
<li>
<a href="#">Tutorials</a>
<ul class="subnav">
<li>
<a href="#">Sub Nav Link</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li>
<a href="#">Resources</a>
<ul class="subnav">
<li><a href="#">Sub Nav Link</a></li>
<li><a href="#">Sub Nav Link</a></li>
</ul>
</li>
<li><a href="#">About Us</a></li>
<li><a href="#">Advertise</a></li>
<li><a href="#">Submit</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
和css风格
.top-menu{
width:100%;
text-align:center;
}
ul.topnav {
border:1px solid black;
list-style: none;
padding: 0 20px;
margin: auto;
width: 100%;
background: #222;
background: url(topnav_bg.gif) repeat-x;
}
ul.topnav li {
float: left;
margin: 0;
padding: 0 15px 0 0;
position: relative; /*--Declare X and Y axis base for sub navigation--*/
display: inline;
}
有什么不对? 小提琴在这里http://jsfiddle.net/m82LK/2/,谢谢
答案 0 :(得分:1)
请参阅 FIDDLE
CSS:
.top-menu {
width:100%;
text-align:center;
}
ul.topnav {
border:1px solid black;
list-style: none;
padding: 0 20px;
margin: auto;
width: 100%;
background: #222;
background: url(topnav_bg.gif) repeat-x;
}
ul.topnav li {
display:inline-block;
margin: 0;
padding: 0 15px 0 0;
position: relative;
/*--Declare X and Y axis base for sub navigation--*/
}
ul.topnav li a {
padding: 10px 5px;
color: #aaaaaa;
display: block;
text-decoration: none;
}
ul.topnav li a:hover {
background: url(topnav_hover.gif) no-repeat center top;
}
ul.topnav li span {
/*--Drop down trigger styles--*/
width: 17px;
height: 35px;
float: left;
background: url(subnav_btn.gif) no-repeat center top;
}
ul.topnav li span.subhover {
background-position: center bottom;
cursor: pointer;
}
/*--Hover effect for trigger--*/
ul.topnav li ul.subnav {
list-style: none;
position: absolute;
/*--Important - Keeps subnav from affecting main navigation flow--*/
left: 0;
top: 35px;
background: #333;
margin: 0;
padding: 0;
display: none;
float: left;
width: 170px;
border: 1px solid #111;
}
ul.topnav li ul.subnav li {
margin: 0;
padding: 0;
border-top: 1px solid #252525;
/*--Create bevel effect--*/
border-bottom: 1px solid #444;
/*--Create bevel effect--*/
clear: both;
width: 170px;
}
html ul.topnav li ul.subnav li a {
float: left;
width: 145px;
background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
padding-left: 20px;
}
html ul.topnav li ul.subnav li a:hover {
/*--Hover effect for subnav links--*/
background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}
答案 1 :(得分:1)
将ul.topnav
替换为:
ul.topnav {
border:1px solid black;
list-style: none;
padding: 0 20px;
background: url(topnav_bg.gif) repeat-x; // or #222 based on ur requirement
display: inline-block;
}
答案 2 :(得分:0)
删除你的float: left
并使用inline-block
。将text-align: center
添加到您的ul
请参阅更新的小提琴:http://jsfiddle.net/m82LK/3/
答案 3 :(得分:0)
修改:这有效。我还添加了box-sizing:border-box,这样UL上的填充不会影响它的宽度(在它被推离屏幕右侧之前)。这是一个更新的小提琴http://jsfiddle.net/m82LK/11/
* {
box-sizing:border-box;
-webkit-box-sizing:border-box;
-moz-box-sizing:border-box;
}
.top-menu{
width:100%;
text-align:center;
}
ul.topnav {
border:1px solid black;
list-style: none;
padding: 0 20px;
margin: auto;
width: 100%;
background: #222;
background: url(topnav_bg.gif) repeat-x;
}
ul.topnav li {
display:inline-block;
margin: 0;
padding: 0 15px 0 0;
position: relative; /*--Declare X and Y axis base for sub navigation--*/
}
ul.topnav li a{
padding: 10px 5px;
color: #aaaaaa;
display: block;
text-decoration: none;
display:inline-block;
}
ul.topnav li a:hover{
background: url(topnav_hover.gif) no-repeat center top;
}
ul.topnav li span { /*--Drop down trigger styles--*/
width: 17px;
height: 35px;
display:inline-block;
background: url(subnav_btn.gif) no-repeat center top;
}
ul.topnav li span.subhover {background-position: center bottom; cursor: pointer;} /*--Hover effect for trigger--*/
ul.topnav li ul.subnav {
list-style: none;
position: absolute; /*--Important - Keeps subnav from affecting main navigation flow--*/
left: 0; top: 35px;
background: #333;
margin: 0; padding: 0;
display: none;
float:left;
width: 170px;
border: 1px solid #111;
}
ul.topnav li ul.subnav li{
margin: 0; padding: 0;
border-top: 1px solid #252525; /*--Create bevel effect--*/
border-bottom: 1px solid #444; /*--Create bevel effect--*/
clear: both;
width: 170px;
}
html ul.topnav li ul.subnav li a {
display:inline-block;
width: 145px;
background: #333 url(dropdown_linkbg.gif) no-repeat 10px center;
padding-left: 20px;
}
html ul.topnav li ul.subnav li a:hover { /*--Hover effect for subnav links--*/
background: #222 url(dropdown_linkbg.gif) no-repeat 10px center;
}