L84*_*L84 5 css layout position positioning cross-browser
可能的重复:
如何使用 CSS 轻松水平居中 <div> ?
我读过多种使用 CSS 在页面上居中内容的方法。我不是在谈论文本,而是在谈论图像或容器等内容。
居中内容的最佳方式是什么?
设置框的宽度,然后使用类似 left: 50% 的值?
我读过很多方法,试图理解哪种方法真正效果最好,并且跨浏览器友好并且可以在旧版浏览器中工作,这确实令人困惑。
关于什么是真正最好和最有效的方法有什么想法吗?
谢谢!
将 div 居中:
<div class="pageContainer">
The awesome content is here
<div> the secret of life....</div>
.... more content ....
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
body {
width: 100%;
}
.pageContainer {
width: 600px;
margin: 0px auto;
}
Run Code Online (Sandbox Code Playgroud)
这将使宽度为 600px 的 div 在页面上水平居中。
边距:0 自动;将左右边距设置为页面左侧和右侧剩余的任何像素。
例如,如果页面宽度为 800px,则左边距为 100px,右边距为 100px,将 600px 移动到页面中心