我想使图像居中

时间:2019-07-11 12:42:40

标签: html css angular ionic-framework

我正在为初始屏幕创建一个加载徽标。animation looks exactly like this。因此,当我尝试此代码时,徽标并未按我的意愿居中放置,并且徽标随着设备长度的增加而减小{ {3}}。it is looking like this。我正在使用ionic4 / angular8。

ion-content{
--background:yellow;
}
.tree {
    left: 0;    
    right: 10%;
    margin:0 auto;
    top:50%;
    transform:translateY(-50%);
    position: relative;
}
.tree > div {
 position: absolute;
 height: 100%;
 width: 100%;
 background: yellow;
top: 0;
left: 0;
-webkit-animation-name: hello;
-webkit-animation-duration: 5s;
-webkit-animation-fill-mode: forwards;
 animation-name: hello;
animation-duration: 5s;
animation-fill-mode: forwards;
}

.tree img {
    max-width: 100%;
}
@keyframes hello {
    0% {
        height: 100%;
    }
    100% {
        height: 0%;
    }
}
@-webkit-keyframes hello {
    0% {
        height: 100%;
    }
    100% {
        height: 0%;
    }
}
<ion-content>
<div class="tree">
    <div></div>
    <img src="logo.svg" />
</div>
<ion-content>

4 个答案:

答案 0 :(得分:1)

您可以使用保证金:0自动

.tree {
    margin: 0 auto;
}

答案 1 :(得分:0)

您还可以使用flexbox解决此问题(https://css-tricks.com/snippets/css/a-guide-to-flexbox/

以当前正在使用的80vh(查看高度)的高度播放,您可以垂直确定位置。使用其他类型的设备(屏幕高度)时会有所不同

.tree {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 80vh;
        }
    
    .tree > div {
        position: absolute;
        height: 100%;
        width: 100%;
        background: #fff;
        top: 0;
        left: 0;
        -webkit-animation-name: hello;
        -webkit-animation-duration: 5s;
        -webkit-animation-fill-mode: forwards;
        animation-name: hello;
        animation-duration: 5s;
        animation-fill-mode: forwards;
    }
    
    .tree img {
        max-width: 300px;
    }
    
    @keyframes hello {
        0% {
            height: 100%;
        }
        100% {
            height: 0%;
        }
    }
    
    @-webkit-keyframes hello {
        0% {
            height: 100%;
        }
        100% {
            height: 0%;
        }
    }
<div class="tree">
    <div></div>
    <img src="http://www.colourbox.com/preview/4221089-363386-green-vector-tree-nature-symbol.jpg" alt="" />
</div>

答案 2 :(得分:-1)

简单地将div居中

<ion-content>
<center>
<div class="tree">
    <div></div>
    <img src="logo.svg" />
</div>
</center>
<ion-content>

答案 3 :(得分:-2)

ion-content{
--background:yellow;
}
.tree {
    position: absolute;
    left:50%;
    top:50%;
    margin-top:-50px;
    margin-left:-50px;
}

.tree img {
    max-width: 100px;
}
@keyframes hello {
    0% {
        height: 100%;
    }
    100% {
        height: 0%;
    }
}
@-webkit-keyframes hello {
    0% {
        height: 100%;
    }
    100% {
        height: 0%;
    }
}
<ion-content>
<div class="tree">
   <div class="loader-img">
    <img src="http://www.colourbox.com/preview/4221089-363386-green-vector-tree-nature-symbol.jpg" />
    </div>
</div>
<ion-content>