Solution 1 :

U need to turn off overflow of the image. Also the image should be 100% width of its parent. The trigger button is in the container that closes, so you must move it away. And I used simple JS function to add a class to open it and close.

So essentially it should look like this:

const cover = document.querySelector('.cover');
const btn = document.querySelector('.close-btn');

btn.addEventListener('click', () => {
  func();
});

function func() {
  cover.classList.toggle('closed');
}
.cover {
  position: sticky;
  display: flex;
  align-items: flex-start;
  height: 275.25px;
  width: 275.25px;
  overflow: hidden;
}

.cover img {
  width: 100%;
}

.close-btn {
  position: relative;
}

@keyframes closeCover {
  0% {
    height: 275.25px;
  }
  100% {
    height: 0px;
  }
}

.cover.closed {
  animation: closeCover 1s linear forwards;
}
<div>
  <div class="cover">
    <img src="https://media.istockphoto.com/vectors/thumbnail-image-vector-graphic-vector-id1147544807?k=20&m=1147544807&s=612x612&w=0&h=pBhz1dkwsCMq37Udtp9sfxbjaMl27JUapoyYpQm0anc=" alt="close cover" id="close">
  </div>
  <button class="close-btn">toggle</a>
</div>

Solution 2 :

Move the image inside a label with overflow: hidden. then connect to the label an input type="checkbox" that can have a checked-state:

.cover {
  position: sticky;
  display: flex;
  align-items: flex-start;
  height: 275.25px;
  width: 275.25px;
}

/* hides the checkbox */
.cover input {
  display: none;
}

.cover label {
  overflow: hidden;
}

#img:checked + label {
  animation: closeCover 1s linear forwards;
}

@keyframes closeCover {
  0% {
    height: 275.25px;
  }
  100% {
    height: 0px;
  }
}
<div class="cover">
  <input type="checkbox" id="img">
  <label for="img"><img src="https://via.placeholder.com/100.jpg"></label>
</div>

Problem :

I’m trying to close and open an image on HTML with a click on a button but it’s not working.

There’s my code:

.cover {
  position: sticky;
  display: flex;
  align-items: flex-start;
  height: 275.25px;
  width: 275.25px;
}

@keyframes closeCover {
  0% {
    height: 275.25px;
  }
  100% {
    height: 0px;
  }
}

.cover:active {
  animation: closeCover 1s linear forwards;
}
<div class="cover">
  <img src="./images/arrow.svg" alt="close cover" id="close">
  <a href=""><img src="./images/Fiure-de-vie.jpg" alt="cover" id="cover"></a>
</div>

I’m trying to close and open an image with HTML/CSS.

Comments

Comment posted by A Haworth

Could you describe exactly what ‘not working’ means here. Also have you checked that you definitely have the right paths for those images?

Comment posted by leoprosy

@AHaworth the paths are good it’s not the problem I wanted to mean that the image doesn’t go to 0 of height on click

Comment posted by tacoshy

For that you should use either JS or a CSS-only solution a “hack” by wrapping the image in a label connected to a checkbox

Comment posted by A Haworth

I can’t make sense of your code (even correcting for the fact you are using a class rather than the close id). When close is active you give it an animation, but it’s the other img you want to shrink to nothing on the click isn’t it? And do you want it to remain shrunk?

Comment posted by leoprosy

@AHaworth yes. If possible I want to shrink the div and the image in the same time. I tried with javascript but not working

Comment posted by tacoshy

You should not use the

Comment posted by A Haworth

@tacoshy could you explain the reson for not using double quotes in JS? I had not heard that before (and actually thought JS strings were marginally preferably double quoted because of the single quote also being used so much in things like isn’t in English.

Comment posted by mdabrowski

@tacoshy Thanks for contributing. I’m still learning and in fact I use stack to learn even more. All the tips are implemented now.

Comment posted by tacoshy

@AHaworth especially with

By