XML technologies / CSS / CSS Cookbook / Images / Panoramic images

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

An image’s width should adapt dynamically to the browser window size, as shown in the following figure.

Enlarging the browser window reveals more of the panoramic image

Figure: Enlarging the browser window reveals more of the panoramic image.

Solution

Place the panoramic image in the background of a block element (see the next figure).

<h1>Visit France City!</h1>
<div><img src="frenchtown.jpg" alt=" " /></div>
<h2>The quaint and charming little destination in France</h2>

Panoramic photograph on a web page without CSS

Figure: The panoramic photograph on a web page without CSS.

Position the background image in the block element’s upper-right corner, then hide the actual image element by setting the property display to none:

div {
 background-image: url(frenchtown.jpg);
 background-repeat: no-repeat;
 background-position: top right;
 height: 300px;
 border: 1px solid black;
 max-width: 714px;
}
div img {
 display: none;
}

When the image is used as a block element’s background, its size adapts according to the browser window size.

Discussion

To present a panoramic image, you first need a wide photograph. Then position it in the upper-right corner so that it can adapt to the browser window size. Using the property max-width prevents the element div from becoming wider than the image itself.

This solution uses the same image in both the HTML and CSS code. This ensures that the image is also displayed when the user agent does not support CSS.

See also

The CSS 2.1 specification for the property max-width.

  

 

 

 

back to top