+ All Categories
Home > Documents > Flexible Web Design Sample Ch9

Flexible Web Design Sample Ch9

Date post: 08-Apr-2018
Category:
Upload: areej-bahjat
View: 218 times
Download: 0 times
Share this document with a friend
24
9 Creating Flexible Images No matter how perfectly you build your liquid or elastic layout, it’s not going to work if you don’t make the content within it exible too. Text is easy—it wraps by default. Images are where it gets tricky. Luckily, as you saw in Chapter 2, there are lots of creative ways to make your images— content images as well as decorative graphic elements—exible to either the viewport or the text size. In this chapter, you’ll learn the CSS behind those exible image examples.
Transcript
Page 1: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 1/25

Creating FlexibleI

Page 2: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 2/25

CHAPTER 9: CREATING FLEXIBLE IMAGES286

Dynamically ChangingImages’ Screen AreaSince the area available for an image to display wi

changes on the fly, your images may need to as we

images canwork within flexible layouts—as long

 you have matching minimum widths in place—th

can dynamically change the screen area that an im

Foreground Images that Scale with

One way to dynamically alter the footprint of an i

ally scale You saw an example of an image that sc

NOTE: Each of the

completed example files

is available for down-

load from this book’s

companion web site at

www.flexiblewebbook.

com. Download the file

ch9_examples.zip to get

the complete set. I’ll let

you know which file

goes with which tech-

nique as we go along

Page 3: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 3/25

DYNAMICALLY CHA

If you want the image to scale with the text size instead of the widt

Page 4: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 4/25

CHAPTER 9: CREATING FLEXIBLE IMAGES288

To assure that the browser always scales the image

a maximum width on the image that matches its simg {

width: 20em;

  max-width: 500px;

}

Now the image will scale only until it grows to 500

will act as any other fixed-width image (Figure 9.

TIP: If the image must

stay above a certain size toremain “readable,” add a

pixel min-width value too.

NOTE: The page

showing this completed

technique is scale.html in

the ch9_examples.zip file.

FIGURE 9.3 The image

does not stay proportional

to the text size once it

reaches its maximum

width of 500 pixels

Page 5: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 5/25

DYNAMICALLY CHA

Hiding and Revealing Portions of Images

Another way to change the amount of screen area an image takes u

dynamically change how much of the image is shown at any given

The image itself doesn’t change in size—the amount of space in wh

allowed to show does, and the rest of the image just remains hidde

of that space. I call this “variable cropping,” and you saw an examp

Figure 9.2.

You can create a variable cropping effect with either background or f

images. Both look the same, but each is specially suited to different s

VARIABLE CROPPING WITH BACKGROUND IMAGES

Putting the image that you want to dynamically crop in the backgr

ideal hen the image is p rel decorati e This techniq e lets o

Page 6: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 6/25

CHAPTER 9: CREATING FLEXIBLE IMAGES290

div#background {

  width: 50%;height: 330px;

background: url(styx.jpg) no-repeat;

border: 2px solid #000;

}

The width is set to some flexible dimension—eith

done here, or an em value to make it elastic—so thwidth to show more or less of the image. The heig

of the image so that the entire height of the image

The div will now always be 50 percent as wide as t

ground image doesn’t change in size, but gets crop

TIP: To dynamically

change the height ofthe image as well as or 

instead of the width,

use a flexible value for 

the height property .

Page 7: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 7/25

DYNAMICALLY CHA

This is all the CSS necessary to get the basic variable cropping techn

working, but you can add a few other enhancements if you like. For

Page 8: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 8/25

CHAPTER 9: CREATING FLEXIBLE IMAGES292

As with the background-image version of the variabl

need some block element in the (X)HTML to hold th

again; this time it won’t be empty, but will instead c

<div id=”foreground”>

<img src=”styx.jpg” alt=”my cat Styx”

height=”330”>

</div>

 Just as before, the div needs to have a flexible wid

pixel height of the image:

div#foreground {

width: 50%;

height: 330px;

NOTE: The img ele-

ment has an alt attri-bute providing the text

equivalent of the image.

You can’t do this with a

CSS background image.

Page 9: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 9/25

DYNAMICALLY CHA

Now whatever portion of the image would overflow out of the div i

from view (Figure 9.7).

FIGUR

 With ov

set to hi

extra po

image is

den from

Page 10: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 10/25

CHAPTER 9: CREATING FLEXIBLE IMAGES294

than the variable width image techniques we’ve g

web-site example of this composite image techniq

and 2.24 used two images to create the effect; you

number of images, but we’ll keep it simple and us

invasion example as well.

One image is going to be at least partially overlapp

the topmost image needs to have a transparent ba

choose to make the lower image transparent too,

page background to show through, for instance.) Y

with index transparency or a PNG with alpha tran

versatile, since they can lay over any other color o

skinny colored edge that shows around GIF image

something that’s a different color than they were

l h i bl d f t i t

Page 11: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 11/25

DYNAMICALLY CHA

#outer {

background: url(skyline.jpg) no-repeat;

}

#inner {

background: url(ufo.png) no-repeat;

}

Since the divs are empty, they also need dimensions added to them

them from collapsing entirely, as well as to create the flexible beha

we want:

#outer {

  width: 100%;

max-width: 1000px;

Page 12: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 12/25

CHAPTER 9: CREATING FLEXIBLE IMAGES296

There are a couple ways to fix this: use the backgr

change where the flying saucer displays within th

div. Either option is fine, but the latter seems a lit

and implement—at least to me—so that’s what w

 We’ll move the div using absolute positioning; flo

First, add position: relative; to the #outer rule

the containing element for the absolutely position

position: absolute; as well as top and right val

#inner {  position: absolute;

top: 50px;

right: 50px;

Page 13: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 13/25

CREATING FLEX

two of the most common types of image collections—teaser thumb

and image galleries—flexible too.

Teaser Thumbnail Lists

A teaser thumbnail list is my own personal name for the design con

of a list where each item is made up of a title, short description, an

nail image. Figure 2.22 is one example of a teaser thumbnail list, as

of featured pets on the home page of our fictional Beechwood Anim

site (Figure 2.43). These types of lists can be built in many different

but many techniques result in lists that are not flexible or not as ac

to the end user as they could be.

FIGURE 9.12 Ea

Page 14: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 14/25

CHAPTER 9: CREATING FLEXIBLE IMAGES298

<p>Soy-glazed salmon, served with c

broccoli slaw.</p>

</li>

<li>

<h2>March</h2>

<img src=”march.jpg” alt=”” width=”

<p>Tuna steak with ginger-shitake c

sesame broccoli and brown rice.</p>

</li>

</ul>

You’ll note that the img elements follow the h2 head

Figure 9.12 shows the images appearing on the sam

Page 15: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 15/25

CREATING FLEX

margin: 0 0 20px 0;

padding: 0;

}

This removes the default left margin and padding that some brows

li elements, as well as adds 20 pixels of space below each list item

them out from each other.

EASIER TEASER THUMBNAIL LIST CREATION OPTIONS

If you’re still feeling a little uneasy about creating negative margin layo

there are a few easier ways to create teaser thumbnail lists that achieve

same visual effect:

Remove the images from the (X)HTML altogether and simply use C

Page 16: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 16/25

CHAPTER 9: CREATING FLEXIBLE IMAGES300

FIGURE 9.13 A large

empty space on the left side

of the list stands ready toreceive the thumbnails.

Page 17: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 17/25

CREATING FLEX

width: 100%;

margin: 0;

}

FIGURE 9.14 Negative

margins pull the images t

left, but don’t pull them

to sit beside the headings

Page 18: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 18/25

CHAPTER 9: CREATING FLEXIBLE IMAGES302

To address this, we need to use a float containmen

item to encompass all of the floated elements with

ments themselves is one easy way to do this:

li {

  float: left;

width: 100%;

margin: 0 0 20px 0;

padding: 0;

}

The list items are now properly spaced out from e

within them is shorter or longer than the thumbn

The only problem is that floating the list items made

NOTE: The page

showing this completed

technique is teaser.html in

the ch9_examples.zip file.

Page 19: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 19/25

CREATING FLEX

WRAPPING THE THUMBNAILS

The two behaviors you want thumbnails in a flexible image gallery

achieve—sitting side by side and wrapping onto more lines as need

both native behaviors of floats. So, the only thing you need to do to

thumbnails wrap is to float each one in the same direction.

You could just place the images straight into the (X)HTML with no

and float each of the img elements. But a more semantic way to ma

group of images is to use an unordered list, which offers you more

possibilities as well. Put each img into an li element:<ul>

<li><img src=”january.jpg” alt=”January” width=”100” height

</li>

<li><img src ”february jpg” alt ”February” width ”100” heig

Page 20: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 20/25

CHAPTER 9: CREATING FLEXIBLE IMAGES304

Now, simply float the li elements all to the left, a

on their right and bottom sides to space them out

li {

  float: left;

margin: 0 10px 10px 0;

padding: 0;

}

That’s all you need to do to create a basic, wrappin

lery (Figure 9.17). The perfect number of thumbn

line, no matter the viewport width, so you don’t g

a really large gap on the right. If you didn’t want t

entire width of its parent, simply assign a width to

NOTE: The pageshowing this completed

technique is gallery_

wrap.html in the

ch9_examples.zip file.

Page 21: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 21/25

CREATING FLEX

FIGURE 9.18 The extra he

second thumbnail blocks the

nail from moving all the wayleaving a gap in the second ro

same problem happens in th

Page 22: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 22/25

CHAPTER 9: CREATING FLEXIBLE IMAGES306

In browsers that support inline-block, that’s all y

thumbnails from hanging up on each other when

 you want the thumbnails aligned along their top e

we used floats, simply add vertical-align: top;

FIGURE 9.19  When the

thumbnails are turned

into inline blocks, instead

of floats, they no longer

hang up on one another.

Page 23: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 23/25

CREATING FLEX

display property that worked almost identically. Add either of the

to the li rule:

li {

  display: -moz-inline-box;

display: inline-block;

margin: 0 10px 10px 0;

padding: 0;

vertical-align: top;

}

This fixes the problem in Firefox 2 without hurting any other brow

including Firefox 3—they all just ignore the -moz-inline-box value

have links wrapped around the images, however, you’ll have just a

Page 24: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 24/25

CHAPTER 9: CREATING FLEXIBLE IMAGES308

Next, add a percentage or em width onto the li el

li {

float: left;

  width: 18%;

margin: 0 10px 10px 0;

padding: 0;

}

Finally, add a rule for the img elements that sets thso they always fill up the variable size of their pare

img {

width: 100%;

NOTE: The pageshowing this completed

technique is gallery_

scale.html in the

ch9_examples.zip file.

Page 25: Flexible Web Design Sample Ch9

8/6/2019 Flexible Web Design Sample Ch9

http://slidepdf.com/reader/full/flexible-web-design-sample-ch9 25/25

SITE-BUILDING EXERCISE: ADDING FLEXIBL


Recommended