responsive-and-fast-implementing-high-performance-responsive-design
responsive-and-fast-implementing-high-performance-responsive-design
responsive-and-fast-implementing-high-performance-responsive-design
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
CSS Responsive Images<br />
I previously described how browsers consistently download hidden<br />
images, but in fact, that description is only correct for foreground<br />
images placed in a page using an tag. Background images, on<br />
the other h<strong>and</strong>, go through a different process altogether.<br />
Background images are added to a page using the backgroundimage<br />
CSS property, which puts an image behind a DOM element.<br />
The DOM element does not have to contain other content, <strong>and</strong> so an<br />
empty element with a background image achieves a similar effect to<br />
an tag.<br />
Browsers process background images differently than they do <br />
tags. While building the DOM, which contains the different elements<br />
on a page, the browser applies CSS styles to the DOM elements to<br />
construct a structure called the render tree. The render tree holds the<br />
objects that will actually get painted on the screen, which means some<br />
DOM elements are not included in it (such as the page title, any script<br />
tag, or HTML comments). While building the render tree, when a<br />
node is determined to include a background image, the browser will<br />
download <strong>and</strong> render it.<br />
For content elements on the page, the render tree usually resembles<br />
the DOM, but the two are not exactly the same. For example, a hov<br />
er style on a DOM element may be represented as a child node in the<br />
render tree alone. On the flip side, when a node is marked as dis<br />
play:none, it is removed from the render tree (along with its children),<br />
<strong>and</strong> thus background images on its child nodes will not be seen or<br />
downloaded.<br />
Furthermore, since background images are set using a style, they can<br />
be set within a Media Query condition. Using Media Queries, we can<br />
set different background images for different displays, <strong>and</strong> address the<br />
“Download <strong>and</strong> Shrink” problem as well.<br />
Responsive Background Image Pitfalls<br />
At first glance, using CSS as a vehicle for Responsive Images looks<br />
promising. However, a closer inspection reveals many reasons not to<br />
do so.<br />
One such reason is that <strong>responsive</strong> background images are still prone<br />
to excessive downloads. For instance, the background image of a hid‐<br />
14 | Chapter 3: Image Over-Downloading