我创建了一个图像滑块,但我不知道如何居中,即使我尝试使用justify-content和align-items属性,但它不工作。
我尝试使用justify-content和align-items属性,但是图像和单选按钮没有居中。
下面是我的代码:
`
<!--image slider start-->
<div class="slider">
<div class="slides">
<!--radio buttons start-->
<input type="radio" name="radio-btn" id="radio1">
<input type="radio" name="radio-btn" id="radio2">
<input type="radio" name="radio-btn" id="radio3">
<input type="radio" name="radio-btn" id="radio4">
<!--radio buttons end-->
<!--slide images start-->
<div class="slide first">
<img src="images/slider-1.jpg" alt="">
</div>
<div class="slide">
<img src="images/slider-2.jpg" alt="">
</div>
<div class="slide">
<img src="images/slider-3.jpg" alt="">
</div>
<div class="slide">
<img src="images/slider-4.jpg" alt="">
</div>
<!--slide images end-->
<!--automatic navigation start-->
<div class="nav-auto">
<div class="auto-btn1"></div>
<div class="auto-btn2"></div>
<div class="auto-btn3"></div>
<div class="auto-btn4"></div>
</div>
<!--automatic navigation end-->
</div>
<!--manual navigation start-->
<div class="nav-manual">
<label for="radio1" class="manual-btn"></label>
<label for="radio2" class="manual-btn"></label>
<label for="radio3" class="manual-btn"></label>
<label for="radio4" class="manual-btn"></label>
</div>
<!--manual navigation end-->
</div>
<!--image slider end-->
<script type="text/javascript">
var counter = 1;
setInterval(function(){
document.getElementById('radio' + counter).checked = true;
counter++;
if(counter > 4){
counter - 1;
}
}, 5000);
</script>
这是CSS:
`/**For slider images**/
.slider {
width: 800px;
height: 500px;
overflow: hidden;
}
.slides {
width: 500%;
height: 500px;
display: flex;
}
.slides input {
display: none;
}
.slide {
width: 20%;
transition: 2s;
}
.slide img {
left: 50%;
width: 800px;
height: 500px;
}
/**for manual slide nav**/
.nav-manual {
position: absolute;
width: 800px;
margin-top: -40px;
display: flex;
justify-content: center;
}
.manual-btn {
border: 2px solid #add8e7;
padding: 5px;
border-radius: 10px;
cursor: pointer;
transition: 1s;
}
.manual-btn:not(:last-child) {
margin-right: 40px;
}
.manual-btn:hover {
background: #add8e7;
}
#radio1:checked ~ .first {
margin-left: 0;
}
#radio2:checked ~ .first {
margin-left: -20%;
}
#radio3:checked ~ .first {
margin-left: -40%;
}
#radio4:checked ~ .first {
margin-left: -60%;
}
/**for automatic nav**/
.nav-auto {
position: absolute;
display: flex;
width: 800px;
justify-content: center;
margin-top: 460px;
}
.nav-auto div {
border: 2px solid #add8e7;
padding: 5px;
border-radius: 10px;
transition: 1s;
}
.nav-auto div:not(:last-child) {
margin-right: 40px;
}
#radio1:checked ~ .nav-auto .auto-btn1 {
background: #add8e7;
}
#radio2:checked ~ .nav-auto .auto-btn2 {
background: #add8e7;
}
#radio3:checked ~ .nav-auto .auto-btn3 {
background: #add8e7;
}
#radio4:checked ~ .nav-auto .auto-btn4 {
background: #add8e7;
}`
2条答案
按热度按时间lrl1mhuk1#
我也附上下面的代码。
rggaifut2#
您已将
overflow:hidden
属性应用于.slider
类。因此,每当您将display:flex;
属性应用于.slider
类时,所有内容都会以该类为中心,并且由于overflow:hidden
属性,内容会消失。请将display:flex; justify-content:center; align-items:center;
应用于.slider
类,而不是将.slider
类放入另一个类中,然后将display:flex; justify-content:center; align-items:center;
属性应用于该类。谢谢你。