您可以使用
display:flex
height:100vh
在父容器上
min-height/min-width
当整个内容处于绝对位置时,可以进行明智的选择。
align-items
和
justify-content
将以X、Y轴为中心进行管理。。
#history_slideshow {
padding-top: 1%;
/* update */
display:flex;
align-items:center;
justify-content:center;
height:100vh;
/* could be usefull */
min-height:400px;
min-width:600px;
/* end update */
}
.crossfade2>figure {
animation: imageAnimation 27s linear infinite 0s;
backface-visibility: hidden;
color: transparent;
opacity: 0;
position: absolute;
z-index: 0;
margin: 0 auto;
padding-top: 0%;
}
.crossfade2>figure:nth-child(1) {
background-image: url('http://dummyimage.com/200x400/acd&text=history_1.jpg');
background-repeat: no-repeat;
}
.crossfade2>figure:nth-child(2) {
animation-delay:6s;
background-image:url('http://dummyimage.com/600x400/afd&text=history_2.jpg');
background-repeat:no-repeat;
}
.crossfade2>figure:nth-child(3){
animation-delay:12s;
background-image:url('http://dummyimage.com/200x400/0cd&text=history_3.jpg');
background-repeat:no-repeat;
}
.crossfade2>figure:nth-child(4) {
animation-delay: 18s;
background-image: url('http://dummyimage.com/600x400/456&text=history_4.jpg');
background-repeat: no-repeat;
}
@keyframes imageAnimation {
0% {
animation-timing-function:ease-in;
opacity:0;
}
8% {
animation-timing-function:ease-out;
opacity:1;
}
17% {
opacity:1
}
25% {
opacity:0
}
100% {
opacity:0
}
}
<div id="history_slideshow" class="crossfade2">
<figure>
<img src="http://dummyimage.com/200x400/acd&text=history_1.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/600x400/afd&text=history_2.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/200x400/0cd&text=history_3.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/600x400/456&text=history_4.jpg" alt="" />
</figure>
</div>
水平定心
您可以使用
display
+
margin
在父容器上。
display:table;
将缩小内容大小,然后
margin:auto
您需要在流程中通过
position:
absolute
relative
#history_slideshow {
padding-top: 1%;
/* update for horizontal centering */
display:table;
margin:auto;
}
.crossfade2>figure:first-of-type {
position:relative;
}
/* end update */
.crossfade2>figure {
animation: imageAnimation 27s linear infinite 0s;
backface-visibility: hidden;
color: transparent;
opacity: 0;
position: absolute;
z-index: 0;
margin: 0 auto;
padding-top: 0%;
}
.crossfade2>figure:nth-child(1) {
background-image: url('http://dummyimage.com/300x200/acd&text=history_1.jpg');
background-repeat: no-repeat;
}
.crossfade2>figure:nth-child(2) {
animation-delay:6s;
background-image:url('http://dummyimage.com/300x200/afd&text=history_2.jpg');
background-repeat:no-repeat;
}
.crossfade2>figure:nth-child(3){
animation-delay:12s;
background-image:url('http://dummyimage.com/300x200/0cd&text=history_3.jpg');
background-repeat:no-repeat;
}
.crossfade2>figure:nth-child(4) {
animation-delay: 18s;
background-image: url('http://dummyimage.com/300x200/456&text=history_4.jpg');
background-repeat: no-repeat;
}
@keyframes imageAnimation {
0% {
animation-timing-function:ease-in;
opacity:0;
}
8% {
animation-timing-function:ease-out;
opacity:1;
}
17% {
opacity:1
}
25% {
opacity:0
}
100% {
opacity:0
}
}
<div id="history_slideshow" class="crossfade2">
<figure>
<img src="http://dummyimage.com/300x200/acd&text=history_1.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/300x200/afd&text=history_2.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/300x200/0cd&text=history_3.jpg" alt="" />
</figure>
<figure>
<img src="http://dummyimage.com/300x200/456&text=history_4.jpg" alt="" />
</figure>
</div>