Html 为什么我不能以 margin: 0 auto 居中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/963636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 23:51:50  来源:igfitidea点击:

Why can't I center with margin: 0 auto?

htmlcssmargin

提问by zeckdude

I have a #headerdiv that is 100% widthand within that div I have an unordered list. I have applied margin: 0 autoto the unordered list but it won't center it within the header div.

我有一个#headerdiv,100% width在那个 div 中我有一个无序列表。我已经申请margin: 0 auto了无序列表,但它不会在标题 div 中居中。

Can anybody please tell me why? I thought that if I define the width of the parent div, then the unordered list should be able to center itself with margin: 0 auto. What am I missing?

谁能告诉我为什么?我认为如果我定义父 div 的宽度,那么无序列表应该能够以margin: 0 auto. 我错过了什么?

Here is my code:

这是我的代码:

<style>

* {
    margin: 0;
    padding: 0;
}

#header {
    width: 100%;    
    background-color: #333;
    min-height: 160px;
    font-family:Arial, Helvetica, sans-serif;
}

#sitename {
    font-size: 50px;
    width: 620px;
    margin:0 auto;
    padding-top: 35px;
    color:#999;
}

#header ul {
    float: right;
    margin: 0 auto;
}

#header ul li {
    float: left;
    padding-right: 20px;
    list-style-type: none;
    font-size: 20px;
}

</style>

</head>

<body>

<div id="header">
    <h1 id="sitename">Photography Auction Site</h1>

    <ul>
        <li>List of Photos</li>
        <li>Image Gallery</li>
        <li>Contact Us</li>
    </ul>
</div>

</body>
</html>

回答by PatrikAkerstrand

You need to define the width of the element you are centering, not the parent element.

您需要定义要居中的元素的宽度,而不是父元素的宽度。

#header ul {
    margin: 0 auto;
    width: 90%;
}

Edit: Ok, I've seen the testpage now, and here is how I think you want it:

编辑:好的,我现在已经看到了测试页,这是我认为你想要的:

#header ul {
    list-style:none;
    margin:0 auto;
    width:90%;
}

/* Remove the float: left; property, it interferes with display: inline and 
 * causes problems. (float: left; makes the element implicitly a block-level
 * element. It is still good to use display: inline on it to overcome a bug
 * in IE6 and below that doubles horizontal margins for floated elements)
 * The styles below is the full style for the list-items. 
 */
#header ul li {
    color:#CCCCCC;
    display:inline;
    font-size:20px;
    padding-right:20px;
}

回答by Hoeks

An inline-block covers the whole line (from left to right), so a margin left and/or right won't work here. What you need is a block, a block has borders on the left and the right so can be influenced by margins.

内联块覆盖整行(从左到右),因此左边距和/或右边距在这里不起作用。你需要的是一个块,一个块的左右都有边界,所以可能会受到边距的影响。

This is how it works for me:

这对我来说是这样的:

#content {
display: block;
margin: 0 auto;
}

回答by kalys.osmonov

Why not?

为什么不?

#header {
    text-align: center;
}

#header ul {
    display: inline;
}

回答by bill dou

I don't know why the first answer is the best one, I tried it and not working in fact, as @kalys.osmonov said, you can give text-align:centerto header, but you have to make ulas inline-blockrather than inline, and also you have to notice that text-aligncan be inherited which is not good to some degree, so the better way (not working below IE 9) is using marginand transform. Just remove float rightand margin;0 autofrom ul, like below:

我不知道为什么第一个答案是最好的,我尝试过了,其实不是工作,如@ kalys.osmonov说,你可以给text-align:centerheader,但你必须做ulinline-block,而不是inline和你也必须通知其text-align可以继承,这是不好的一定程度,以便更好的方式(不工作下面IE 9)正在使用margintransform。只需删除float rightmargin;0 autofrom ul,如下所示:

#header ul {
   /* float: right; */
   /* margin: 0 auto; */
   display: inline-block;
   margin-left: 50%; /* From parent width */
   transform: translateX(-50%); /* use self width which can be unknown */
  -ms-transform: translateX(-50%); /* For IE9 */
}

This way can fix the problem that making dynamic width of ulcenter if you don't care IE8 etc.

ul如果你不关心IE8等,这种方式可以解决动态中心宽度的问题。

回答by Vani S

We can set the width for ul tag then it will align center.

我们可以设置 ul 标签的宽度,然后它会居中对齐。

#header ul {
    display: block;
    margin: 0 auto;
    width: 420px;
    max-width: 100%;
}