css垂直居中固定定位div

joh*_*ith 40 css

我有一个类似灯箱项目的以下HTML.

<div id="lightbox">
  <img id="image" src="" />
</div>
Run Code Online (Sandbox Code Playgroud)

使用以下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;
}
Run Code Online (Sandbox Code Playgroud)

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

我遇到了垂直居中的问题,我使用的方法是简单地设置#lightbox属性中的顶部值.

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

有任何想法吗?谢谢.

Vin*_*ent 108

我见过的垂直和水平居中的最佳解决方案position: fixed div是:

position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Run Code Online (Sandbox Code Playgroud)

小提琴来源.您不需要知道它的尺寸,div也不需要任何容器div.居中div的宽度甚至可以是一个百分比(这是我找到这个解决方案时所寻找的).

  • 如果您居中的元素包含文本,这将导致文本变得模糊,因为使用translate会激活3d硬件加速. (13认同)

小智 17

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

#image{
position:relative;
height:600px;
top: 50%;
margin-top: -300px; /* minus half the height */
border:1px solid grey;
}
Run Code Online (Sandbox Code Playgroud)

  • 使用“转换:translateY(-50%);” 并放下'margin-top:-300px'以使它适合任何高度 (4认同)

Pla*_*nox 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%);
}
Run Code Online (Sandbox Code Playgroud)


Bil*_*exy 5

使用 CSS3 的视口单位和 calc( ) 的组合来使灯箱居中,而不会因硬件加速而造成文本模糊的风险。此解决方案也是响应式的。将公式margin-top = -height / 2从固定单位转换 为视口单位

/* 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 );
}
Run Code Online (Sandbox Code Playgroud)

JSFiddle

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

详细示例

/* 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 );
}
Run Code Online (Sandbox Code Playgroud)
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%);
}
Run Code Online (Sandbox Code Playgroud)