So I have the following CSS in place to display a horizontal navigation bar using:
因此,我有以下CSS显示水平导航栏使用:
.navigation ul {
list-style: none;
padding: 0;
margin: 0;
}
.navigation li {
float: left;
margin: 0 1.15em;
/* margin: 0 auto;*/
}
.navigation {
/* width: auto;*/
/* margin: 0 auto;*/
text-align: center;
}
My question is: how do I align the navigation bar centrally above the title?
我的问题是:如何在标题上方居中对齐导航条?
10 个解决方案
#1
20
Give your .navigation ul a width and use margin:0 auto;
给你的。navigation ul一个宽度和使用裕度:0自动;
.navigation ul
{
list-style: none;
padding: 0;
width: 400px;
margin: 0 auto;
}
#3
13
Well, to use margin:0 auto
on something, it must have a defined width. Probably the best workaround is:
要使用margin:0 auto,它必须有一个定义的宽度。也许最好的解决办法是:
ul li {
display: inline;
list-style-type: none;
}
ul {
text-align:center;
}
#4
5
There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.
有很少的设置,如浮动,空白可能会影响这段代码正常工作。它也适用于IE7。这段代码来自CSS wizard dry上的一篇文章。
.navigation ul
{
list-style: none;
padding: 0;
margin: 0;
text-align: center;/*make this change*/
}
.navigation li
{
float: none;/*make this change*/
display:inline;/*make this change*/
margin: 0 1.15em;
/* margin: 0 auto; */
}
.navigation li a {
display:inline-block;/*make this change*/
}
.navigation
{
/*width: auto;*/
/*margin: 0 auto;*/
text-align: center;
}
#5
2
You could set the <li>
's to be display: inline
, then set text-align: center
on the <ul>
. Doing that, you can remove the float: left
from the list items and you don't need to have a fixed width for the navigation bar as you would if you used margin: 0 auto
.
您可以将
-
。这样做,您可以从列表项中删除float: left,并且您不需要为导航条设置一个固定的宽度,如果您使用的是margin: 0 auto的话。
<html>
<head>
<style>
ul {
list-style: none;
text-align: center;
}
li {
display: inline;
margin: 0 1.15em;
}
</style>
</head>
<body>
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</body>
</html>
#6
2
This one works great with me! (if I'm correct: IE7+)
这个对我来说很棒!(如果我是正确的:IE7 +)
Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/
小提琴:http://jsfiddle.net/fourroses666/zj8sav9q/3/
.nav{list-style:none; text-align:center;}
.nav ul{list-style:none;}
.nav li{display:inline;}
.nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
<nav class="nav" role="navigation" aria-label="main navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu</a></li>
<li><a href="#">Onze producten</a></li>
<li><a href="#">Impressie</a></li>
<li><a href="#">Media</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
#7
1
ul {
display: inline-block;
text-align: center;
}
#8
0
.navigation ul
{
list-style-type: none;
padding: 0px;
width: 200px;
margin: 0 auto;
}
#9
0
If you want to keep using your floated LI in your code, you can use this:
如果你想继续在你的代码中使用浮动LI,你可以使用这个:
JSFiddle: https://jsfiddle.net/Lvujttw3/
JSFiddle:https://jsfiddle.net/Lvujttw3/
<style>
.wrapper {
width: 100%;
background-color:#80B5EB;
text-align: center;
}
.intWrapper {
display: inline-block;
}
.mainMenu {
padding: 0;
min-height: 40px;
margin:auto;
}
ul {
list-style-type: none;
}
ul li {
float: left;
font-size: 15px;
line-height: 40px;
}
ul li A {
display: block;
color: white;
font-weight: bold;
font-family: Arial;
text-decoration: none;
min-height: 40px;
padding: 0 36px;
}
</style>
<div class="wrapper">
<div class="intWrapper">
<ul class="mainMenu">
<li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
</li><li><a href="two.htm">ITEM TWO</a>
</li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>
</li>
</ul></div>
</div>
</div>
#10
-1
style="position: absolute; z-index: 1;"
#1
20
Give your .navigation ul a width and use margin:0 auto;
给你的。navigation ul一个宽度和使用裕度:0自动;
.navigation ul
{
list-style: none;
padding: 0;
width: 400px;
margin: 0 auto;
}
#2
#3
13
Well, to use margin:0 auto
on something, it must have a defined width. Probably the best workaround is:
要使用margin:0 auto,它必须有一个定义的宽度。也许最好的解决办法是:
ul li {
display: inline;
list-style-type: none;
}
ul {
text-align:center;
}
#4
5
There are few settings like float, margin which may affect this code to work properly. It works in IE7 too. I got this code from an article over at CSS Wizardry.
有很少的设置,如浮动,空白可能会影响这段代码正常工作。它也适用于IE7。这段代码来自CSS wizard dry上的一篇文章。
.navigation ul
{
list-style: none;
padding: 0;
margin: 0;
text-align: center;/*make this change*/
}
.navigation li
{
float: none;/*make this change*/
display:inline;/*make this change*/
margin: 0 1.15em;
/* margin: 0 auto; */
}
.navigation li a {
display:inline-block;/*make this change*/
}
.navigation
{
/*width: auto;*/
/*margin: 0 auto;*/
text-align: center;
}
#5
2
You could set the <li>
's to be display: inline
, then set text-align: center
on the <ul>
. Doing that, you can remove the float: left
from the list items and you don't need to have a fixed width for the navigation bar as you would if you used margin: 0 auto
.
您可以将
-
。这样做,您可以从列表项中删除float: left,并且您不需要为导航条设置一个固定的宽度,如果您使用的是margin: 0 auto的话。
<html>
<head>
<style>
ul {
list-style: none;
text-align: center;
}
li {
display: inline;
margin: 0 1.15em;
}
</style>
</head>
<body>
<ul>
<li>Option 1</li>
<li>Option 2</li>
<li>Option 3</li>
</ul>
</body>
</html>
#6
2
This one works great with me! (if I'm correct: IE7+)
这个对我来说很棒!(如果我是正确的:IE7 +)
Fiddle: http://jsfiddle.net/fourroses666/zj8sav9q/3/
小提琴:http://jsfiddle.net/fourroses666/zj8sav9q/3/
.nav{list-style:none; text-align:center;}
.nav ul{list-style:none;}
.nav li{display:inline;}
.nav a{text-decoration:none; font-size:20px; line-height:20px; display:inline-block;}
<nav class="nav" role="navigation" aria-label="main navigation">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Menu</a></li>
<li><a href="#">Onze producten</a></li>
<li><a href="#">Impressie</a></li>
<li><a href="#">Media</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
#7
1
ul {
display: inline-block;
text-align: center;
}
#8
0
.navigation ul
{
list-style-type: none;
padding: 0px;
width: 200px;
margin: 0 auto;
}
#9
0
If you want to keep using your floated LI in your code, you can use this:
如果你想继续在你的代码中使用浮动LI,你可以使用这个:
JSFiddle: https://jsfiddle.net/Lvujttw3/
JSFiddle:https://jsfiddle.net/Lvujttw3/
<style>
.wrapper {
width: 100%;
background-color:#80B5EB;
text-align: center;
}
.intWrapper {
display: inline-block;
}
.mainMenu {
padding: 0;
min-height: 40px;
margin:auto;
}
ul {
list-style-type: none;
}
ul li {
float: left;
font-size: 15px;
line-height: 40px;
}
ul li A {
display: block;
color: white;
font-weight: bold;
font-family: Arial;
text-decoration: none;
min-height: 40px;
padding: 0 36px;
}
</style>
<div class="wrapper">
<div class="intWrapper">
<ul class="mainMenu">
<li><a href="one.htm" style='background-color:red'>ITEM ONE</a>
</li><li><a href="two.htm">ITEM TWO</a>
</li><li><a href="three.htm" style='background-color:red'>ITEM THREE</a>
</li>
</ul></div>
</div>
</div>
#10
-1
style="position: absolute; z-index: 1;"