0

Code is like:

<a href="http://localhost/link-a"><img src="a.png"></a>

The image file a.png is 100x100 but on retina screen it's very ugly so I can generate a 2x PNG file. But how do I make it work in this HTML code?

AGamePlayer
  • 7,404
  • 19
  • 62
  • 119

2 Answers2

1

Best option would be to use svg image which would fit in all resolutions.

But for your current code, you can use media query

HTML:

<a href="http://localhost/link-a"><img src="a.png" class="normalDisplay"><img src="abiggerresolution.png" class="retinaDisplay"></a>

In your CSS:

@media 
(-webkit-min-device-pixel-ratio: 2), 
(min-resolution: 192dpi) { 
   img.normalDisplay {
      display: none;
   }
    img. retinaDisplay {
      display: block;
   }
}

Pure JS solution:

var retina = window.devicePixelRatio > 1;

if(retina) {

 document.querySelector("img.specifyclassname").src="mention your higher resolution image link";

}

Another option would be: Use retina.js link.

The script will check your server to see if you have any image source with @2x at the end. It will replace that image with the higher resolution image.

mohamedrias
  • 18,326
  • 2
  • 38
  • 47
1

I'll suggest to go for SVG if you are using icons. However if you want to use image you can use srcset attribute.

<img src="image-src.png" srcset="image-1x.png 1x, image-2x.png 2x, image-3x.png 3x, image-4x.png 4x">

see can i use..

Kheema Pandey
  • 9,977
  • 4
  • 25
  • 26