【问题标题】:DIV - exact center of page [duplicate]DIV - 页面的确切中心[重复]
【发布时间】:2015-01-31 11:58:44
【问题描述】:

我需要在页面中间准确放置一个宽度和高度未知的 div。我可以让它以宽度为中心,但可以让它以高度为中心吗?

HTML:

<div class='lightbox'>
    <div class='tc-content'>AAA</div>
</div>

CSS

.lightbox { 
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background: #000;
    text-align: center;
}

.tc-content {
    display: inline-block;
}

【问题讨论】:

标签: html css


【解决方案1】:

对 tc-content 使用这种风格

.tc-content {
    position: relative;
    top: calc(50% - 1em);
}

这是一个有效的Fiddle

【讨论】:

    【解决方案2】:

    很多时候我用它来水平和垂直居中 div

    .lightbox {
        position: absolute;
        margin: auto;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        /* Other code here */   
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-27
      • 2011-02-05
      • 2023-01-27
      • 2012-03-30
      • 2018-08-02
      • 1970-01-01
      • 2015-03-01
      • 2014-09-30
      • 2013-11-09
      相关资源
      最近更新 更多