CSS图库 - 响应式中心对齐

时间:2017-03-23 22:59:20

标签: javascript html css alignment responsive

我一直在使用JavaScript控件构建CSS库,我正在使用纵向和横向图片。我可以让它们在主页面的中心对齐<div>我已将图像和控件分组到一个较小的容器中但是我不能让两种类型的图像都沿着该容器内的两个轴居中

以下是我在jsfiddle的第一次努力,请看看所有建议,包括重定向到我可能错过的类似问题,欢迎使用。

的HTML

<div class="image-wrap">
  <div id="pic-gallery">
    <div class="gal-butt">
      <a onclick="plusDivs(-1)">&#10094;</a>
    </div>
    <div class="pic_ver">
      <a target="_blank" href="images/katie_Holding.gif">
        <img class="mySlides" src="images/katie_Holding.gif" border="1" alt="" width="200" height="300">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_1.gif">
        <img class="mySlides" src="images/Mindees_1.gif" border="1" alt="" width="300" height="200">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_2.gif">
        <img class="mySlides" src="images/Mindees_2.gif" border="1" alt="" width="300" height="200">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_3.gif">
        <img class="mySlides" src="images/Mindees_3.gif" border="1" alt="" width="300" height="200">
      </a>
    </div>
    <div class="gal-butt">
      <a onclick="plusDivs(-1)">&#10095;</a>
    </div>
  </div>
</div>

CSS

#pic-gallery {
  margin-left: 330px;
  width: 400px;
  height: 400px;
  border: 1px solid black;
  align-items: center;
  display: flex;
}

.image-wrapper {
  margin: auto;
  align-items: center;
  justify-content: center;
}

.gal-butt {
  font-size: 2.5em;
}

#pic-ver {
  padding-top: 50px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 100px;
}

#pic-hor {
  padding-top: 100px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 50px;
}

JS

 var slideIndex = 1;
 showDivs(slideIndex);

 function plusDivs(n) {
   showDivs(slideIndex += n);
 }

 function showDivs(n) {
   var i;
   var x = document.getElementsByClassName("mySlides");
   if (n > x.length) {
     slideIndex = 1
   }
   if (n < 1) {
     slideIndex = x.length
   }
   for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
   }
   x[slideIndex - 1].style.display = "block";
 }

https://jsfiddle.net/bb85rqpe/1/

1 个答案:

答案 0 :(得分:0)

margin: auto添加到.gal-butt以水平居中该元素。这是一个关于中心事物https://www.w3.org/Style/Examples/007/center.en.html

的好资源

#pic-gallery {
  margin-left: 330px;
  width: 400px;
  height: 400px;
  border: 1px solid black;
  align-items: center;
  display: flex;
}

.image-wrapper {
  margin: auto;
  align-items: center;
  justify-content: center;
}

.gal-butt {
  font-size: 2.5em;
  margin: auto;
}

#pic-ver {
  padding-top: 50px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 100px;
}

#pic-hor {
  padding-top: 100px;
  padding-right: 0px;
  padding-bottom: 0px;
  padding-left: 50px;
}
<div class="image-wrap">
  <div id="pic-gallery">
    <div class="gal-butt">
      <a onclick="plusDivs(-1)">&#10094;</a>
    </div>
    <div class="pic_ver">
      <a target="_blank" href="images/katie_Holding.gif">
        <img class="mySlides" src="images/katie_Holding.gif" border="1" alt="" width="200" height="300">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_1.gif">
        <img class="mySlides" src="images/Mindees_1.gif" border="1" alt="" width="300" height="200">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_2.gif">
        <img class="mySlides" src="images/Mindees_2.gif" border="1" alt="" width="300" height="200">
      </a>
    </div>
    <div class="pic_hor">
      <a target="_blank" href="images/Mindees_3.gif">
        <img class="mySlides" src="images/Mindees_3.gif" border="1" alt="Katie holding Mindee" width="300" height="200">
      </a>
    </div>
    <div class="gal-butt">
      <a onclick="plusDivs(-1)">&#10095;</a>
    </div>
  </div>
</div>

<script>
 var slideIndex = 1;
 showDivs(slideIndex);

 function plusDivs(n) {
   showDivs(slideIndex += n);
 }

 function showDivs(n) {
   var i;
   var x = document.getElementsByClassName("mySlides");
   if (n > x.length) {
     slideIndex = 1
   }
   if (n < 1) {
     slideIndex = x.length
   }
   for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";
   }
   x[slideIndex - 1].style.display = "block";
 }
</script>