1

I'm pretty new to HTML/CSS and stumbled across a tutorial to help me create an image slideshow for a website I'm making. I figured out how to get the slideshow to work but still haven't figured out how to move it yet. sorry if the formatting is off I use prettier and not too sure if I like it. Thanks in advance!

Video by Clever Techie

/*image slider*/

.slide-container {
  top: 150%;
  width: 586px;
  height: 330;
  margin-left: auto;
  margin-right: auto;
  overflow: hidden;
  text-align: center;
}

.image-container {
  top: 100%;
  width: 2344px;
  height: 330;
  position: relative;
  transition: left 0.5s;
  -webkit-transition: left 0.5s;
  -moz-transition: left 1s;
}

.slider-image {
  float: left;
  margin: 0px;
  padding: 0px;
}

.button-container {
  top: 120%;
  position: relative;
}

.slider-button {
  top: 120%;
  display: inline-block;
  height: 10px;
  width: 10px;
  border-radius: 5px;
  background-color: #fff;
}

#slider-image-1:target~.image-container {
  left: 0px;
}

#slider-image-2:target~.image-container {
  left: -586px;
}

#slider-image-3:target~.image-container {
  left: -1172px;
}

#slider-image-4:target~.image-container {
  left: -1758px;
}
<div class="title">
  <h1>Photoreal Scenery</h1>
</div>

<div class="slide-container">
  <span id="slider-image-1"></span>
  <span id="slider-image-2"></span>
  <span id="slider-image-3"></span>
  <span id="slider-image-4"></span>
  <div class="image-container">
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
  </div>
</div>
<div class="button-container">
  <a href="#slider-image-1" class="slider-button"></a>
  <a href="#slider-image-2" class="slider-button"></a>
  <a href="#slider-image-3" class="slider-button"></a>
  <a href="#slider-image-4" class="slider-button"></a>
</div>
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49

3 Answers3

1

Welcome to SO.

seems like you are trying pure HTML CSS solution in that case please check this snippet (use full pageview). the tutorial related to this is here

.row {
  background: #fff;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
* {
  box-sizing: border-box;
}

.slider {
  width: 300px;
  text-align: center;
  overflow: hidden;
}

.slides {
  display: flex;
  
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  
  
  
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  
  /*
  scroll-snap-points-x: repeat(300px);
  scroll-snap-type: mandatory;
  */
}
.slides::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.slides::-webkit-scrollbar-thumb {
  background: black;
  border-radius: 10px;
}
.slides::-webkit-scrollbar-track {
  background: transparent;
}
.slides > div {
  scroll-snap-align: start;
  flex-shrink: 0;
  width: 100%;
  height: 400px;
  margin-right: 50px;
  border-radius: 10px;
  background: #eee;
  transform-origin: center center;
  transform: scale(1);
  transition: transform 0.5s;
  position: relative;
  
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 100px;
}
.slides > div:target {
/*   transform: scale(0.8); */
}
.author-info {
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 0.75rem;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  margin: 0;
}
.author-info a {
  color: white;
}
img {
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.slider > a {
  display: inline-flex;
  width: 1.5rem;
  height: 1.5rem;
  background: white;
  text-decoration: none;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  margin: 0 0 0.5rem 0;
  position: relative;
}
.slider > a:active {
  top: 1px;
}
.slider > a:focus {
  background: #000;
}

/* Don't need button navigation */
@supports (scroll-snap-type) {
  .slider > a {
    display: none;
  }
}

html, body {
  height: 100%;
  overflow: hidden;
}
body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to bottom, #ddd, #eee);
  font-family: 'Ropa Sans', sans-serif;
}
<div class="slider">
   <a href="#slide-1">1</a>
   <a href="#slide-2">2</a>
   <a href="#slide-3">3</a>
   <a href="#slide-4">4</a>
   <a href="#slide-5">5</a>
   <div class="slides">
      <div id="slide-1">
         <img
            src="https://image.freepik.com/free-photo/empty-wooden-plank-floor-space-with-green-garden-leaves-product-display-space-with-fresh-green-nature_1150-18098.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-2">
         <img
            src="https://image.freepik.com/free-photo/wooden-board-empty-table-top-blurred-background_1253-1584.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-3">
         <img
            src="https://image.freepik.com/free-photo/boards-with-unfocused-background_1253-55.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-4">
         <img
            src="https://image.freepik.com/free-photo/wooden-table-looking-out-defocussed-room-interior_1048-11830.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-5">
         <img
            src="https://image.freepik.com/free-photo/empty-wood-table-top-blur-glass-window-interior-restaurant-banner-mock-up-abstract-background-can-used-display-montage-your-products_7191-1132.jpg"
            class="slider-image"
            />
      </div>
   </div>
</div>

Also as Squashie01 has suggested you can use bootstrap classes to get this done easily here is a fiddle using the newest bootstrap classes. refer the documentation here.

Rosh_LK
  • 680
  • 1
  • 15
  • 36
1

Since you haven't described which type of slideshow you need, I have created slideshow which can either run automatically or can be controlled by the user. After user click or site load, it start automatic slide show in 10s. Automatic slideshow change slides in every 3s. Check out this snippet:

var slideIndex = 0;
var timer1, timer;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");

function showSlides() {//called by automatic slideshow
    clearTimeout(timer1);//clear previously set timer 

    //clear styles
    for (var i = 0; i < slides.length; i++)
        slides[i].style.display = "none";           
    for (var i = 0; i < dots.length; i++) 
        dots[i].className = dots[i].className.replace(" active", "");
    
    //reset slideIndex
    if (++slideIndex == slides.length)
        slideIndex = 0;

    //reset styles
    slides[slideIndex].style.display = "block"; 
    dots[slideIndex].className += " active";
    
    //reset timer
    timer1 = setTimeout(showSlides, 3000);
}

function showSlide(n) {//called when user manually choose particular slide
    //clear previously set timers
    clearTimeout(timer1);
    clearTimeout(timer);

    //clear styles
    for (var i = 0; i < slides.length; i++) 
        slides[i].style.display = "none";
    for (var i = 0; i < dots.length; i++) 
        dots[i].className = dots[i].className.replace(" active", "");

    //reset slideIndex
    if (n == slides.length)
        slideIndex = 0; 
    if (n < 0)
        slideIndex = slides.length;

    //reset styles
    slides[slideIndex].style.display = "block"; 
    dots[slideIndex].className += " active";

    //reset timer for automatic slideshow
    timer = setTimeout(showSlides, 10000);
}
/* make width, height, min/max properties includes content, padding and border */
* {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
    width: 50vw;
    height: 50vh;
    position: relative;
    margin: auto;
    clear:both;
    background-color: black;        
}

/* Hide the images by default */
.mySlides {
    display: none;
}

/* make images fit to slide width */
.mySlides img {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
    height: 50vh; object-fit:  fill;
}

/* Next & previous buttons */
.prev, .next {
    cursor: pointer;
    position: absolute;
    top: 50%;
    margin: 0;
    width: auto;
    padding: 16px;
    color: white;
    font-weight: bold;
    font-size: xx-large;
    transition: 0.6s;
    user-select: none;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);

}

/* Position the "next button" to the right */
.next {
    right: 0;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
    background-color: black;
    opacity: 0.7;
}

/* Caption text */
.text {
    color: #f2f2f2;
    font-size: 4em;
    padding-bottom: : 12px;
    position: absolute;
    bottom: 8px;
    width: 100%;
    text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
    color: #f2f2f2;
    font-size: 1.5em;
    padding: 8px 12px;
    position: absolute;
    top: 0;
    z-index: 1;
}


/* Dots container */
.dots-container {
    text-align: center;
}


/* The dots/bullets/indicators */
.dot {
    cursor: pointer;
    height: 15px;
    width: 15px;
    margin: 0 2px;
    background-color: #bbb;
    border-radius: 50%;
    display: inline-block;
    transition: background-color 0.6s ease;
}

.active, .dot:hover {
    background-color: #717171;
}

/* Fading animation */
.fade {
    -webkit-animation-name: fade;
    -webkit-animation-duration: 1.5s;
    animation-name: fade;
    animation-duration: 1.5s;
}

@-webkit-keyframes fade {
    from {opacity: .4} 
    to {opacity: 1}
}

@keyframes fade {
    from {opacity: .4} 
    to {opacity: 1}
}

/* Small devices specific alterations */
@media only screen and (max-width: 600px) {
    /* Slideshow container */
    .slideshow-container {
        width: 96vw;
    }

    .text {
        font-size: 1.5em;
    }

    .numbertext {
        font-size: 1em;
    }

    /* Next & previous buttons */
    .prev, .next {
        padding: 4px;
        font-weight: bold;
        font-size: large;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Apple</title>
</head>
<!-- start slideshow on page load. -->
<body onload="showSlide(1);">
    <div class="title">
        <h1>Photoreal Scenery</h1>
    </div>

    <!-- Slideshow container -->
    <div class="slideshow-container">

        <!-- Full-width images with number and caption text -->
        <div class="mySlides fade" >
            <div class="numbertext">1 / 4</div>
            <img src="red.png">
            <div class="text">Caption Text</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">2 / 4</div>
            <img src="blue.png">
            <div class="text">Caption Two</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">3 / 4</div>
            <img src="green.png">
            <div class="text">Caption Three</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">4 / 4</div>
            <img src="j.jpg">
            <div class="text">Caption Three</div>
        </div>

        <!-- Next and previous buttons -->
        <a class="prev" onclick="showSlide(--slideIndex);">&#10094;</a>
        <a class="next" onclick="showSlide(++slideIndex);">&#10095;</a>
    </div>
    <br>

    <!-- The dots/circles -->
    <div class="dots-container">
        <span class="dot" onclick="showSlide(slideIndex = 0)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 1)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 2)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 3)"></span> 
    </div>
</body>
</html>

Here is the logic:

HTML

We have added <meta name="viewport" content="width=device-width, initial-scale=1.0"> to make page-width follows device width and set initial scaling to 1.0.

These are the classes

  1. slideshow-container - store container
  2. mySlides - slides
  3. fade - fade animation
  4. prev/next - previous, next buttons
  5. dots-container - store dots
  6. dot - individual dot

CSS Important CSS additions are

  1. *(all) - make border, padding included in measurements of height width
  2. slideshow-container - set it to 50% of viewport height and width
  3. mySlides - hide all slides
  4. mySlides img - center image and set bounds
  5. .prev, .next - disable selection, change to pointing cursor add transition
  6. .numbertext - set z-index to 1
  7. .dot - change to pointing cursor add transition
  8. @-webkit-keyframes fade - setup IE specific fading rules
  9. @keyframes fade - setup fading rules
  10. @media only screen and (max-width: 600px) - tweaks for small screen devices

JavaScript There are 2 functions:

  1. showSlides() - used to create automatic slideshow.
  2. showSlide(n) - used to view slide selected by the user.
gpl
  • 1,380
  • 2
  • 8
  • 24
0
    <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
       <div class="carousel-inner">
           <div class="carousel-item active">
             <img class="d-block w-100" src="..." alt="First slide">
           </div>
           <div class="carousel-item">
             <img class="d-block w-100" src="..." alt="Second slide">
           </div>
           <div class="carousel-item">
             <img class="d-block w-100" src="..." alt="Third slide">
           </div>
         </div>
         <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
           <span class="carousel-control-prev-icon" aria-hidden="true"></span>
           <span class="sr-only">Previous</span>
         </a>
         <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
           <span class="carousel-control-next-icon" aria-hidden="true"></span>
           <span class="sr-only">Next</span>
         </a>
       </div>                                                         
Always Helping
  • 14,316
  • 4
  • 13
  • 29
Squashie01
  • 11
  • 3