<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.5);
  /* 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;
}
.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;
}
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.