html - 如何在 css 中正确居中 100% 宽度的 div

标签 html css

我试图让我的网站居中,我试过将 div 放在 div 中,但我认为这不是正确的做法。有人能告诉我如何拥有 100% 宽度但像 facebook 一样居中吗?提前致谢。

最佳答案

简单使用,例如:

Demo Fiddle

body{
    width:100%; /* <--- make the document content the width of the viewport */
    text-align:center; /* <--- centre the document content */
    margin:0;
}
div{
    margin:0 auto; /** <-- align the content to the centre of the document */
    width:500px; /** <-- width of your content */
}

关于html - 如何在 css 中正确居中 100% 宽度的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25244098/

相关文章:

javascript - jQuery:淡入数据而不是警报?

javascript - jQuery 日期选择器定位

html - max-width 在 IE11 上的 Flex-box 中无法正常工作

javascript - 如何让我的滚动到顶部按钮在滚动后出现

html - 并排对齐 2 个 div - 右侧随机宽度,左侧使用剩余宽度

css - Wordpress 图像在移动设备上被切断

html - CSS:3x3 repeat-y 背景顶部和底部限制?

javascript - 滚动时突出显示目录。在 jquery 1.10.1 上工作正常,但在 3.0.0 上不行

javascript - PHP 变量不在 Javascript 的范围内,如何在范围内获取它?

javascript - Bootstrap 下拉按钮问题