首页 > 解决方案 > 如何居中预加载器(移动)?

问题描述

尝试了很多不同的解决方案,但由于某种原因,我的预加载器无法在移动设备上居中。在桌面/移动风景上很好,但每当我在移动肖像上时......它不是居中的。

CSS:

    .load-screen{
    background-color: white;
    opacity: 1;
    position: fixed;
    margin-left: auto;
    z-index: 1000;
    width: 100%;
    height: 100vh;
    transition: 0.4s ease-in;
}

.loading-image{
    display: block;
    width: 100%;
    margin-top: 15vw;
    margin-left: auto;
    animation-name: loadscreen;
    animation-duration: 3.5s;
    animation-iteration-count: infinite;
    align-items: center;
    justify-content: center;
    text-align: center;

}

截图: 手机截图

标签: javascripthtmlcsspreloader

解决方案


使用 display:flex 将使里面的任何东西成为一个弹性项目。justify-content: center 将项目从左到右居中对齐项目: center 将项目从上到下居中。

见小提琴:http: //jsfiddle.net/fm3xvzc8/

.load-screen{
    background-color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    opacity: 1;
    position: fixed;
    margin-left: auto;
    z-index: 1000;
    width: 100%;
    height: 100vh;
    transition: 0.4s ease-in;
}

.loading-image{
    display: block;
    width: 100%;
    // margin-top: 15vw; The margins aren't necessary
    // margin-left: auto; The margins aren't necessary
    animation-name: loadscreen;
    animation-duration: 3.5s;
    animation-iteration-count: infinite;

}

推荐阅读