【问题标题】:CSS - center everything within the body tagCSS - 将 body 标签内的所有内容居中
【发布时间】:2016-06-08 03:48:01
【问题描述】:

我试图在页面上的标记中居中放置所有内容,而不在其中添加另一个容器。我的情况不允许我更改正在生成的标记,但我可以编辑 CSS。我的总体目标是创建 css,允许我在 IE 中使用缩放标签来打印 css(缩小),但它目前的工作方式,这会在右侧创建很多空白,我会喜欢确保内容始终居中。

【问题讨论】:

标签: html css internet-explorer


【解决方案1】:

如果您可以为主体指定一个固定宽度,您可以通过为它的左右边距指定宽度和 auto 边距来将其居中:

例如:

body { width: 960px; margin: 0 auto; }

【讨论】:

    【解决方案2】:

    如果你想要这样的东西 - http://jsbin.com/emomi3/5/ ...

      body {
        position: relative;
        width: 100%;
        border: 1px solid red;
        display: block;
      }
    
      body > *  {
        margin: auto;
        width: 500px;
        border: 1px solid blue;
        display: block;
        overflow: auto;
      }
    
      body > script {
        display: none
      }
    

    (边框仅用于说明目的)

    【讨论】:

    • 为什么是“位置:相对;”?
    【解决方案3】:

    如果您使用primefaces ...,我建议您添加!important,如下所示:

    body { 
        width: 1280px;
        height: 1024px;
        margin: 0 auto !important;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2013-02-04
      • 1970-01-01
      • 2014-05-07
      相关资源
      最近更新 更多