如何使导航菜单居中?
我使用以下代码来创建布局,问题是,导航菜单不会居中,文本居中,但导航栏的其余部分不会居中。
这是我的 HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="container">
<h1>Test</h1>
<ul id="navigation">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Contact</a></li>
</ul>
<div id="content">
</div>
<div id="footer">
<p>Test</p>
</div>
</div>
</body>
</html>
这是我的 CSS:
/***** Body *****/
body {
font-family: arial, helvetica;
text-align:center;
}
div#container {
}
/***** Navigation Menu *****/
ul#navigation {
margin: 20px;
padding: 0;
list-style: none;
width: 525px;
}
ul#navigation li {
display: inline;
}
ul#navigation a {
text-decoration:none;
padding: 5px 0;
width: 100px;
background: #485e49;
color: #eee;
float: left;
text-align:center;
}
ul#navigation a:hover {
background: #FF00FF;
text-align:left;
}
ul#navigation a:active {
text-align:right;
}
I am using the following codes to create a layout, the issue is, the navigation menu won't center, the text centers, but the rest of the navigation bar doesn't.
Here is my HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="container">
<h1>Test</h1>
<ul id="navigation">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Contact</a></li>
</ul>
<div id="content">
</div>
<div id="footer">
<p>Test</p>
</div>
</div>
</body>
</html>
And here is my CSS:
/***** Body *****/
body {
font-family: arial, helvetica;
text-align:center;
}
div#container {
}
/***** Navigation Menu *****/
ul#navigation {
margin: 20px;
padding: 0;
list-style: none;
width: 525px;
}
ul#navigation li {
display: inline;
}
ul#navigation a {
text-decoration:none;
padding: 5px 0;
width: 100px;
background: #485e49;
color: #eee;
float: left;
text-align:center;
}
ul#navigation a:hover {
background: #FF00FF;
text-align:left;
}
ul#navigation a:active {
text-align:right;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的
ul#navigation
更改为示例:http://jsfiddle.net/jasongennaro/3WS7B /
发生的事情是你的
width
关闭了。一旦固定为400px
,应用margin:0 auto
就可以了。Change your
ul#navigation
toExample: http://jsfiddle.net/jasongennaro/3WS7B/
What happened is your
width
was off. Once that was fixed to400px
, applyingmargin:0 auto
worked.您可以将其添加到
ul#navigation
css 中以使其居中:或者:
You can add this to the
ul#navigation
css to center it:Alternatively: