简体   繁体   中英

Center elements horizontally in div

I am fairly new to HTML and CSS and am trying to align the navigation elements for my slideshow horizontally in a div. I have tried the solutions found here , but am not having any luck with my issue. With my current code, the circle elements are not center aligned with the < > arrows on either side. I think this is a simple fix, I'm just not sure where I'm going wrong in my code. Any help/explanations would be much appreciated!

<!DOCTYPE html>
<html>
<head>
  <style>
    .slideshow-container {
      position: relative;
      background: #f1f1f1f1;
    }

    .mySlides {
      display: none;
      padding: 5px 20px 5px 20px;
      text-align: center;
    }

    /* Next & previous buttons */
    .prev, .next {
      cursor: pointer;
      width: auto;
      color: #888;
      font-weight: bold;
      font-size: 20px;
      user-select: none;
    }

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

    .prev:hover, .next:hover {
      background-color: #717171;
    }

    .dot-container {
      text-align: center;
      background: #ddd;
    }

    .dot {
      cursor: pointer;
      height: 20px;
      width: 20px;
      margin-bottom: 5px;
      background-color: #bbb;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }

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

  </style>
</head>
<body>

  <div class="slideshow-container">
    <div class="mySlides">
      <h1>Title 1</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>

    <div class="mySlides">
      <h1>Title 2</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>

    <div class="mySlides">
      <h1>Title 3</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
    </div>
  </div>

  <div class="dot-container">
    <span class="prev" onclick="plusSlides(-1)">❮</span>
    <span class="dot" onclick="currentSlide(1)"></span>
    <span class="dot" onclick="currentSlide(2)"></span>
    <span class="dot" onclick="currentSlide(3)"></span>
    <span class="next" onclick="plusSlides(1)">❯</span>
  </div>

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

    function plusSlides(n) {
      showSlides(slideIndex += n);
    }

    function currentSlide(n) {
      showSlides(slideIndex = n);
    }

    function showSlides(n) {
      var i;
      var slides = document.getElementsByClassName("mySlides");
      var dots = document.getElementsByClassName("dot");
      if (n > slides.length) {
        slideIndex = 1;
      }    
      if (n < 1) {
        slideIndex = slides.length
      }
      for (i = 0; i < slides.length; i++) {
        slides[i].style.display = "none";  
      }
      for (i = 0; i < dots.length; i++) {
        dots[i].className = dots[i].className.replace(" active", "");
      }
      slides[slideIndex-1].style.display = "block";  
      dots[slideIndex-1].className += " active";
    }
  </script>
</body>
</html>

Use flexbox to align the items vertically in combination with line-height (set this the same as the height of the dots) and padding on the container (optional, to make it look nicer).

.dot-container {
    text-align: center;
    background: #ddd;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    padding: 5px;
}

.dot {
    cursor: pointer;
    height: 20px;
    width: 20px;
    background-color: #bbb;
    border-radius: 50%;
    display: inline-block;
    transition: background-color 0.6s ease;
}

.prev, .next {
    cursor: pointer;
    width: auto;
    color: #888;
    font-weight: bold;
    font-size: 20px;
    user-select: none;
    line-height: 20px;
}

You can vertical align with flex box for example. This two classes i change:

    .dot-container {
      height:40px;
      background: #ddd;
      display: flex;
      justify-content: center;
      align-items: center;      
    }

    .dot {
      cursor: pointer;
      height: 20px;
      width: 20px;
      background-color: #bbb;
      border-radius: 50%;
      display: inline-block;
      transition: background-color 0.6s ease;
    }

 var slideIndex = 1; showSlides(slideIndex); function plusSlides(n) { showSlides(slideIndex += n); } function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("mySlides"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1; } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; }
 .slideshow-container { position: relative; background: #f1f1f1f1; }.mySlides { display: none; padding: 5px 20px 5px 20px; text-align: center; } /* Next & previous buttons */.prev, .next { cursor: pointer; width: auto; color: #888; font-weight: bold; font-size: 20px; user-select: none; } /* Position the "next button" to the right */.next { right: 0; }.prev:hover, .next:hover { background-color: #717171; }.dot-container { height:40px; background: #ddd; display: flex; justify-content: center; align-items: center; }.dot { cursor: pointer; height: 20px; width: 20px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; }.active, .dot:hover { background-color: #717171; }
 <div class="slideshow-container"> <div class="mySlides"> <h1>Title 1</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div class="mySlides"> <h1>Title 2</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> <div class="mySlides"> <h1>Title 3</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> </div> </div> <div class="dot-container"> <span class="prev" onclick="plusSlides(-1)">❮</span> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> <span class="next" onclick="plusSlides(1)">❯</span> </div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM