

- #Rails 5 bootstrap 4 fluid image how to#
- #Rails 5 bootstrap 4 fluid image code#
- #Rails 5 bootstrap 4 fluid image download#
If the browser doesn’t support srcset, (something that’s only an issue for quite old browsers), it will fall back to showing the small image. (Describing viewport sizes very roughly).
#Rails 5 bootstrap 4 fluid image code#
With the above code the browser will load the small image at small viewport sizes, the medium image at medium viewport sizes, and the large at large viewport sizes.

It’s important to include your default image in the srcset along with its width, despite advice you may have heard to the contrary, or the browser will not include it in the list of options to choose from and so it will never load at any viewport width. Then add your default image and its larger alternative images inside the srcset attribute as a comma separated list, specifying the width of each, (after a space), with w: It’s a good idea to follow a mobile first approach here and load your smallest image via the src attribute. You’ll still use the src attribute when using srcset as this provides the default image the browser should use, and acts as a fallback if someone is using an old browser that doesn’t support srcset. It can then use that information to load the most appropriate image depending on the size of the viewport at the time. But with the srcset attribute we can tell the browser how wide each of our images is in advance. When an image is loaded with only a regular src attribute the browser doesn’t know how wide it is until after it’s loaded. That’s what the srcset attribute is for: a set of images rather than just one via the src attribute.

Instead of using just one image everywhere, it’s much better if we can have a set of images we load depending on the size of the viewport, loading large images for wide viewports and small images for narrow viewports. To start working up a better solution let’s move on to srcset. “According to the Jrun of HTTP Archive, the median site now weighs 1937kb.” – What Does My Site Cost? Tip: Take a look at What Does My Site Cost? by Tim Kadlec to get an idea of the real cost of mobile data for users all over the world.
#Rails 5 bootstrap 4 fluid image download#
For example, it’s quite possible that the original image is impractically large (in terms of file size) for some users to download on their mobile network. But it doesn’t allow us to specify different images for differing circumstances. It solves the problem in one respect, allowing us to display the same image under many different circumstances. This does the essential job of making sure the image isn’t sized dramatically incorrectly, but it still leaves us with just one image for all cases, regardless of how well (or poorly) that one image works:

This is our starting point, a regular old vanilla element with an alt attribute to provide a text based description.
#Rails 5 bootstrap 4 fluid image how to#
