Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using @media queries, does a phone load non-relevent queries and images?

If I base my CSS on mobile styling, then use @media queries for gradually larger displays (tablets, desktops etc), will the mobile devices use the desktop styles?

I believe that typically, mobile devices will load all images even if they don't apply to its own particular media size. Meaning it will load all images and hide ones not matching its query-based stylesheet.

What I am trying to do is use one background for the larger version of the site:

.splash {     background: #1a1a1a url('/assets/imageLarge.png') no-repeat; } 

and another for the mobile version:

.splash {     background: #1a1a1a url('/assets/imageSmall.png') no-repeat; } 

If I apply the mobile CSS before any media queries, and add the large media CSS below using a query like @media screen and (min-device-width: 481px) {...}, will mobile devices load the large image too?

like image 805
ChaBuku Bakke Avatar asked May 20 '13 19:05

ChaBuku Bakke


People also ask

Do media queries work on mobile?

As we discussed in this post, you can use media queries for mobile and other devices by adding a few lines of CSS to your theme's style. css file. You can define these queries based on common screen sizes, as well as apply conditions for hiding and moving certain elements.

How does @media work in CSS?

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.

What is @media rule?

The @media rule is used in media queries to apply different styles for different media types/devices. Media queries can be used to check many things, such as: width and height of the viewport. width and height of the device. orientation (is the tablet/phone in landscape or portrait mode?)


1 Answers

Behaviour is browser depended but iOS Safari and Android Chrome will respect the media queries and only download the background images for the applicable media queries.

If you want to inspect this behaviour, try loading the page with Mobitest (http://mobitest.akamai.com/) or a tethered device.

If you use separate CSS files (and I'd urge you not too) the browser will download each of them even if it doesn't need them, this is a limitation of the CSSOM. Some browsers e.g. WebKit and Blink based ones prioritise the stylesheets so the ones needed to render the page are downloaded first and the others at some point later.

One thing to watch out for is display:none on content images as it won't prevent download in many situations. Tim Kadlec explores this more here: http://timkadlec.com/2012/04/media-query-asset-downloading-results/

like image 175
Andy Davies Avatar answered Oct 03 '22 19:10

Andy Davies