Skip to main content
Inspiring
May 2, 2022
Question

How To Change Header Image on Mobile Devices?

  • May 2, 2022
  • 3 replies
  • 1264 views

I have a detailed header image on my site, but when viewed on a smartphone, it is too small to see. I have a simpler image that I would like to display when viewed on a mobile device. Is it possible to automatically switch images?

 

www.winvoices.com

    This topic has been closed for replies.

    3 replies

    Liam Dilley
    Inspiring
    May 5, 2022

    Bit suprised with the comments here.

     

    Picture is a really good implementation. This works for static sites and some use cases if your using a CMS but not an otpion in other CMS's lets say you have a shopify site as easily in solutions.

     

    Sever multiple images and CSS changing is an otpion but as Nancy pointed out not great.

     

    But you also need to consider DPI as well as screen size and image quality.

     

    Picture is really there not just to server smaller images but more over different aspect ratio images to suit the different devices.  A long landscape image on a mobile you can only scale down and it looses itself or your crop and you loose parts of the image. Serving up a portait image variation helps with the design. That is the true use.

     

    On a more basic level you serve a high quality image that when scaled will look sharp still on a mobile device and you do that changes to that image/elements around it or change a background images values such as background size and position through media queries. 

    Nancy OShea
    Community Expert
    Community Expert
    May 2, 2022

    As Jon said, use the <picture> tag with SRCSET.  This serves the right size image to targetted devices without forcing them to absorb unecessary bandwidth.

     

    The less preferred option is to serve both images to all devices and use display:hidden in a media query. 

     

    Nancy O'Shea— Product User & Community Expert
    B i r n o u
    Community Expert
    Community Expert
    May 2, 2022
    quote

    The less preferred option is to serve both images to all devices and use display:hidden in a media query. 

    By @Nancy OShea

     

    I'm not at all in favor of such a solution, just because that do mean that the enduser will download all the images which some how bandwith consuming... if PICTURE can't be set (I don't know why) MQ are a better solution to go

    https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries

    Legend
    May 2, 2022

    so please, as I wasn't clear enough, let me rephrase my comment... using display:hidden is kind of really forbidden... MQs allow so many other things than just displaying or hiding contents.


    https://www.linkedin.com/pulse/why-responsive-websites-just-simple-step-website-birnou-s%C3%A9barte 


    quote

    so please, as I wasn't clear enough, let me rephrase my comment... using display:hidden is kind of really forbidden...


    By @B i r n o u

     

    No display: hidden; is NOT forbidden, we need it for modals, navigation items etc. It might be considered poor practice to swap out images though.

    Jon Fritz
    Community Expert
    Community Expert
    May 2, 2022

    That is a perfect use situation for the <picture> element: https://www.w3schools.com/tags/tag_picture.asp 

    Inspiring
    May 2, 2022

    I tried it, but once I include the <picture> code, the 2nd flag jumps to the next line!

    https://www.winvoices.com/index-test.php

    Legend
    May 2, 2022
    quote

    I tried it, but once I include the <picture> code, the 2nd flag jumps to the next line!

    https://www.winvoices.com/index-test.php


    By @sneedbreedley

     

    Your <div class="gridContainer"> is not closed - its missing the closing </div>

     

    Try adding that and see what happens.