Html 将元素水平放置在页面中间

Html 将元素水平放置在页面中间,html,css,Html,Css,我想知道如何将元素水平居中放置在页面中间 这是我的密码: #item { height:400px; width:700px; background-color:white; -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; } 我想把这个放在中间,这样看起来: (水平居中,标题下方几个像素) 谢谢,M4DNE55水平方向很容易,垂直方向很麻烦

我想知道如何将元素水平居中放置在页面中间

这是我的密码:

#item {
    height:400px;
    width:700px;
    background-color:white;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
}
我想把这个放在中间,这样看起来:

(水平居中,标题下方几个像素)


谢谢,M4DNE55水平方向很容易,垂直方向很麻烦。 要使文本和图像等内嵌元素居中,请使用
text align:center


要使div居中,可以使用
margin:0 auto

一般来说,要将某物居中,请使用

margin:0px auto;
这相当于

margin-top:0px
margin-right:auto;
margin-bottom:0px
margin-left:auto;

您应该选择以下内容:

margin 30px auto 0px auto;
请注意,这仅适用于具有设置宽度的元素。 另外,你可以使用

    line-height: 'your div height' 

要对齐中间的文本

{width:auto;margin:auto}
将起作用,您可能需要固定要居中的元素的宽度

只需在CSS代码中添加以下内容:

text-align: center;
vertical-align: middle;
line-height: 400px; /* the same as your div height */

您可以使用绝对位置,使其在页面上居中

#item {
    height:400px;
    width:700px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-top: -200px;
    margin-left: -350px;
    background-color:white;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;
    border-radius: 10px;
}
再加上这些

文本对齐:居中;
垂直对齐:中间对齐;

显示:表格单元格

在此div之外创建一个div,并使其宽度为100%。现在给出你的
div边距:0aut
o;带有
位置:相对
。这应该行得通

<div style="width:100%">
  <div id="item"></div>
</div>


文本对齐:居中;页边距:0自动?(a)该页眉中还有其他内容吗?,(b)页眉高度是固定的还是动态的?,(c)您是否支持IE8及以下版本?正如其他人所说,水平是容易的,但垂直不是。。。这是可行的,取决于这些问题谢谢,但现在我明白了:我不能在页边加上页边:…px;
<div style="width:100%">
  <div id="item"></div>
</div>