Skip to content
Advertisement

Resize thumbnail image for mobile site

I’m making a website for mobile and for desktop, but I want images to show mobile images if loading on a mobile device. For example, I have a list of images of thumbnails of food types like:


(source: whereshouldieat.ie)

But I also have images where there is a mobile version:


(source: whereshouldieat.ie)

The current CSS is:

JavaScript

I have seen in other locations where you can have redirects on images using CSS like this: Changing Image depending on Mobile or Desktop HTML & CSS

But because the images are all automatically generated using this function:

JavaScript

I’m not sure if I could automatically use CSS due to the images normally having to include the URL name in the CSS for the redirect. Could anyone advise on a way to resolve this?

Advertisement

Answer

As it looks like you are using Bootstrap 3 here you can add both of the images to the page using PHP and then hide the one you don’t need on mobile and the opposite for desktop. This can be achieved using the hidden responsive utilities (http://getbootstrap.com/css/#responsive-utilities) which have options for extra small, small, medium, large and extra large screen sizes.

Desktop:

JavaScript

Mobile (note the output of the mobile image):

JavaScript

So your final code for those two lines would be:

JavaScript

This will obviously increase load time as both images must load.

Another option is to use jQuery and change the image source based on the screen resolution. This only adjusts it one way so if the users screen is larger than 768 it will remove -Mobile.jpg from the image source and replace it with just .jpg. However if the user resized their browser window back down it doesn’t revert it. You could however add this functionality but I haven’t included it here.

JavaScript

This method will favour mobile users as the mobile version of the image is expect to be loaded by default and swapped out for any devices larger than 768px.

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement