Enhance Mobile Web Experience with RetinaJS for High-Res Images

With the advent of high-resolution screens on mobile devices, users have come to expect images that are sharp and detailed. This shift in expectations means web developers must ensure their images meet this high-quality standard. Images that looked fine a decade ago can now appear blurry on modern retina display screens, signaling a need for updated methods of displaying graphics on the web.

Different techniques can be employed based on the image’s intended use. For icons or logos, Font Icon and SVG are preferred for their scalability. Meanwhile, Media Queries are ideal for controlling image display through CSS.

If your goal is to incorporate high-resolution images directly within your website’s content, then Retina.js is your solution. Retina.js is a JavaScript library designed to simplify the process of serving high-resolution images without needing significant code adjustments. Let’s dive into how it works.

Understanding Retina Displays and Media Queries

Understanding Retina Displays and Media Queries

Apple revolutionized screen quality with its Retina display. This technology offers incredibly clear, vibrant, and detailed images. However,... Read more

How to Begin with Retina.js

Retina.js is a standalone solution that doesn’t depend on any external libraries. Simply download the Retina.js file and include it in your HTML. Alternatively, you can use a CDN, such as CDNJS.com, to link to the file like this:

 <script src="//cdnjs.cloudflare.com/ajax/libs/retina.js/1.0.1/retina.js"></script>

Retina.js provides two methods for serving high-resolution images on your website.

First Method

Following Apple’s convention, Retina.js initially used a method where high-resolution images are marked with an @2x suffix. For example, a high-resolution version of autumn.jpg would be named autumn@2x.jpg. From version 1.3.0 onwards, Retina.js added a new suffix, _2x, offering you an alternative naming convention, such as autumn_2x.jpg.

When a visitor with a high-resolution screen accesses your site, Retina.js searches for images with these suffixes on your server and replaces the standard images with their high-resolution counterparts. To facilitate this process, make sure to store the high-resolution images in the same directory as their standard versions.

Second Method

An alternative approach involves using the data-at2x attribute within the img tag as shown below:

 <img src="img/autumn.jpg" data-at2x="img-hi-res/image-autumn.jpg" alt="">

This method allows for different naming conventions or folder structures, bypassing the need for a server check and thus speeding up the process.

Try opening your website on both a regular and a high-resolution screen to see the difference in clarity and detail, as illustrated in the comparison below.

Image comparison with and without Retina.js
Utilizing LESS Mixins

Retina.js also offers a LESS Mixin, .at2x, for serving images through CSS. For instance, this code:

.social-icons {
 .at2x('image/icons.jpg');
}

…gets compiled into the following CSS:

.social-icons {
 background-image: url('image/icons.jpg');
}
@media (-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx) {
 .social-icons {
 background-image: url("image/icons@2x.jpg");
 background-size: auto auto;
 }
}

This feature can be quite useful for seamlessly integrating high-resolution images into your CSS styling.

Integrating Retina.js with WordPress

For WordPress enthusiasts looking to enhance their site’s imagery, the WP Retina 2x plugin is a perfect tool. Once installed and with your images uploaded, navigate to the Media > WP Retina 2x section on your dashboard. Here, you’ll find a list of your uploaded images ready for Retina display optimization.

Generating Retina Images in WordPress

By clicking the Generate button, the plugin will produce versions of your images with the @2x suffix for various sizes such as Thumbnail, Medium, Large, and any other custom sizes you’ve set up.

WordPress Plugin for Retina Images

Concluding Thoughts

As devices with high-resolution displays become increasingly common, it’s becoming crucial for web developers to adapt and support these technologies. Retina.js stands out as an all-encompassing solution for delivering high-resolution images on the web. Whether it’s using images with the @2x or data-* attributes in your content, utilizing .at2x Mixins for CSS, or leveraging a WordPress plugin, Retina.js equips you with the tools needed to meet and exceed modern web standards.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail