For a long time, when I had to create a website with a lot of different pages (and with every pages having a hero section with a different background image), I used to do like this:
<div class="hero" style="background-image: url(my-dynamic-img.jpg)"></div>
And the following css:
.hero {
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
The image would have been loaded dynamically for each pages. In a CMS for exemple, the client would have been able to change images on it's own, without changing the css
But now, I realize it's not good at all, because you end up with loading the same image on every device (mobile, tablet, desktop...) .
So I'd like to have your opinion on the best way to do this: having 3 different images (hero-mobile.jpg, hero-tablet.jpg and hero-desktop.jpg) for the same background and target the good one automatically. The declaration of the 3 images has to be in the HTML, not in the css (so the client can change it at any time)
Responsive design is a graphic user interface (GUI) design approach used to create content that adjusts smoothly to various screen sizes. Designers size elements in relative units (%) and apply media queries, so their designs can automatically adapt to the browser space to ensure content consistency across devices.
Responsive web design (RWD) is a web development approach that creates dynamic changes to the appearance of a website, depending on the screen size and orientation of the device being used to view it.
A fully responsive website will rescale itself to preserve the user experience and look and feel across all devices — with no irritating zooming, scrolling or resizing.
What is responsive design? Responsive web design is an approach that allows design across various devices (mobile, desktop, tablet, etc.) and suggests design should respond to the user's behavior based on screen size, platform and orientation. Flexible grids are foundational elements of responsive design.
Have you discovered the srcset
attribute? What srcset
does is it allows you to add more than one image in a <img>
tag and set certain conditions to it. Depending on which condition is met, the corresponding image will be shown. Time for an example
If you want an image to be viewed half of the users viewport width you would use
<img src="images/space-needle.jpg" sizes="50vw"
srcset="images/space-needle.jpg 200w, images/space-needle-2x.jpg 400w,
images/space-needle-hd.jpg 600w">
What this does is it finds how large the users viewport width is, say its 500px wide, so your image images/space-needle.jpg 200w
will load for the user.
In this example we specified that the image needs to take up half the screens width sizes="50vw"
. It would be impossible to show an image at 600px or 400px wide on a 500px wide screen and comfortably have it shown only at half the viewport so it chooses the 200w
option.
There are many ways to specify conditions for which image should load, device pixel ratio, screen size, browser size so on and so forth.
Eduational links:
https://www.sitepoint.com/how-to-build-responsive-images-with-srcset/
https://www.udacity.com/course/responsive-images--ud882
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With