首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使菜单内容中心对齐?

如何使菜单内容中心对齐?
EN

Stack Overflow用户
提问于 2014-07-03 10:08:00
回答 5查看 104关注 0票数 0

我想使菜单内容中心对齐。我有两个文件: index.html和style.css

index.html

代码语言:javascript
复制
<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 style.css

代码语言:javascript
复制
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;
}

如何使菜单项出现在中心?现在它出现在左边。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-07-03 10:19:53

试试这个:

代码语言:javascript
复制
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

工作:摇摆不定

票数 1
EN

Stack Overflow用户

发布于 2014-07-03 10:22:50

解决方案-1

将这两个属性添加到css中。

小提琴演示

代码语言:javascript
复制
#menu
{
text-align:center;
}

ul{
display:inline-block;
}

解决方案-2

将此属性添加到ul中:

小提琴演示

代码语言:javascript
复制
ul{
display:table;
}
票数 1
EN

Stack Overflow用户

发布于 2014-07-03 10:14:41

如果给ul div中的menu指定一个固定宽度,则可以应用margin:0 auto;

HTML:

代码语言:javascript
复制
<div id="menu">
    <ul class="centerMargin">

CSS

代码语言:javascript
复制
.centerMargin
{
    width:450px;
    margin:0 auto;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24550769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档