【问题标题】:Centering div horizontally and vertically without distorting content将 div 水平和垂直居中而不扭曲内容
【发布时间】:2014-02-09 20:56:11
【问题描述】:

我正在尝试使 id="center" 的 div 在 body 标记内垂直和水平居中,而不会扭曲内容的位置:M、cicle div 和 Coming Soon... 文本。即使它应该是一件简单的事情,也无法真正让它发挥作用..

HTML:

<body>
    <div id="center">
        <div class="circle"><div>M</div></div>
        <h2>Coming soon...</h2>
    </div>
</body>

CSS:

* {
    margin: 0;
    padding: 0;
    font-family: futura;
}

body {
    height: 100%;
    width: 100%;
    background-color: black;
}

h2 {
    color: #f6c003;
    font-family: serif;
    font-style: italic;
    font-weight: 400;
    font-size: 0.9em;
    margin-top: 10px;
}

.circle {
    text-align: center;
    width: 50px;
    height: 50px;
    -moz-border-radius: 50%; 
    -webkit-border-radius: 50%; 
    border-radius: 50%;
    background: #f6c003;
}

.circle div {
    float:left;
    width:50px;
    padding-top:25px;
    line-height:1em;
    margin-top:-0.5em;
    text-align:center;
    color:black;
    font-style: bold;
    font-weight: 700;
    font-family: serif;
    font-size: 2.5em;
}

Fiddle

【问题讨论】:

  • 询问如何使占据整个宽度的元素居中是没有意义的……

标签: css html position center


【解决方案1】:

你可以使用一些绝对定位来完成它

#center {
    position:absolute;
    left:50%;
    top:50%;
    margin: -33px 0 0 -43px;
}

关键是你有一半宽度和一半高度的负边距来正确定位元素(仅限固定大小)

还要确保您的身体设置为position: relative;,以便绝对定位正常工作。

【讨论】:

    【解决方案2】:
    <style>
    body {
    height: 100%;
    width: 100%;
    background-color: black;
    }
    
    h2 {
    color: #f6c003;
    font-family: serif;
    font-style: italic;
    font-weight: 400;
    font-size: 0.9em;
    margin-top: 10px;
    }
    
    .circle {
    text-align: center;
    width: 50px;
    height: 50px;
    -moz-border-radius: 50%; 
    -webkit-border-radius: 50%; 
    border-radius: 50%;
    background: #f6c003;
    }
    
    .circle div {
    float:left;
    width:50px;
    padding-top:25px;
    line-height:1em;
    margin-top:-0.5em;
    text-align:center;
    color:black;
    font-style: bold;
    font-weight: 700;
    font-family: serif;
    font-size: 2.5em;
    }
    #center
    {
    position:absolute;
    left:50%;
    top:50%;
    }
    </style>
    <body>
    <div id="center">
    <div class="circle"><div>M</div></div>
    <h2>Coming soon...</h2>
    </div>
    </body>
    

    【讨论】:

      【解决方案3】:

      尽管我讨厌表格,但表格在垂直对齐方面做得很好。也许有更好的方法?

      <body>
          <table>
              <tr>
                  <td>
                      <div id="center">
                          <div class="circle"><div>M</div></div>
                          <h2>Coming soon...</h2>
                      </div>
                  </td>
              </tr>
          </table>
      </body>
      

      风格:

      table {
          width: 100%;
          height: 100%;
      
          border-spacing: 0px;
          border-collapse: collapse;
      }
      
      .center {
          margin: auto;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-09-16
        • 2015-08-29
        • 1970-01-01
        • 2015-10-10
        • 2012-09-23
        • 2020-01-19
        • 2012-08-31
        • 2011-09-04
        • 1970-01-01
        相关资源
        最近更新 更多