skip to Main Content

Explanation

I’m trying to set a overlay on each image of the slider (slick.js) when hovering them, but for some reason, when I hover it, the overlay appears on top of the slider (all 12 images), not the image hovered.

Code

You can also see it in JSFiddle and in “fullscreen mode”.

ps: it’s be better to see the overlay content in fullscreen.

$('.carousel').slick({
    arrows: false,
    dots: true,
    slidesPerRow: 3,
    rows: 3
});
.carousel-wrapper {
    background-color: rgb(235, 235, 235);
    position: relative;
}

img {
    background-color: black;
}

.slick-slide {
    text-align: center !important;
}

.overlay {
    position: absolute;
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
    background: rgba(1, 1, 1, .35);
    transition: 0.2s;
    opacity: 0;
}

.overlay:hover {
    opacity: 1;
}

.overlay-content {
    color: rgb(255, 255, 255);
    position: absolute;
    top: 50%;
    left: 50%;
}
<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css" />
</head>

<body>

    <section class="carousel-wrapper">
        <div class="container">
            <div class="row">

                <ul class="col-md-12 carousel text-center">
                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>

                    <li>
                        <a href="#">
                            <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
                            <div class="overlay">
                                <div class="overlay-content">
                                    <h4>content</h4>
                                </div>
                            </div>
                        </a>
                    </li>
                </ul>

            </div>
        </div>
    </section>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.js"></script>

</body>

</html>

Thanks in advance,
Luiz.

2

Answers


  1. Why is it not working?

    Your code doesn’t work as expected because your overlay is being positioned relatively to .carousel-wrapper not the a element (.carousel-wrapper is the first parent of .overlay that has position other than static – if you don’t set your position explicitly it defaults to static).

    How to avoid this mistake in the future?

    If you want any element on your website to have an overlay (or in general you want to use position: absolute somewhere) you need to remember that this element is going to be positioned relatively to it’s parent that has positon: relative or position: absolute. If there’s no such element it’s going to position itself to <html>.

    It’s very well explained here:

    Remember that these values will be relative to the next parent element
    with relative (or absolute) positioning. If there is no such parent,
    it will default all the way back up to the element itself
    meaning it will be placed relatively to the page itself.

    (on absolute positioning)

    https://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

    Check the working demo:

    https://jsfiddle.net/thffcgqc/2/

    demo

    Login or Signup to reply.
  2. Wrap your anchor tags and overlay content in a separate div and style as follows. The reason is due to the fact that you are not using a margin-right for whitespace between boxes. You are simply using a fixed with li tags and the images leaves a gap in the parent container . If an overlay is placed, you will see the overlay over the white gap and the image (which is somewhat not what you want – I believe)

    See snippet below

    $('.carousel').slick({
      arrows: false,
      dots: true,
      slidesPerRow: 3,
      rows: 3
    });
    .carousel-wrapper {
      background-color: rgb(235, 235, 235);
      position: relative;
    }
    
    img {
      background-color: black;
    }
    
    .slick-slide {
      text-align: center !important;
    }
    
    .overlay {
      position: absolute;
      height: 100%;
      width: 100%;
      top: 0;
      left: 0;
      background: rgba(1, 1, 1, .35);
      transition: 0.2s;
      opacity: 0;
      background: red;
      z-index: 1;
    }
    
    .overlay:hover {
      opacity: 1;
    }
    
    .overlay-content {
      color: rgb(255, 255, 255);
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
    
    a {
      position: relative;
      display: inline-block;
    }
    
    a:hover .overlay {
      opacity: 1
    }
    
    .img_cont {
      position: relative;
    }
    <!DOCTYPE html>
    <html>
    
    <head>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css" />
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css" />
    </head>
    
    <body>
    
      <section class="carousel-wrapper">
        <div class="container">
          <div class="row">
    
            <ul class="col-md-12 carousel text-center">
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
    
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
              <li>
                <a href="#">
                  <div class="img_cont">
                    <img class="img-responsive" src="http://www.onedlq.com/wp-content/themes/komodo/img/no-image-140.jpg" alt="">
    
                    <div class="overlay">
                      <div class="overlay-content">
                        <h4>content</h4>
                      </div>
                    </div>
                  </div>
                </a>
              </li>
    
            </ul>
    
          </div>
        </div>
      </section>
    
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.js"></script>
    
    </body>
    
    </html>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search