What method does the browser use to select the srcset image for loading when no sizes attribute is included?

My website features an image that is displayed in various sizes depending on the browser dimensions:

  • Since the website is responsive, the image's width can range from 200 to over 1000 pixels, depending on the size of the browser window.
  • We aim to showcase the image in its native resolution on high-resolution devices, such as Retina displays.

Therefore, we have the image available in three resolutions, named i1.png, i2.png, and i3.png.

My desired approach is as follows:

<img src="i1.png" srcset="i1.png 420w, i2.png 840w, i3.png 1260w" />

In my view, the browser should be able to determine which image to load based on the provided information.

  • For a non-Retina device, the browser considers the width of the <img /> element and selects an image that matches or exceeds that width. For example, if the element is 600px wide, it will load i2.png.
  • On a Retina device, the browser calculates the required image width by multiplying the <img /> element's width by the device-pixel ratio. For a 2x device and a 600px image, it loads i3.png because 2 x 600 = 1200, necessitating an image width of 1260px.

Although the browser generally follows this logic, there are instances where it may load a larger image when a smaller one would suffice.

What criteria does the browser use to determine the appropriate image size to load?

  • Does it leverage CSS rules to ascertain the image's displayed width?
  • Or does it prioritize the width and height attributes on the <img /> tag, if available, over CSS?
  • How does it handle situations where certain CSS rules are not yet loaded?

It is important to note that I prefer not to utilize the sizes attribute, as it would require extensive media queries for various device-pixel ratios and browser window sizes. The goal is for the browser to independently determine the optimal image based on conditions.

I consulted the Mozilla Docs on this matter. They suggest using 2x descriptors in the srcset attribute if sizes is omitted. However, this approach does not align with my objective of having the browser choose the image based on both device-pixel ratio and responsiveness, rather than solely the former.

Answer №1

It appears that the browser is not following any CSS guidance when determining which image to load. This is likely due to the uncertainty of which CSS files have been loaded by the time the image loading process begins.

As a result, the browser treats every image as if it should be stretched to span the entire width of the page. For example, on a non-retina screen that is 600px wide, the browser would load the next largest image size (such as 840px) regardless of the image's actual display size on the screen.

On retina screens, the browser simply calculates the required image width by multiplying it with the device-pixel ratio.

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

What causes a header to appear transparent when it has a background color set in HTML?

I'm attempting to create a webpage that resembles Gmail. I have a header with a background color, but when I scroll, the content in the body becomes visible. Here is the view without scrolling: https://i.stack.imgur.com/UQ2sO.png However, while scr ...

When a user scrolls over an element with a data-attribute,

Looking to create a dynamic header effect on scroll? I have two headers, menu1 by default and menu2 hidden with display:none. In specific sections of my website, I've added a special attribute (data-ix="change-header") to trigger the header change. T ...

Arranging divs parallelly while incorporating components within

Striving to create a layout with 3 divs side by side in an HTML document, my initial attempt resulted in this: https://i.stack.imgur.com/CpdLX.png However, I encountered an issue where the div would shift down whenever text or other objects were added: ...

Using the "this" keyword in JavaScript to access the "rel"

Take a look at the JSFIDDLE , where you will notice that the rel attribute in the alert is shown as 'undefined' : var ItemTypeArray = $('input[name^=ItemType]:checked').map(function(){ alert(this.id + ' , r= ' + this.rel) ...

Utilizing Angular's ngShow and ngHide directives to hide spinner when no data is retrieved

I'm having an issue with the HTML code below. It currently displays a spinner until a list of tags is loaded for an application. However, the spinner continues even if no events exist. I want to make the spinner disappear and show either a blank inpu ...

What is the mechanism behind the functionality of padding percentage?

HTML <div class='container'> <div class='boxContainer'> The text inside this box should be centered vertically </div> </div> CSS .container { position: relative; height: 300px; wid ...

When additional elements follow, the button ceases to function properly in JavaScript

I am working on creating a text-based idle game that involves multiple buttons and text around them. However, I have encountered an issue where the functionality stops working when I try to add text after the "Work" button. The callback function is no lon ...

Jquery div mysteriously disappearing without explanation

I need some help with my image options. Whenever I hover over the image, the options appear below it. However, when I try to move down to select an option, they disappear. I have configured the slideUp function so that it only happens when the user moves a ...

How can I toggle the visibility of an item on click using jQuery?

I need to toggle the visibility of a specific div when clicking on an anchor. Here is the code I have written for this: jQuery('.mycart').click(function(e){ e.preventDefault(); var target = jQuery(".basket"); ...

.comment {Visibility:hidden;} is only functioning on one specific page

What could be the reason behind this function only functioning on one page and not the others? The URL in question is . Specifically, the functionality is only active on the contact page while all other pages still display a comment section. ...

Turn words into smiley faces without having to refresh the page

Recently, I came across an interesting feature on Facebook where text in messages is automatically converted into smiley faces without the need to reload the page. This got me thinking about how I could recreate this same functionality using jQuery and HTM ...

Highlighting the menu items when hovering over them in an ASP menu

Recently, I have been working on my menu code and here is what I currently have: <td><a href="Products.asp?isnew=true"><img height="21" border="0" src="images/productmenu/new_items<%if request.querystring("isnew")="" then%><%else%& ...

Tips for aligning navbar content in the center using Bootstrap

I'm currently working on creating a Bootstrap 4 navbar, but I'm having trouble positioning objects within the navbar. I want to center a search box and place a button on the right, but so far I have not been able to achieve this as the navbar is ...

What is the counterpart of `width: max(100%, fit-content)`?

In order for my div to adjust its size based on the content if the parent div is smaller than the content, and match the size of the parent otherwise, I have been trying to achieve the first scenario by using width: fit-content, and the second scenar ...

Adjust the path of an element as it decreases in size

Sorry for the weird title, but that's likely why I can't find the solution on my own. I'm experimenting with CSS animations and I want the form element to decrease in height from 100px -> 0, but I want it to collapse from the top to the ...

Retrieve the nearest attribute from a radio button when using JQuery on a click event

I have a query regarding the usage of JS / JQuery to extract the data-product-name from a selected radio button, prior to any form submission. Although my attempt at code implementation seems incorrect: return jQuery({{Click Element}}).closest("form" ...

Navigate through the parent elements and show the text if the child element is not present

I'm currently exploring how to iterate through all parent classes (.grid) and if there is no child div with the class (.image-container), then to display the (.content-container) within the same (.grid) section. HTML: <style> .grid .content-co ...

Is there a tool available to monitor the assets being loaded by a website?

During my site review of one of our properties, I noticed it is quite messy. The .js folder alone contains 123 files, and I am unsure of how many are actually being used since each page on the site calls specific dependencies in its own header. I am curio ...

Converting non www to www in React.js

I want to redirect users from http://example.com to http://www.example.com. If I'm using nginx + php, I can achieve this with the following configuration: server { listen 80; server_name example.com; return 301 http://www.example.com$req ...

Adjusting the grid for various mobile screen sizes

I am currently working on designing a user interface (UI) for mobile screens that includes a header, grid, and buttons. I want the UI to look consistent across all mobile devices. Here are screenshots from various mobile screens: Samsung S5 Pixel 2 XL I ...