html - 如何使菜单内容居中对齐?

标签 html css menu center

我想让菜单内容居中对齐。 我有 2 个文件:index.html 和 style.css

HTML [index.html]:

<div id="container">
<div id="menu">
  <ul>
    <li><a href="">Home</a></li>
    <li><a href="">News</a></li>
    <li><a href="">Contact</a></li>
    <li><a href="">About</a></li>
  </ul>
</div>
<div class="clear"></div>
<div id="header">
  <h1 id="logo">Lorem Ipsum</h1>
</div>
<div class="left">
  <div class="content">
    <div class="contentHeader"> What is Lorem Ipsum? </div>
    <div class="contentBody"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. </div>
    <div class="content">
      <div class="contentHeader"> Where does it come from? </div>
      <div class="contentBody"> Contrary to popular belief, Lorem Ipsum is not simply random text. </div>
    </div>
  </div>
  <div class="center">
    <div class="content">
      <div class="contentHeader"> Where do we use it? </div>
      <div class="contentBody"> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </div>
    </div>
  </div>
  <div class="right">
    <div class="content">
      <div class="contentHeader"> Where do we use it? </div>
      <div class="contentBody"> It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </div>
    </div>
  </div>
</div>

CSS [样式.css]:

ul {
    list-style-type: none;
    margin: auto;
    padding: 0;
}

a {
    text-decoration: none;
    display: block;
    width: 80px;
    height: 30px;
    padding: 10px 10px 10px 10px;
    color: white;
    font-weight: bold;
    text-align: center;
}

a:hover { background-color: #16a085; }

li {
    float: left;
    font-family: arial;
    font-size: 20px;
}

#menu {
    background-color: #1abc9c;
    height: 50px;
    width: 100%;
    position: fixed;
    z-index: 1;
    left: 0px;
    top: 0px;
}

#header {
    width: 100%;
    height: 100px;
    background-color: #2ecc71;
    position: absolute;
    border-radius: 5px;
    top: 50px;
}

#container { position: relative; }

#logo {
    font-family: arial;
    color: #3498db;
    padding-left: 20px;
}

.right {
    width: 25%;
    height: 1000px;
    background-color: #2980b9;
    border-radius: 5px;
    float: right;
    margin-top: 160px;
}

.left {
    width: 34%;
    height: 1000px;
    background-color: #f39c12;
    border-radius: 5px;
    float: left;
    margin-top: 160px;
}

.clear { clear: both; }

.content { margin: 30px 30px 30px 30px; }

.contentHeader {
    font-family: impact;
    color: #bdc3c7;
    font-size: 25px;
    margin-bottom: 20px;
}

.contentBody {
    font-family: arial;
    color: #ecf0f1;
    font-size: 15px;
    margin-bottom: 20px;
}

.center {
    width: 38%;
    height: 1000px;
    background-color: #f39c12;
    border-radius: 5px;
    margin-top: 160px;
    position: absolute;
    margin-left: 560px;
}

如何让菜单项出现在中间?它现在出现在左侧。

最佳答案

试试这个:

ul
{
list-style-type:none;
margin:auto;
padding:0;
text-align:center;

}



li
{
font-family:arial;
font-size:20px;
display:inline-block;
}

删除 float:left 并添加 text-align:centerdisplay:inline-block

Working: jsfiddle

关于html - 如何使菜单内容居中对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24550769/

相关文章:

javascript - 如何从 textarea 中获取突出显示的文本位置?

javascript - 如何在网站中制作这样的动画组件?

jquery - 如何使用 jquery 切换 li id ='' Sprite 背景

javascript - 如何在 ASP.Net 中使用 javascript 函数设置查询字符串值?

javascript - 在 http 请愿后选择 angularjs 不更新标题

javascript - 使用 javascript/jquery 将 css 链接注入(inject)头部

javascript - 尝试在 Canvas 和 javascript 上使用圆形波纹制作音频可视化

jquery - jQuery 导航链接的事件链接颜色

css - 使用文本阴影过滤器在 IE 中链接单击区域问题

ios - 使用 UIScrollView 在两个菜单之间进行转换