DIV - 确切的页面中心

时间:2014-12-03 03:07:04

标签: html css

我需要在页面中间放置一个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;
}

2 个答案:

答案 0 :(得分:1)

将此样式用于tc-content

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

这是一个有效的 Fiddle

答案 1 :(得分:0)

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

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