-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Responsive Media in HTML5
By :

Now that we've worked out how much space we have to work with, we can now work out what happens to elements when we hit the limits of our available space. This is particularly relevant if we want to display hi-res images for example. After all, we don't want to show a high quality image if it chokes the available bandwidth of our device!
Let's take a look at how we can use media queries to switch between lo-res and hi-res versions of a single image:
min-resolution.html
and save it to the root of the project folder.min-resolution.css
in the css
subfolder of our project folder. This is where the magic happens, that is, switching from the lo-res to hi-res versions of our image:#orchid { background-image: url('../img/mothorchid.png'); height: 24.31rem; width: 36.5rem; } @media (min-resolution: 120dpi) { #orchid { background-image: url('../img/[email protected]'); height: 24.31rem; width: 36.5rem; } }
mothorchid.png
and [email protected]
into the img
subfolder of our project folder.mothorchid.png
.Using Google Chrome?
We can achieve the same effect using Chrome's Developer Toolbar. Press Ctrl + Shift + I to display it and then click on the drawer icon. Now, switch to the Screen tab and change the Device pixel ratio setting from 1
to 2
to show the hi-res image. For more details, please visit https://developer.chrome.com/devtools/docs/device-mode.
At this point, we can use this trick to display any hi-res image we need; the key is to ensure we have two images, one of a standard resolution, while the other is of a higher quality. A small word of note though—if you spend any time researching different types of media queries, then you may come across something akin to these lines of code:
@media (-webkit-min-device-pixel-ratio: 1.25), (min-resolution: 120dpi){ /* Retina-specific stuff here */ }
While still perfectly usable, the initial –webkit-min-device-pixel-ratio
setting has been deprecated in favor of min-resolution; there is no need to use it unless you have to cater to really old browsers!
Now, we could easily use CSS queries in all of our projects, but there may still be occasions where standard queries might not work. A good example is for a navigation that behaves differently at different sizes. Fortunately, there is a solution for this—we can achieve a similar effect using the breakpoints.js
library. Let's delve in now and take a look.
Change the font size
Change margin width
Change background colour