<img src="https://picsum.photos/id/1016/200/100" alt="l'adorazione dei Magi" onclick="onClick(this)" class="modal-hover-opacity">
<!-- the onclick above and below should be in the js and  not inline -->

<div id="modaldiv" class="modal" onclick="this.style.display='none'">
  <span class="close">&times;</span>
  <div class="modal-content">
    <img id="modalimg" src="https://picsum.photos/id/1016/1000/500" alt="Pic">
  </div>
</div>

</div>
html,
body {
  margin: 0;
  padding: 0;
}
.modal {
  z-index: 99;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  overflow: auto;
  background-color: rgb(0, 0, 0);
  background-color: rgba(0, 0, 0, 0.8);
  /* display: flex; when shown*/
  display: none; /* hide initially*/
  justify-content: center;
  align-items: center;
}
.modal-content {
  margin: auto;
}

#modalimg {
  width: auto;
  height: auto;
  display: block;
  margin: auto;
  max-height: calc(100vh - 2rem);
  max-width: calc(100% - 2rem);
  object-fit: contain;
  border: 5px solid #000;
}
.close {
  position: absolute;
  right: 25px;
  top: 25px;
  z-index: 2;
  background: #000;
  color: #fff;
  cursor: pointer;
  width: 25px;
  height: 25px;
  display: flex;
  font-size: 1.5rem;
  justify-content: center;
  align-content: center;
}
.modal-content {
  animation: fade 3s ease;
}
@keyframes fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

#modalimg {
  animation: zoom 3s ease;
}
@keyframes zoom {
  from {
    transform: scale(0);
  }
  to {
    transform: scale(1);
  }
}
function onClick(element) {
  //document.getElementById("modalimg").src = element.src;
  // re-instate the above line in a working page.
  document.getElementById("modaldiv").style.display = "flex";
}
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.