5

So im trying to change the image depending on if the user is on mobile or desktop version.

I have two different images, the one with an "m" in the end is a mini-version which is for desktop, and the other is for mobile. I cant get it to work though.

Here's some code:

HTML (Using Razor, so C# code works):

<img id="ifMobile1" src="images/arts/IMG_1447m.png" alt="">

CSS:

#ifMobile1 {
    background-image: url(/images/arts/IMG_1447m.png)
}

@media all and (max-width: 499px) {
    #ifMobile1 {
        background-image: url(/images/arts/IMG_1447.png)
    }
}

Help me please.

5 Answers5

16

If you want to live in the future, the <picture> element is the way to go. It still has really bad browser support (only blink based browsers, and firefox beta as of now). The good news is that it falls back to a dumb <img> tag, so no harm done except a little slower loading if it's not supported.

Alright, so how does it work?

Most of this example is taken from html5rocks with some modifications

A picture element looks like this:

<picture>
  <source 
    media="(min-width: 650px)"
    srcset="images/kitten-stretching.png">
  <source 
    media="(min-width: 465px)"
    srcset="images/kitten-sitting.png">
  <img 
    src="images/kitten-curled.png" 
    alt="a cute kitten">
</picture>

Try the example for yourself at http://googlechrome.github.io/samples/picture-element/, simply resize the width of the browser to see the kitten change.

The cool thing about the picture element is that it allows you to specify media queries to each of the <source> elements. The last <img> is shown if no source is matched, or if the picture element is unsupported.

Henrik Karlsson
  • 5,559
  • 4
  • 25
  • 42
  • Ahh the future... looks awesome. – Aaron Mar 10 '15 at 14:35
  • Yes, the picture element has less support than srcset, but I don't see it as srcset version 2. They "kind of" do the same thing, but in different ways. srcset is better if it's the same image with different resolutions - the browser could then decide for itself what image to load depending on resolution, pixel density, and probably network speed in the future. The picture element on the other hand you decide the images. This can be good or bad. If you want a different image on smaller devices it's the way to go. – Tony Gustafsson Mar 11 '15 at 10:16
  • @tony.gustafsson Oh thanks for the info! I thought the picture element was the thing that would stay. Do happen to know if one of them is on the way out? – Henrik Karlsson Mar 11 '15 at 10:25
  • @Ineentho No I think they are both here to stay :) There are parts of these standards that is not yet implemented in any of the browsers, but it will be awesome when we can use it better. – Tony Gustafsson Mar 11 '15 at 10:28
9

Another trick would be to have two img tags, and hide one depending on the device.

HTML

<img id="img1" src="images/arts/IMG_1447.png" alt="">
<img id="img2" src="images/arts/IMG_1447m.png" alt="">

CSS

#img1 {display:block;}
#img2 {display:none}

@media all and (max-width: 499px) {
    #img1 {display: none;}
    #img2 {display: block;}
}
WebbySmart
  • 666
  • 6
  • 12
  • 5
    Display none does not prevent the browser from loading the image still. So this is actually worse as both images are loading on every device. – Todd Padwick Sep 29 '17 at 09:05
3

Updated: Please have a look at http://jsfiddle.net/p96denv4/2/

Here's what you would do.

HTML

<div id="ifMobile1"></div>

CSS

#ifMobile1 {
    background-image: url(/images/arts/IMG_1447m.png)
    width: set your width  ;
    height: set your height  ;
}

@media all and (max-width: 499px) {
    #ifMobile1 {
        background-image: url(/images/arts/IMG_1447.png)
        width: set your width  ;
        height: set your height  ;
    }
}
WebbySmart
  • 666
  • 6
  • 12
  • Try display: block and also make sure it is pointing to the right path of your image. Is your stylesheet in your main root? – WebbySmart Mar 10 '15 at 14:28
  • Please see my JSFIDDLE - http://jsfiddle.net/p96denv4/2/ Your background image should be showing. I don't think you can really use img tag to change the image source for mobile at this day and age. Due to browser support. – WebbySmart Mar 10 '15 at 14:47
0
img#id {
    content:url(http://example.com/image.png);
}

this may be of use to you, define each of your images in the respective media queries.

Jack hardcastle
  • 2,748
  • 4
  • 22
  • 40
-3

you can't change an image source like that. You'll need to use a div with the original as a background image for this to work.

<div id="ifMobile1" style="background-image: url(images/arts/IMG_1447m.png);"></div>
Aaron
  • 10,187
  • 3
  • 23
  • 39