Can I use a media query in internal CSS, and will it avoid loading the background images I define?

Can I use a media query in internal CSS, and will it avoid loading the background images I define?

I’m working on a responsive site where each page will have a large hero image, defined in the CMS. I’d like to avoid having to download that background image on mobiles.
The only way I can think to do it is to have some inline CSS in the head of the page like so:

First, can I use media queries in inline CSS?
Second, will this avoid downloading the image on mobiles?
Third, is there a better way?
Thanks

Solutions/Answers:

Solution 1:

Yes, you may use media queries in a <style> tag. The image is only loaded if the CSS requires it to be, so if nothing matches the selector then it won’t bother loading the image.

It would probably be better to include the media query in your external CSS file, though. There’s no reason to include it inlline.

Related:  How to activate the Bootstrap modal-backdrop?

Solution 2:

No, it appears that you cannot inline @media tags:

Please refer to this:
Is it possible to put CSS @media rules inline?

Solution 3:

You might have better luck with a two-step media query, one that begins in CSS but ends in jQuery. Label your divs with an ID so jQuery can find them. The process is explained in detail here: http://www.fourfront.us/blog/jquery-window-width-and-media-queries

References