I need to use a different image for a mobile a tablet and a pc screen, and have managed to get it right for mobile and pc, but cant seem to get it right for the third image for tablet working with the code i got.
A better approach to swapping images based on screen size is the <picture> element.
Or you may find it easier to apply the image as a background.
But in this case, because the image is largely text and plain colour, with just one part an actual photo, I would take a different approach to this design.
I would crop the picture to be only the photo part. Then make the text part be actual text and the background colour be an actual background colour, then use css to arrange the layout.
The picture being smaller would not require to be swapped making it faster for all users. Also the text would be machine readable, making the page more accessible and semantic which would benefit both users and search engines.
Yes as Sam said the picture element is the way to go because if you use three foreground images which you show with media queries the browsers will still load all three images. If you used background images then only one image would get loaded depending on the browser width.
The picture element is much better as the browser just loads the one image. If you don’t need ‘art direction’ then srcet is a better way to go although support in IE is only from edge onwards so a fallback will be needed.
Just for information your original question could be answered like this.
Here is a rough example. In this I use a background image in the banner.
In the end I did end up swapping the image in a query because otherwise it goes under the text on small screens and the purple text clashes with the purple flowers and gets lost. So I load a smaller version and re-position it.
It’s not a pixel perfect match for your site, but a rough approximation to show how it could be done to be more responsive and semantic.
I’ve never seen both min and max used before so I tried a bit of experimenting myself.
I’ve learned that I obviously don’t understand how they work as much as I thought I did, so I’m looking forward to seeing more about this as well.
The only approaches I’ve used that work are JavaScript that uses height and width, finds the acceptable max and scales accordingly.
But I would be interested in learning a CSS-only way of doing this as well.
Ye its an awkward one, because what I’m trying to do is set the max-width of the image, whilst its being dictated to by the min-width of the screen width.
I’m probably miles off mind, but ye I’m looking to see what options there are if any
Just use media queries and target the image at the appropriate points. The image that will be visible will be the one in the srcset and you can control that via the media query if you need to. If I understand you correctly of course
It all seems a bit elaborate to me as I rarely have to go to those lengths and a simple max-width does the trick. Many times simpler is better so don;t over-think the solution.
You can’t set a max-height and a max-width on the image anyway because that will break aspect ratios. It’s either or.