css垂直居中固定定位div

时间:2013-01-29 13:01:17

标签: css

我有一个类似灯箱项目的HTML。

<div id="lightbox">
  <img id="image" src="" />
</div>

使用以下CSS:

#lightbox{
display:none;
position:fixed;
width:100%;
text-align:center;
z-index:600;
cursor:pointer;
left:0;
top:100px;
}

#image{
position:relative;
height:600px;
border:1px solid grey;
}

要让图像始终位于水平中心,我只是在包装div上使用text-align:center,所以我100%确定它是正确的。

我遇到垂直居中问题,我正在使用一种解决方法,只需在#lightbox属性中设置最高值。

正如您所看到的,图像的高度已知,因此在jQuery中很容易实现,但我正在寻找纯CSS解决方案。

有什么想法吗?感谢。

7 个答案:

答案 0 :(得分:95)

我在position: fixed div的垂直和水平居中方面看到的最佳解决方案是:

position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

Fiddlesource。您不需要知道div的维度,也不需要任何容器div。居中div的宽度甚至可以是百分比(这是我找到此解决方案时所寻找的)。

答案 1 :(得分:17)

或者,你可以试试这个(只有你知道图像的高度才有效):

#image{
position:relative;
height:600px;
top: 50%;
margin-top: -300px; /* minus half the height */
border:1px solid grey;
}

答案 2 :(得分:6)

以下是我正在使用的SASS mixins ...我希望这会帮助某人:

// vertically centers as relative
@mixin vertical-align {
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}


// vertically centers the element as absolute
@mixin vertical-center {
    position: absolute;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}


// horizontally centers the element as absolute
@mixin horizontal-center {
    position: absolute;
    left: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}


// absolutely centers the element inside of its first non-static parent
@mixin absolute-center {
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}

答案 3 :(得分:4)

如果您希望图像具有流体大小,可以使用视图的高度作为参考:

#img {
   position: relative; /* or absolute or fixed depending on your needs */
   top: 50%;
   height: 80vh;
   margin-top: -40vh; /* minus half the height */
}

答案 4 :(得分:3)

使用CSS3的视口单元和calc()的组合来使灯箱居中,而不会因硬件加速而模糊文本。该解决方案也具有响应性。转换公式 aid从固定单位到视口单元

margin-top = -height / 2

JSFiddle

有关相对长度单位和calc()的更多信息:

详细示例

&#13;
&#13;
/* Overlay */
body::after
{
    position: fixed;
    z-index: 19000;
    top: 0;
    left: 0;

    display: block;

    width: 100%;
    height: 100%;

    content: ' ';

    opacity: .2;
    background-color: #000;
}

/* Lightbox */
.lightbox
{
    position: fixed;
    z-index: 15;
    top: 50%;
    width: 90vw;
    height: 90vh;
    margin: 0 5vw;
    margin-top: calc(-90vh / 2 );
}
&#13;
body {
    margin: 0;
}
/* The Overlay */
 body::after {
    position: fixed;
    z-index: 14;
    top: 0;
    left: 0;
    display: block;
    width: 100%;
    height: 100%;
    content:' ';
    opacity: .5;
    background-color: #000;
}
/* The Lightbox */
 .lightbox{
    position: fixed;
    z-index: 15;
    top: 50%;
    width: 60vw;
    height: 60vh;
    margin: 0 20vw;
    margin-top: calc(-60vh / 2);
    background:#fff;
    border: 1px solid white;
    border-radius: 5px;
    overflow:hidden;
    box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.8);
}

/* Bonus: responsive and centered image */
 .lightbox h1 {
     box-shadow: 0px 0px 20px 0px rgba(0, 0, 0, 0.8);
     position: relative;
     padding-left: 1em;
     font-family: sans-serif;
     color: #E2E8F2;
 }
.highlight
{
    color: #4E2622;
}
 .lightbox img {
    max-width: 120%;
    height: auto;
    position:absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
&#13;
&#13;
&#13;

答案 5 :(得分:-1)

要垂直居中项目,您需要以下2个css属性:

display:table-cell;
vertical-align:middle;

示例:http://jsfiddle.net/yjv94/

答案 6 :(得分:-2)

你检查过这个消息吗?:

CSS: Vertically align div when no fixed size of the div is known

这里你不需要声明一个高度。

此致

相关问题