我一直在尝试创建一个可以在页面中间修复的菜单。
菜单宽度为1170px
,高度为30px
,底部为蓝线。
现在....我想把它作为一个位于页面中间的菜单,其中包含页面上的其他项目。
对齐:中心似乎不起作用。我做错了什么?
<div align="center;" id="menu">
<ul>
<li><a href="#">Nuestros Cursos</a>
<ul>
<li><a href="#">Spanish</a></li>
<li><a href="#">English</a></li>
<li><a href="#">French</a></li>
<li><a href="#">German</a></li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li><a href="#">Preparación de exámenes</a>
<ul>
<li><a href="#">Spanish</a></li>
<li><a href="#">English</a></li>
<li><a href="#">French</a></li>
<li><a href="#">German</a></li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li><a href="#">Estudiar en el Extranjero</a>
<ul>
<li><a href="#">Spanish</a></li>
<li><a href="#">English</a></li>
<li><a href="#">French</a></li>
<li><a href="#">German</a></li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li><a href="#">Quienes somos</a>
<ul>
<li><a href="#">Spanish</a></li>
<li><a href="#">English</a></li>
<li><a href="#">French</a></li>
<li><a href="#">German</a></li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li><a href="#">Trabaja con nosotros</a>
<ul>
<li><a href="#">Spanish</a></li>
<li><a href="#">English</a></li>
<li><a href="#">French</a></li>
<li><a href="#">German</a></li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
</div>
CSS
/*-----------------------new menu I am working on------------------------*/
#menu {
width:1170px;
height:30px;
text-align:center;
margin-left: auto; /*margin left es right fontosak olyan szinten hogy amit meretnek valasztok a menure...azt szepent automatikusan kozepre helyezi*/
margin-right: auto;
border-bottom:solid 3px #289dcb; /*#68bc1d szep zold szint jelent*/
border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
}
#menu ul {
margin:0;
padding:0;
line-height:30px;
}
#menu li {
margin:0;
padding:0;
list-style:none;
float:left;
position:relative;
background:#3b3b3b; /*colours the menu and also the submenu background*/
}
#menu ul li a {
display:block;
height:30px;
width:200px;
text-align:center;
font-family:Arial, Helvetica, sans-serif;
text-decoration:none;
color: #FFF; /*colour of the text in the menu*/
border:1px solid #000;
}
#menu ul ul {
position: absolute; /* this is what orders the nested links to appear in a block under the main ul*/
visibility:hidden;
top:32px;
}
#menu ul li:hover ul { /* this is what makes the dropdown menu appear on hovering over it*/
visibility:visible;
}
#menu li:hover {
background:#ff5454; /* main menu box changes to this color (also the submenus) on hovering over it, red in this case*/
}
这是它的样子: Demo js Fiddle
答案 0 :(得分:2)
答案 1 :(得分:1)
您实际上需要修改HTML和CSS:
关键点:
您正在向#menu
添加连续列表,其中您只需要一个,每个菜单项都有子li
text-align:center
需要添加到#menu
和display:inline-block
添加到#menu ul
才能实现居中
HTML
<div id="menu">
<ul>
<li><a href="#">Nuestros Cursos</a>
<ul>
<li><a href="#">Spanish</a>
</li>
<li><a href="#">English</a>
</li>
<li><a href="#">French</a>
</li>
<li><a href="#">German</a>
</li>
</ul>
</li>
<li><a href="#">Preparación de exámenes</a>
<ul>
<li><a href="#">Spanish</a>
</li>
<li><a href="#">English</a>
</li>
<li><a href="#">French</a>
</li>
<li><a href="#">German</a>
</li>
</ul>
</li>
<li><a href="#">Estudiar en el Extranjero</a>
<ul>
<li><a href="#">Spanish</a>
</li>
<li><a href="#">English</a>
</li>
<li><a href="#">French</a>
</li>
<li><a href="#">German</a>
</li>
</ul>
</li>
<li><a href="#">Quienes somos</a>
<ul>
<li><a href="#">Spanish</a>
</li>
<li><a href="#">English</a>
</li>
<li><a href="#">French</a>
</li>
<li><a href="#">German</a>
</li>
</ul>
</li>
<li><a href="#">Trabaja con nosotros</a>
<ul>
<li><a href="#">Spanish</a>
</li>
<li><a href="#">English</a>
</li>
<li><a href="#">French</a>
</li>
<li><a href="#">German</a>
</li>
</ul>
</li>
</ul>
</div>
CSS
#menu {
text-align: center;
}
#menu ul {
height: 30px;
display:inline-block;
margin: 0;
position:relative;
padding: 0;
line-height: 30px;
border-bottom: solid 3px #289dcb;
/*#68bc1d szep zold szint jelent*/
;
}
#menu li {
margin: 0;
padding: 0;
list-style: none;
float: left;
position: relative;
background: #3b3b3b;
/*colours the menu and also the submenu background*/
;
}
#menu a {
display: block;
height: 30px;
width: 200px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: #FFF;
/*colour of the text in the menu*/
border: 1px solid #000;
}
#menu ul ul {
position: absolute;
/* this is what orders the nested links to appear in a block under the main ul*/
visibility: hidden;
top: 32px;
left:0;
}
#menu ul li:hover ul {
/* this is what makes the dropdown menu appear on hovering over it*/
visibility: visible;
}
#menu li:hover {
background: #ff5454;
/* main menu box changes to this color (also the submenus) on hovering over it, red in this case*/
;
}
答案 2 :(得分:1)
这个CSS可以解决问题。 DEMO
我没有使用display: inline-block
因为它会给你每个<li>
之间的差距。
相反,我使用了display: table
和display: table-cell
来使其正确对齐,而且列表之间没有任何空白。
#menu {
width: 1170px;
height: 30px;
margin-left: auto;
margin-right: auto;
border-bottom: solid 3px #289dcb;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
display: table;
}
#menu ul {
margin: auto;
padding: 0;
line-height: 30px;
display: table-cell;
}
#menu li {
margin: 0;
padding: 0;
list-style: none;
float: left;
position: relative;
background: #3b3b3b;
width: 100%;
}
#menu ul li a {
display: block;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: #FFF;
border: 1px solid #000;
}
答案 3 :(得分:0)
替换给定的css并尝试,希望它能正常工作
#menu {
width:1170px;
height:30px;
text-align:center;
margin-left: auto; /*margin left es right fontosak olyan szinten hogy amit meretnek valasztok a menure...azt szepent automatikusan kozepre helyezi*/
margin-right: auto;
border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
}
#menu ul {
margin:0;
padding:0;
line-height:30px;
display:inline-block;
border-bottom:solid 3px #289dcb; /*#68bc1d szep zold szint jelent*/
}