skip to Main Content

I have troubles regarding a slider I have on my website.

Currently it’s a slider with the <img> element. Problem I encounter now is that on mobile the large images get loaded as well. Now I want to load specific (smaller) images for mobile so that my website is faster on these devices.

A solution I had was using the media queries in combination with CSS background image. This is perfect to use because I can load the images with CSS and thus can use media queries to select the image based on screen size.

But the problem is is that I want to add the images to the SEO, they are essential for my website and I read everywhere that if you have such images, you need to use the <img> element. So that the SEO can work and include these images in the content. Also I cannot add ALT tags to background-image.

Another solution is rendering two sliders, one for mobile and one for desktop, and hiding the slider you don’t want to see. Problem I have with this solution is that both of the sliders need to render, thus decreasing performance.

Is there a solution that I’m missing here? In my understanding you cannot change images in a <img> element with CSS media queries.

2

Answers


  1. You can use the picture element. As the Mozilla Developer Network says, “[This element] serves as a container for zero or more elements and one element to provide versions of an image for different display device scenarios“. I think it does the trick. Here you are another good article that explains how to use this element to achieve the result you need with your responsive images.

    Login or Signup to reply.
  2. The picture element is a really nice modern solution. But if you want something more cross-browser, consider an approach with JavaScript. My solution for some websites was using sets of images with regular suffixes, for example image.jmg, image-medium.jpg, image-small.jpg, and a script checking the screen resolution. In the HTML only small images are included, but if the script finds the screen is big enough, it updates their src with corresponding suffixes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search