What are the best techniques for displaying high-resolution images on mobile devices and lower-resolution images on desktop computers?

I implemented a typical bootstrap grid to display a row of images, with 4 images in desktop view and 1 image in mobile view. Here is what it looked like:

<div class="row">
    <div class="col-md-3 col-sm-12">
        <img src="image1_src">
    </div>
    <div class="col-md-3 col-sm-12">
        <img src="image2_src">
    </div>
    <div class="col-md-3 col-sm-12">
        <img src="image3_src">
    </div>
    <div class="col-md-3 col-sm-12">
        <img src="image4_src">
    </div>
</div>

The rendered size of the images in browsers was approximately 250px*250px. However, this resolution proved to be insufficient for mobile devices as the images appeared almost fullscreen on smaller screens. To address this issue, I opted to use images with resolutions around 500px*500px, which resulted in improved image quality on both mobile and desktop views. Nevertheless, this decision led to a Google PageSpeed warning stating:

Image elements do not have explicit width and height

To tackle this problem, I decided to utilize the <picture> tag to implement two different versions of each image - a smaller one for desktop and a larger one for mobile:

<div class="row">
    <div class="col-md-3 col-sm-12">
        <picture>
            <source media="(min-width:701px)" srcset="250px_image1_src">
            <source media="(max-width:700px)" srcset="500px_image1_src">
            <img src="500px_image1_src">
        </picture>
    </div>
    <div class="col-md-3 col-sm-12">
        <picture>
            <source media="(min-width:701px)" srcset="250px_image2_src">
            <source media="(max-width:700px)" srcset="500px_image2_src">
            <img src="500px_image2_src">
        </picture>
    </div>
    <div class="col-md-3 col-sm-12">
        <picture>
            <source media="(min-width:701px)" srcset="250px_image3_src">
            <source media="(max-width:700px)" srcset="500px_image3_src">
            <img src="500px_image3_src">
        </picture>
    </div>
    <div class="col-md-3 col-sm-12">
        <picture>
            <source media="(min-width:701px)" srcset="250px_image4_src">
            <source media="(max-width:700px)" srcset="500px_image4_src">
            <img src="500px_image4_src">
        </picture>
    </div>
</div>

However, a new challenge arose where the browser automatically selected the higher-resolution image in desktop mode, ignoring the lower-resolution option.

Answer №1

There are two distinct issues at play here:

Firstly, Google is raising concerns about providing explicit width and height for images. By including these values, the browser can allocate the necessary space in the layout even before the image is fully loaded. Without specified dimensions, the space allocated will be zero until the image loads, potentially causing a layout shift.

Secondly, it's important to offer images in various resolutions to accommodate different screen sizes, especially with the high resolution screens found on modern smartphones. Using the picture element allows you to specify multiple image options and let the browser decide which one to display based on the current layout and resolution. The last img tag within the picture element serves as a fallback option if none of the media queries match, hence why it is selected by the browser.

For more information, refer to https://www.w3schools.com/tags/tag_picture.asp

Another approach to handling different resolutions is through using srcset. A comprehensive explanation can be found on this page: https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on making a material-ui grid fill up the entire screen

Currently, I am working on developing a layout that resembles most editor/IDE setups using material-ui and react. In this layout, I have a top bar, a bottom bar, side panels on both sides, and a center area. My main concern is how to ensure that this grid ...

I created a footer using Bootstrap 4 that is perfectly centered on the desktop version but unfortunately appears off-center on mobile devices

.footer_img{ padding-left: 30px; width:170px; height:63px; align-self: center; } .footer{ margin-bottom: 41px; } <div class="footer container"> <div class="clearfix"> <span class="float-left"><a href="#"><img class="footer_img ...

showcasing real-time webcam information within an HTML web application

I have successfully integrated my webcam data live into my web application using the following code snippet. The live feed from my webcam is now visible on the webpage. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

Is it possible to include a button in Bootstrap 4 Popper content?

Adding a button to the Popover content of the bootstrap in my project is proving challenging as I cannot seem to locate the button element on the page. The current version being used is v4.3.1, yet it seems this issue can be omitted in v3.3.7. How can I re ...

A <div> with relative positioning is inhibiting the visibility of absolutely positioned elements without any visible overlap

Recently, I encountered a strange issue on my webpage: Image of page The lines on the left side are supposed to be displayed behind or on top of the text box. However, when I increase their z-index, the lines extend all the way to the top of the page and g ...

Experiencing difficulties with my .css formatting following the installation of bootstrap in Angular

I've been immersing myself in Angular and encountered an issue that has me stumped. Despite using the latest version of Bootstrap in my Angular project, I'm facing challenges with my styles.css file that was defined prior to installing Bootstrap. ...

My attempt to use the Redux method for displaying data in my testing environment has not yielded

I've been attempting to showcase the data in a Redux-friendly manner. When the advanced sports search button is clicked, a drawer opens up that should display historical data when the search attributes are selected. For this purpose, I implemented the ...

What is the most efficient way to create a vertically stacked grid with minimal reliance on JavaScript?

When using an AngularJS ng-repeat, I encounter an issue with aligning elements on a new line. Instead of aligning the element above it, all elements on the same 'row' align with the lowest-hanging element. Even though I am using Bootstrap, I cou ...

Problem encountered when closing a lightbox on ASP.net using C# during page load

Can you explain the events that are triggered when the ASP.NET page load event occurs? I am currently using a lightbox for some insertion tasks, and after the insertion is complete, I want the parent page to reload with the new value displayed in the gri ...

Is it possible to create a grid by arranging each statement with a specific format?

After fetching and parsing some data, I have utilized the 'each' function to display it: $.ajax({ url : 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=' + encodeURIComponent('htt ...

"Illuminating the way: Adding a search bar to your

Looking to enhance my WordPress blog with a search bar, I opted for a generic HTML approach over using get-search-form(). Here is the code snippet from my theme: <div class="input-group"> <input type="text" class="form-c ...

Depending on the size of the screen, the layout of my HTML elements adjusts and

I'm struggling with positioning HTML elements on my websites. Despite knowing it's a simple issue, I find myself facing this problem with every website I create, as I am still new to web development. .artstation_b{ position: absolute; lef ...

Obtaining the value of an item in an unordered list

Hi everyone, I have been trying to extract the value of <li> elements that display images horizontally. Below is the HTML code I am working with: <div id="layoutInnerOptions"> <ul id="navigationItemsContainer" class="layouts_list"> <l ...

Expanding beyond the dimensions of the webpage, HTML CSS Height set to 100% creates a unique

I'm struggling with the CSS on a page I'm working on. I have a header and main content each enclosed in div tags, with the header set to a height of 250px and the main content set to a height of 100%. I've also set the html and body heights ...

Keeping a dangerouslySetInnerHTML div separate from the global CSS to prevent any potential risks

As I render an email HTML string with all its styles defined in-line in my NextJS React app, I notice discrepancies in the styling: <div id='emailContent' dangerouslySetInnerHTML={{ __html: sanitizedHtml }} /> It seems like there is a clas ...

Steps for importing selenium web elements:

Can all of these ... tags be extracted simultaneously? I aim to retrieve every ... from that particular webpage! Web address: 'https://school.programmers.co.kr/learn/challenges?order=recent&page=1' ...

Scrolling is disabled on mobile devices

One issue I am encountering with my website is related to scrolling on mobile devices. Sometimes, the website freezes when trying to scroll, and even swiping a finger across the device does not move the website properly. However, scrolling works fine on de ...

Transitioning from traditional desktop WPF Applications to HTML5-based solutions

Our small team within the department has been utilizing WPF for quite a few years now, consisting of just myself as the programmer and a designer. We primarily develop business applications for our company, such as shipment tracking tools and more. Lately ...

Generating interactive buttons that delete a row in a data table

I am facing an issue with creating delete buttons for each row in my database. Despite the data being displayed correctly and the table functioning properly, when I click on the button to delete a row, it disappears temporarily but reappears upon reloadi ...

Enhance your website design with a custom content scroller using Bootstrap and

Having trouble getting this plugin to work with bootstrap on my website. Does anyone have a solution for this issue? Alternatively, does anyone know of another approach I could take? UPDATE: I managed to fix the problem myself. Add the following code ...