Next, let’s add some CSS code for the Retina image. We’re likely to switch the normal image to some Retina image when the user is on the Retina display.
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) digital)no-repeat width: 778px height: 519px margin: auto
Our media query begins with < @media query statement followed by only, which means only the screen device must execute the code. Then we added min-device-pixel-ratio, which means the screen must be equal or greater that two.
We also added -webkit browser prefix to make sure that our code will work on older Safari browsers. Congratulations!
You just used Media Queries replacement technique. Each time a user will view your web page on their Retina display device, the non-Retina image will be replaced by the Retina image.
jQuery Retina Display Optimization
We can also use jQuery to replace images if the user is using a high density device. For this tutorial, we’re going to take advantage of the functionality of retina.js. Retina.js is an open source script that makes it easy to serve high-resolution images to devices with retina displays. Let’s try this out.
Create a new HTML file and name it jqueryRetina.html. On the head section, place the jQuery library source link from Google.
Next, create a new folder for our Retina folder and name it js. Now let’s download retina.js and then place the link code below before your closing tag.
Place the following code on our body section. This will include our basic retina image markup with a class of imgRetina.
Afterwards, inside our internal/external CSS, place this code for our non-Retina image. This contains the source of the image together with its width and the height. Then, in the end, we just center the image using margin: 0 auto.