Search code examples
htmlcsshtml-head

Is there a css option that links an html image to itself?


So this:

<html>
    <head>
        <style>
            img{href:self}
        </style>
    </head>
    <img src="./Sampleimage"/>
</html>

would basically be the code I need, but since I don't know how or even if there is an option to do this, I figured, I have to ask someone more intelligent than me.

I kinda have to do this because I have about 200 images in this html Document, and every single one of them has to link to itself. So a seperate <a> tag for every image wouldn't be very stylish.


Solution

  • Expanding off of WillardSolutions' comment...

    document.getElementById("myImg").addEventListener("click", function() {
      window.open(this.getAttribute("src"));
    });
    .clickable {
      cursor: pointer;
    }
    <img id="myImg" class="clickable" src="https://www.w3schools.com/images/compatible_chrome.gif"/>

    Open your browser console to see the opening of the URL being blocked...

    If you want it to open in a new window/tab use:

    window.open(this.getAttribute("src"), '_blank');