Search code examples
jqueryhtmlcsscycle

Centered Slides images


I'm using Cycle2 carousel plugin my all images width are different that's why I want to centered my images but there is something that I wonder is if my parent wrapper width is 900px(for example) and if my images aren't 900px than it will be centered ?

I mean if my images bigger than 900px than do nothing (don't centered)but if my images smaller than 900px than centered them

let me show you on image enter image description here

$(document).ready(function(){


    $('.mySlideShow').cycle({
    next: "#single-right",
    pauseOnHover: true,
    pager: "#single-pager",
    pagerTemplate: "<a href='#'><img src='{{src}}' width=48 height=48></a>",
    prev: "#single-left",
  });
  
 
});
.single-gallery{
  width:800px;
 height:640px;
  position:relative;
}
.cycle-slideshow img {
  height:494px;
  width:auto;
}

#single-pager a img {
  width: 49.3px !important;
  height:49.3px !important;
  border: 1px solid #fff;
}

#single-pager a.cycle-pager-active img {
  opacity: 0.4;
}

#single-left,
#single-right {
  position: absolute;
  top: 50%;
  z-index: 1000;
  background: rgba(255, 255, 255, .8);
  padding: 12px;
  cursor: pointer;
}

#single-left {
  left: 0;
}

#single-right {
  right: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script>
<div class="single-gallery">
  <div class="mySlideShow">
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/golden-wheat-field-lightbox.jpg">
    <img src="https://cdn.pixabay.com/photo/2017/01/07/20/40/candy-1961536_960_720.jpg">
    <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/night-in-the-city-lightbox.jpg">
    <img src="https://cdn.pixabay.com/photo/2017/01/11/11/33/cake-1971552_960_720.jpg">
    <img src="https://www.w3schools.com/w3css/img_avatar3.png">
   
  <div id="single-pager" class="center external"></div>
    
  <div id="single-next-prev">
    <span id="single-left">Prev</span>
    <span id="single-right">Next</span>
  </div>
  </div>

</div>


Solution

  • Try this code

        <link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/1.3.9/css/lightgallery.min.css" rel="stylesheet" />
    
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.cycle2/2.1.6/jquery.cycle2.min.js"></script>
    <div class="single-gallery">
      <div class="mySlideShow">
        <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/golden-wheat-field-lightbox.jpg">
        <img src="https://cdn.pixabay.com/photo/2017/01/07/20/40/candy-1961536_960_720.jpg">
        <img src="https://amazingcarousel.com/wp-content/uploads/amazingcarousel/7/images/lightbox/night-in-the-city-lightbox.jpg">
        <img src="https://cdn.pixabay.com/photo/2017/01/11/11/33/cake-1971552_960_720.jpg">
        <img src="https://www.w3schools.com/w3css/img_avatar3.png">
    
      <div id="single-pager" class="center external"></div>
    
      <div id="single-next-prev">
        <span id="single-left">Prev</span>
        <span id="single-right">Next</span>
      </div>
      </div>
    
    </div>
    

    DEMO