Search code examples
htmlcsshoverimage-galleryportfolio

How can I add a link to the expanded images in this gallery without "breaking" the animation/effect?


I'm trying to mimic a portfolio category page using the codepen snippet linked. I want to add links to the images or at least each box but everywhere I place the respective href element and closing tag it breaks the gallery one way or another.

Where should I be placing the tag to link the 4 sections individually?

https://codepen.io/knyttneve/pen/YgZbLO

  HTML:
    <div class="container">
  <div class="box">
    <img src="https://source.unsplash.com/1000x800">
    <span>CSS</span>
  </div>
  <div class="box">
    <img src="https://source.unsplash.com/1000x802">
    <span>Image</span>
  </div>
  <div class="box">
    <img src="https://source.unsplash.com/1000x804">
    <span>Hover</span>
  </div>
  <div class="box">
    <img src="https://source.unsplash.com/1000x806">
    <span>Effect</span>
  </div>
</div>

    CSS:
    .container {
  display: flex;
  width: 100%;
  padding: 4% 2%;
  box-sizing: border-box;
  height: 100vh;
}

.box {
  flex: 1;
  overflow: hidden;
  transition: .5s;
  margin: 0 2%;
  box-shadow: 0 20px 30px rgba(0,0,0,.1);
  line-height: 0;
}

.box > img {
  width: 200%;
  height: calc(100% - 10vh);
  object-fit: cover; 
  transition: .5s;
}

.box > span {
  font-size: 3.8vh;
  display: block;
  text-align: center;
  height: 10vh;
  line-height: 2.6;
}

.box:hover { flex: 1 1 50%; }
.box:hover > img {
  width: 100%;
  height: 100%;
}
 

Solution

  • Assuming you want to have a link for each item.

    HTML Changes

    Added anchor tags to each .box div

    <a href="#"></a>
    

    CSS Changes

    .box {
      position: relative;
    }
    
    .box>a {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
    }
    

    .container {
      display: flex;
      width: 100%;
      padding: 4% 2%;
      box-sizing: border-box;
      height: 100vh;
    }
    
    .box {
      position: relative;
      flex: 1;
      overflow: hidden;
      transition: 0.5s;
      margin: 0 2%;
      box-shadow: 0 20px 30px rgba(0, 0, 0, 0.1);
      line-height: 0;
    }
    
    .box>img {
      width: 200%;
      height: calc(100% - 10vh);
      -o-object-fit: cover;
      object-fit: cover;
      transition: 0.5s;
    }
    
    .box>a {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
    }
    
    .box>span {
      font-size: 3.8vh;
      display: block;
      text-align: center;
      height: 10vh;
      line-height: 2.6;
    }
    
    .box:hover {
      flex: 1 1 50%;
    }
    
    .box:hover>img {
      width: 100%;
      height: 100%;
    }
    <div class="container">
      <div class="box">
        <a href="#"></a>
        <img src="https://source.unsplash.com/1000x800">
        <span>CSS</span>
      </div>
      <div class="box">
        <a href="#"></a>
        <img src="https://source.unsplash.com/1000x802">
        <span>Image</span>
      </div>
      <div class="box">
        <a href="#"></a>
        <img src="https://source.unsplash.com/1000x804">
        <span>Hover</span>
      </div>
      <div class="box">
        <a href="#"></a>
        <img src="https://source.unsplash.com/1000x806">
        <span>Effect</span>
      </div>
    </div>