center (v and h) an expanded element

It is not easy to center vertically and horizontally an expanded element in a page.

I found this code working for me:

.expansible {max-width: 10vw; 
    transition: all 1s; 
    transition:transform 0.25s ease;}
.expanded {
    max-width: 20vw;
    transform:scale(2.6);
    position: absolute;
    left: 30%;
    top: 25%;
    outline:0;
}    

The image is expended with this js code (put at the end of a file):

document.querySelectorAll('img.expansible').forEach(imageElement => {
  imageElement.addEventListener('click', e => {
    e.preventDefault()
    imageElement.classList.toggle('expanded')
  })
})

and adding the class expansible to the images you want expansible.

expansible image with image-map

We managed to add an image map to the on-click expansible image.

This is the code:

<style type="text/css">
img[usemap] { zoom: 12%;}
img[usemap]:focus {zoom: 50%;}
</style>
<map name="mymap">
  <area shape="rect" coords="1294,253,1420,434" title="mount" />
  <area shape="rect" coords="1709,296,1835,477" title="tree" />
</map>
<img tabindex="0" src="path-to/myimage" usemap="#mymap" />

The problem is that to close the expansed image you must click either on a map area or outside the image.

on-click expansible image

We were looking for the simplest possible solution to enlarge an image on click, without javascript and with the least amount of code possible.

a first attempt

At first we found the pseudo-class focus as a solution: with this css code img:focus and in html (inside the img tag) tabindex="0".

In this way, the image expanded on click, but to make it return to the initial size, it was necessary to click outside the enlarged image. As in the following example:

an important step

Therefore we had to add other code, so that we could return to the original, small, size, clicking inside the image. We had to use the input tag.

This is the result:

This is the code:

<style type="text/css">
input:checked + label > img {max-width: 200px; transform:scale(1.0); left: 0px;}
input {display: none;}
img {
    max-width: 200px;
    transition: all 1s;
    transition:transform 0.25s ease;
}

img:focus {
    max-width: none;
    transform:scale(2.8);
    position: relative;
    left: 5%;
    outline:0;
}
</style>
<p>This the result:</p>
<input type="checkbox" checked id="myimage" />
<label for="myimage">
<img tabindex="0" src="path-to-my-image/myimage.jpg" width="200px" />
</label>

fine tuning: adding an image map

We speak about this new feature in another article.