<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zomigi.com &#187; Tutorials</title>
	<atom:link href="http://zomigi.com/categories/tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://zomigi.com</link>
	<description>The professional blog of Zoe Mickley Gillenwater</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:36:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSS effect: space images out to match text height</title>
		<link>http://zomigi.com/blog/css-effect-space-images-out-to-match-text-height/</link>
		<comments>http://zomigi.com/blog/css-effect-space-images-out-to-match-text-height/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 17:15:48 +0000</pubDate>
		<dc:creator>Zoe Gillenwater</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[flexible]]></category>
		<category><![CDATA[images]]></category>

		<guid isPermaLink="false">http://zomigi.com/?p=158</guid>
		<description><![CDATA[Make several fixed-width images change their spacing to stay lined up with accompanying text, no matter the window size or text size.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been on my soapbox a lot lately, so I decided it was time to step down for a bit and do a nice, non-controversial CSS tutorial. Wouldn&#8217;t you agree?</p>
<p>The inspiration for this tutorial came from the <a href="http://clearleft.com/is/">&#8220;Who we are&#8221; page on the Clearleft site</a>. Try resizing the text—<strong>only</strong> the text, not the whole page—and notice how the photos move farther apart and closer together. They always stay aligned with the names they go with, and the overall height of the photo block never gets too far off the height of the block of text to the left. So, the design always appears balanced.</p>
<div id="attachment_178" class="wp-caption alignnone" style="width: 510px"><a href="http://zomigi.com/wp-content/uploads/2009/06/clearleft1.png"><img class="size-full wp-image-178" title="clearleft1" src="http://zomigi.com/wp-content/uploads/2009/06/clearleft1.png" alt="The images remain the same size but change their spacing as the text size changes." width="500" height="266" /></a><p class="wp-caption-text">The images remain the same size but change their spacing as the text size changes.</p></div>
<p>I love this effect; it&#8217;s a very clever way to fill up the entire available space with images without having to actually scale the images. It&#8217;s basically another take on what I call <a href="http://zomigi.com/blog/creating-sliding-composite-images/">sliding composite images</a>. There are an unlimited number of ways to create sliding composite images—background positioning, absolute positioning, margins, etc—but I wanted to share one way to accomplish the particular effect shown on the Clearleft site. Luckily, it&#8217;s really quite simple.<br />
<span id="more-158"></span><br />
<a href="http://www.zomigi.com/demo/composite_flowers.html">Here&#8217;s my version of the effect.</a> Try resizing your browser window and text size to see the flowers move farther apart and closer together.</p>
<p>And here&#8217;s how I did it&#8230;</p>
<h3>The markup and images</h3>
<p>I start with a definition list for the markup of the list of flowers beside the images. (You could also use an unordered list combined with headings, paragraphs, and/or spans. Use whatever makes the most sense, semantically, for your content.) The definition list is contained in its own <code>div</code>, which follows another <code>div</code> containing a few paragraphs of text. I&#8217;ve got them both in a wrapper <code>div</code> to prevent float drop.</p>
<pre><code>&lt;div id="wrapper"&gt;
	&lt;div id="description"&gt;
		&lt;h1&gt;The amazing five-flowered vine&lt;/h1&gt;
		...
	&lt;/div&gt;
	&lt;div id="flowers"&gt;
		&lt;dl id="flowers-list"&gt;
			&lt;dt&gt;&lt;img src="images/flower1.png" ... id="flower1"&gt;Raspberry Lily&lt;/dt&gt;
			&lt;dd&gt;Rare and exotic&lt;/dd&gt;
			&lt;dt&gt;&lt;img src="images/flower2.png" ... id="flower2"&gt;Seaside Daisy&lt;/dt&gt;
			&lt;dd&gt;A native of the North Carolina Atlantic coast&lt;/dd&gt;
			&lt;dt&gt;&lt;img src="images/flower3.png" ... id="flower3"&gt;Orange Starflower&lt;/dt&gt;
			&lt;dd&gt;Dimunitive but intensely colored and scented&lt;/dd&gt;
			&lt;dt&gt;&lt;img src="images/flower4.png" ... id="flower4"&gt;Lagoon Pansy&lt;/dt&gt;
			&lt;dd&gt;The largest of all native pansy breeds&lt;/dd&gt;
			&lt;dt&gt;&lt;img src="images/flower5.png" ... id="flower5"&gt;Sunbeam Peony&lt;/dt&gt;
			&lt;dd&gt;Pleasant to behold but poisonous to ingest&lt;/dd&gt;
		&lt;/dl&gt;
	&lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>I&#8217;ve used <code>img</code> elements in the HTML for two reasons: that&#8217;s how they did it on the Clearleft site, which I&#8217;m trying to emulate, and I think my images are content, not decoration, in this case. You could accomplish the same effect using background images, of course, if that made better sense for your particular content.</p>
<h3>The CSS</h3>
<p>To get the block of descriptive text sitting to the left of the list of flowers, I floated both <code>divs</code>.</p>
<pre><code>#description {
	float: left;
	width: 44%;
	margin-right: 2%;
	}
#flowers {
	float: left;
	width: 53%;
	}
</code></pre>
<p>I wanted to create the appearance that my five flowers were all attached to a single stem, so as they changed in distance from each other they would still appear connected. I created an image of a stem with leaves and tiled this up and down the flowers <code>div</code>, 59 pixels away from the left side. I also added a border at the bottom of the flowers <code>div</code> in the same color as the stem to act as grass.</p>
<pre><code>#flowers {
	float: left;
	width: 53%;
	<em>border-bottom: 10px solid #6EAB23;</em>
	<em>background: url(images/stem.png) repeat-y 59px 0;</em>
	}
</code></pre>
<p class="note">If I wanted to get fancier, I could have created a background image of grass, applied this to the flowers <code>div</code>, and applied the repeating stem background image to the definition list inside the <code>div</code> instead. But this was just a demo!</p>
<p>I applied a hefty amount of left padding to the flowers definition list to keep its text from overlapping the stem background image, as well as create room for the where I would eventually move the individual flower images. I also zeroed out the default margin on the <code>dl</code>, <code>dt</code>, and <code>dd</code> elements, and added some bottom margin to the <code>dd</code> elements to space the text out and again make room for the flowers.</p>
<pre><code>#flowers-list {
	margin: 0;
	padding-left: 220px;
	}
#flowers dt, #flowers dd {
	margin: 0;
	}
#flowers dd {
	margin-bottom: 1em;
	}
</code></pre>
<p>Now it was finally time to move the flowers into place using absolute positioning. I set <code>position: relative</code> on the flowers <code>div</code> so it would act as the point of reference for the individual flowers.</p>
<pre><code>#flowers {
	<em>position: relative;</em>
	float: left;
	width: 53%;
	border-bottom: 10px solid #6EAB23;
	background: url(images/stem.png) repeat-y 59px 0;
	}
<em>#flowers img {
	position: absolute;
	}</em>
</code></pre>
<p>Now I just had to direct each flower to how far away from the left side of the <code>div</code> I wanted it to appear. This varied for each flower based on whether I wanted it to appear on the left or ride side of the stem, as well as on the width of the flower image. Here are the values I used.</p>
<pre><code>#flower1, #flower3, #flower4 {
	left: 101px;
	}
#flower2 {
	left: 0;
	}
#flower5 {
	left: 31px;
	}
</code></pre>
<p>Because I didn&#8217;t change any of the top or bottom values for the flower images, just their left values, each stays aligned with its accompanying name and description along its top edge, no matter how large or small the text size or window size is.</p>
<div id="attachment_179" class="wp-caption alignnone" style="width: 510px"><a href="http://zomigi.com/wp-content/uploads/2009/06/composite_flowers1.png"><img class="size-full wp-image-179" title="composite_flowers1" src="http://zomigi.com/wp-content/uploads/2009/06/composite_flowers1.png" alt="The flowers vary in their spacing along the height of the stem in order to stay with the text." width="500" height="208" /></a><p class="wp-caption-text">The flowers vary in their spacing along the height of the stem in order to stay aligned with the text.</p></div>
<p><a href="http://www.zomigi.com/demo/composite_flowers.html">Check out the finished demo again</a> and play around with it. The block of text on the left always stays roughly the same height as the list of flowers on the right, so this is a great example of how you can make your web page flexible and still keep the design balanced at different font sizes and window sizes. Here, I&#8217;ve used a liquid width on the overall page, as on the Clearleft site, simply because that makes the difference in the spacing of the images more dramatic and easy to see for demo purposes. But you could just as easily use this effect on a fixed-width or elastic wrapper in order to keep images aligned with variable text sizes. You could also use this effect horizontally instead of vertically to fill up the entire width of a <code>div</code> or window with fixed-width images, giving you more leeway to make the overall width flexible.</p>
<p>There&#8217;s really no fancy or complicated CSS in use here, but it produces a fun effect that makes your design a lot more adaptable to the myriad of user configurations on the web.</p>
<h3>A note about IE 6</h3>
<p>I know that there are some problems with my demo page in IE 6—lack of support for alpha-transparent PNGs and float drop at small window sizes due to auto-expansion of <code>divs</code> to fit large content—but they&#8217;re not related to the actual effect that this tutorial sets out to teach, so I&#8217;ve chosen not to include their fixes in this tutorial. In your own pages using this effect, you may never run into these problems—or you may run into more. But the CSS for the effect itself doesn&#8217;t require any tweaking for IE 6.</p>
]]></content:encoded>
			<wfw:commentRss>http://zomigi.com/blog/css-effect-space-images-out-to-match-text-height/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating sliding composite images</title>
		<link>http://zomigi.com/blog/creating-sliding-composite-images/</link>
		<comments>http://zomigi.com/blog/creating-sliding-composite-images/#comments</comments>
		<pubDate>Mon, 27 Apr 2009 18:00:45 +0000</pubDate>
		<dc:creator>Zoe Gillenwater</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[flexible]]></category>
		<category><![CDATA[Flexible Web Design]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[liquid]]></category>

		<guid isPermaLink="false">http://zomigi.com/?p=101</guid>
		<description><![CDATA[Create the appearance of a single image by layering multiple images on top of each other. The pieces can slide around the page as its width is changed, making it a perfect technique for liquid layouts.]]></description>
			<content:encoded><![CDATA[<p class="note">This tutorial is slightly adapted from Chapter 9 of my book, <a href="http://www.amazon.com/gp/product/0321553845?ie=UTF8&amp;tag=zoeshomepage&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321553845"><em>Flexible Web Design: Creating Liquid and Elastic Layouts with CSS</em></a>. You can download most of <a href="http://www.flexiblewebbook.com/bonus.html">Chapter 9</a> for free as a PDF at the <a href="http://www.flexiblewebbook.com">Flexible Web Design companion site</a>, as well as download the <a href="http://www.flexiblewebbook.com/files.html">HTML, CSS and image files</a> that go along with this tutorial.</p>
<p>Since the area available for an image to display within a flexible layout changes on the fly, your images may need to as well. While fixed-width images can work within flexible layouts—as long as they’re not too large, or you have matching minimum widths in place—there are lots of ways you can dynamically change the screen area that an image takes up.</p>
<p>I&#8217;ve already written about <a href="http://zomigi.com/blog/foreground-images-that-scale-with-the-layout/">how to make images literally scale</a> and <a href="http://zomigi.com/blog/hiding-and-revealing-portions-of-images/">how to use variable cropping on images</a>. But perhaps you don’t want either end of your image dynamically cropped off—there may be important content on each side that you want to always keep in view. And perhaps you don&#8217;t want to scale the entire image, because although this would keep the entire width of the image always visible, it would also change the vertical space the image takes up, which might not work for your design. This is the perfect time to try using what I call a composite image.<br />
<span id="more-101"></span><br />
Creating what appears to be a single image out of multiple pieces that slide over and away from each other takes a little more work on the graphics side than the variable width image techniques we’ve gone over so far. You can use an unlimited number of images, but we’ll keep it simple and use two for our own alieninvasion example as well.</p>
<p>One image is going to be at least partially overlapping the other, so at least the topmost image needs to have a transparent background. (You may choose to make the lower image transparent too, to allow parts of the main page background to show through, for instance.) You can use either a GIF with index transparency or a PNG with alpha transparency. PNGs are more versatile, since they can lay over any other color or pattern without the skinny colored edge that shows around GIF images when they’re placed over something that’s a different color than they were optimized for. PNGs can also have variable degrees of transparency, instead of each pixel being either 100 percent transparent or 100 percent opaque.</p>
<p>We’ll use an alpha-transparent PNG of a flying saucer for our top image in this example. The flying saucer will be laid on top of a photo of the Chicago skyline, saved as an ordinary JPG.</p>
<p>Once you have your images made, you need two block elements to place each on as a background image. One block element needs to be nested inside the other. In a real page, you’d want to make use of block elements that were already in place as much as possible, such as existing wrapper and header divs. For this simple example, we’ll use two empty divs:</p>
<pre><code>&lt;div id="outer"&gt;&lt;div id="inner"&gt;&lt;/div&gt;&lt;/div&gt;</code></pre>
<p>Next, you need to create rules placing each image as a non-repeating background on each div, with the image you want on the bottom used for the background of the outer div:</p>
<pre><code>#outer {
	background: url(skyline.jpg) no-repeat;
	}
#inner {
	background: url(ufo.png) no-repeat;
}</code></pre>
<p>Since the divs are empty, they also need dimensions added to them to stop them from collapsing entirely, as well as to create the flexible behavior that we want:</p>
<pre><code>#outer {
	<em>width: 100%;</em>
	<em>max-width: 1000px; </em>
	<em>height: 300px;</em>
	background: url(skyline.jpg) no-repeat;
	}
#inner {
	<em>width: 100px;</em>
	<em>height: 250px; </em>
	background: url(ufo.png) no-repeat;
	}</code></pre>
<p>Both of the images now show on the page, with the <a href="http://www.zomigi.com/demo/composite_static.html">flying saucer layered over the skyline</a>. However, the flying saucer never moves when the window changes in size—it’s always pinned to the top left corner of the skyline photo. That’s because the div for which it’s a background begins in that corner, and non-repeating, non-positioned background images display in the top left corner by default.</p>
<p class="sidenote">Note: Remember that IE 6 and earlier do not support alpha-transparent PNGs, so the flying saucer image will have a solid gray background in those browsers. Use the alphaimageloader hack described in the exercise section of Chapter 7 to fix this.</p>
<p>There are a couple ways to fix this: use the background-position property to change where the flying saucer displays within the div, or move the entire div. Either option is fine, but the latter seems a little easier to understand and implement—at least to me—so that’s what we’ll use here. We’ll move the div using absolute positioning; floating would work as well. First, add position: relative; to the #outer rule to make that div act as the containing element for the absolutely positioned inner div. Then, add position: absolute; as well as top and right values to the #inner rule:</p>
<pre><code>#inner {
	<em>position: absolute;</em>
	<em>top: 50px; </em>
	<em>right: 50px;</em>
	width: 100px;
	height: 250px;
	background: url(ufo.png) no-repeat;
	}</code></pre>
<p>Now the flying saucer image will always be 50 pixels away from both the top and right edges of the skyline photo. Because the outer div has a flexible width, its right edge moves as the window is resized, which in turn makes the <a href="http://www.zomigi.com/demo/composite.html">flying saucer image move</a> as well.</p>
<p class="note">Update, June 2009: I&#8217;ve written <a href="http://zomigi.com/blog/css-effect-space-images-out-to-match-text-height/">another tutorial on creating composite images</a> that takes a different approach. Thought you might want to check it out too!</p>
]]></content:encoded>
			<wfw:commentRss>http://zomigi.com/blog/creating-sliding-composite-images/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hiding and revealing portions of images</title>
		<link>http://zomigi.com/blog/hiding-and-revealing-portions-of-images/</link>
		<comments>http://zomigi.com/blog/hiding-and-revealing-portions-of-images/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 22:25:32 +0000</pubDate>
		<dc:creator>Zoe Gillenwater</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[elastic]]></category>
		<category><![CDATA[flexible]]></category>
		<category><![CDATA[Flexible Web Design]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[liquid]]></category>
		<category><![CDATA[markup]]></category>

		<guid isPermaLink="false">http://zomigi.com/?p=90</guid>
		<description><![CDATA[It's possible to dynamically "crop" both background and foreground images as the layout changes in width, solving the problem of overflowing fixed-width images in a flexible page.]]></description>
			<content:encoded><![CDATA[<p class="note">This tutorial is slightly adapted from Chapter 9 of my book, <a href="http://www.amazon.com/gp/product/0321553845?ie=UTF8&amp;tag=zoeshomepage&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321553845"><em>Flexible Web Design: Creating Liquid and Elastic Layouts with CSS</em></a>. You can download most of <a href="http://www.flexiblewebbook.com/bonus.html">Chapter 9</a> for free as a PDF at the <a href="http://www.flexiblewebbook.com">Flexible Web Design companion site</a>, as well as download the <a href="http://www.flexiblewebbook.com/files.html">HTML, CSS and image files</a> that go along with this tutorial.</p>
<p>Since the area available for an image to display within a flexible layout changes on the fly, your images may need to as well. While fixed-width images can work within flexible layouts—as long as they’re not too large, or you have matching minimum widths in place—there are lots of ways you can dynamically change the screen area that an image takes up.</p>
<p>I&#8217;ve already written about <a href="http://zomigi.com/blog/foreground-images-that-scale-with-the-layout/">how to make images literally scale</a>, but another way to change the amount of screen area an image takes up is to dynamically change how much of the image is shown at any given time. The image itself doesn’t change in size—the amount of space in which it’s allowed to show does, and the rest of the image just remains hidden outside of that space. I call this “variable cropping.”</p>
<p>You can create a variable cropping effect with either background or foreground images. Both look the same, but each is specially suited to different situations.<br />
<span id="more-90"></span></p>
<h3>Variable cropping with background images</h3>
<p>Putting the image that you want to dynamically crop in the background is ideal when the image is purely decorative. This technique lets you keep the image in the CSS with the other decorative images, so if you later change the look of the site, all the decorative images can be changed in a single style sheet instead of having to replace multiple <code>img</code> elements across multiple pages of the site. By keeping the decorative image as a CSS background, you’re also making it likely that the image won’t print when the user prints the page—background printing is turned off by default in all major browsers—so the user can save ink by printing only content.</p>
<p>To use a CSS background image, you’ll fi rst need an element on which to place the background. This example will use a <code>div</code>:</p>
<pre><code>&lt;div id="background"&gt;&lt;/div&gt;</code></pre>
<p>The <code>div</code> is completely empty; it contains no content, but exists simply to hold a background image. If you have a more semantic element you can hang the background on instead, use it. For instance, perhaps the image you want to dynamically crop sits above an <code>h3</code> element and matches it in width. You could add the image as a background to the <code>h3</code> element and give the <code>h3</code> enough top padding to make sure its text sits below the image, not on top of it.</p>
<p>Next, create a rule for this <code>div</code> that sets the image as its non-tiling background:</p>
<pre><code>div#background {
	background: url(styx.jpg) no-repeat;
	border: 2px solid #000;
	}</code></pre>
<p>I’ve added a border on to this <code>div</code> as well so you can easily see where its edges lie. Right now, with no content within the <code>div</code>, it will collapse to zero height. Add dimensions to the <code>div</code> to prop it open:</p>
<pre><code>div#background {
	<em>width: 50%; </em>
	<em>height: 330px;</em>
	background: url(styx.jpg) no-repeat;
	border: 2px solid #000;
	}</code></pre>
<p class="tip">Tip: To dynamically change the height of the image as well as or instead of the width, use a flexible value for the <code>height</code> property.</p>
<p>The width is set to some flexible dimension—either a percentage, as I’ve done here, or an em value to make it elastic—so that the <code>div</code> can change in width to show more or less of the image. The height is set to the pixel height of the image so that the entire height of the image will show at all times.</p>
<p>The <code>div</code> will now always be 50 percent as wide as the viewport; its background image doesn’t change in size, but gets <a href="http://www.zomigi.com/demo/crop_background_right.html">cropped to a varying degree from the right side</a>.</p>
<p>However, this particular image would look better cropped from the left side, as the cat’s face is on the right side of the photo. To specify from where the image gets cropped, use the <code>background-position</code> property, or its shorthand in the <code>background</code> property, to change the alignment of the image within the <code>div</code>:</p>
<pre><code>div#background {
	width: 50%;
	height: 330px;
	background: url(styx.jpg) no-repeat <em>right</em>;
	border: 2px solid #000;
	}</code></pre>
<p>The image is now anchored to the right side of the <code>div</code>, so <a href="http://www.zomigi.com/demo/crop_background.html">more or less of its left side shows as the <code>div</code> changes in size</a>.</p>
<p>This is all the CSS necessary to get the basic variable cropping technique working, but you can add a few other enhancements if you like. For instance, right now, once the <code>div</code> exceeds the width of the image, empty white space shows within the <code>div</code>. There are a few ways you could handle this. You could add a background color to the <code>div</code> as well that would fill up whatever space the image cannot; if you blend the edge of the image into this background color, the effect can look seamless. Or, you could add a maximum width to the <code>div</code> so it can never grow larger than the image. You could also add minimum widths, as well as maximum and minimum heights, to ensure that the <code>div</code> can never grow or shrink past particular<br />
points in the image.</p>
<h3>Variable cropping with foreground images</h3>
<p>If the image that you want to dynamically crop is functional content, you’ll want to keep it as a foreground image by placing it in the (X)HTML using the <code>img</code> element. You can ask yourself these questions to determine if the image is content, not decoration:</p>
<ul>
<li>Does the image convey information that I ought to put as text in an <code>alt</code> attribute?</li>
<li>Do I want to make sure the image always prints because without it the<br />
printout wouldn’t make sense or be complete?</li>
<li>Do I want to link the image?</li>
</ul>
<p>If the answer to any of these questions is yes, the image is content and should be kept in the (X)HTML. CSS background images can’t achieve any of these goals—at least not without some complicated workarounds and hacks, all of which are quite silly, considering how easily a simple <code>img</code> element can achieve all this.</p>
<p>As with the background-image version of the variable cropping technique, you’ll need some block element in the (X)HTML to hold the image. We’ll use a <code>div</code> again; this time it won’t be empty, but will instead contain the img element:</p>
<pre><code>&lt;div id="foreground"&gt;
	&lt;img src="styx.jpg" alt="my cat Styx" width="500" height="330"&gt;
&lt;/div&gt;</code></pre>
<p class="sidenote">Note: The <code>img</code> element has an <code>alt</code> attribute providing the text equivalent of the image. You can’t do this with a CSS background image.</p>
<p>Just as before, the <code>div</code> needs to have a flexible width and a height set to the pixel height of the image:</p>
<pre><code>div#foreground {
	width: 50%;
	height: 330px;
	border: 2px solid #000;
	}</code></pre>
<p>So far, all we have is a regular <code>div</code> holding a regular image—there’s nothing yet that makes this a variable cropping technique. If the image is bigger than the <code>div</code>, it doesn’t get cropped, but simply <a href="http://www.zomigi.com/demo/crop_foreground_overflow.html">overflows</a>.</p>
<p>To get the cropping effect, add <code>overflow: hidden;</code> to the CSS rule:</p>
<pre><code>div#foreground {
	<em>overflow: hidden; </em>
	width: 50%;
	height: 330px;
	border: 2px solid #000;
	}</code></pre>
<p>Now whatever portion of the image would overflow out of the <code>div</code> is <a href="http://www.zomigi.com/demo/crop_foreground_right.html">hidden from view</a>.</p>
<p>Once again, though, it would be better for this image to be cropped from the left side, not the right. We can’t use the <code>background-position</code> property this time because it’s not a background image. To change how a foreground image is anchored within its parent, you can float the image:</p>
<pre><code>div#foreground img {
	float: right;
	}</code></pre>
<p>This anchors the image to the right side of the <code>div</code>, so <a href="http://www.zomigi.com/demo/crop_foreground.html">more or less of its left side shows as the <code>div</code> changes in size</a>. Using a foreground image results in an effect that <a href="http://www.zomigi.com/demo/crop.html">looks exactly like using a background image</a>, but the foreground image has alternative text, and you could also easily add a link to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://zomigi.com/blog/hiding-and-revealing-portions-of-images/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Foreground images that scale with the layout</title>
		<link>http://zomigi.com/blog/foreground-images-that-scale-with-the-layout/</link>
		<comments>http://zomigi.com/blog/foreground-images-that-scale-with-the-layout/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 21:44:34 +0000</pubDate>
		<dc:creator>Zoe Gillenwater</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[elastic]]></category>
		<category><![CDATA[flexible]]></category>
		<category><![CDATA[Flexible Web Design]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[liquid]]></category>

		<guid isPermaLink="false">http://zomigi.com/?p=58</guid>
		<description><![CDATA[Simple CSS techniques can make the content images inside your liquid or elastic pages scale with the layout.]]></description>
			<content:encoded><![CDATA[<p class="note">This tutorial is slightly adapted from Chapter 9 of my book, <a href="http://www.amazon.com/gp/product/0321553845?ie=UTF8&amp;tag=zoeshomepage&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0321553845"><em>Flexible Web Design: Creating Liquid and Elastic Layouts with CSS</em></a>. You can download most of <a href="http://www.flexiblewebbook.com/bonus.html">Chapter 9</a> for free as a PDF at the <a href="http://www.flexiblewebbook.com">Flexible Web Design companion site</a>, as well as download the <a href="http://www.flexiblewebbook.com/files.html">HTML, CSS and image files</a> that go along with this tutorial.</p>
<p>Since the area available for an image to display within a flexible layout changes on the fly, your images may need to as well. While fixed-width images can work within flexible layouts—as long as they’re not too large, or you have matching minimum widths in place—there are lots of ways you can dynamically change the screen area that an image takes up.</p>
<p>One way to dynamically alter the footprint of an image is to make it literally scale, based on either the text size or the changing dimensions of its parent element. Both are elegant effects that are deceptively simple to create.<br />
<span id="more-58"></span><br />
Both liquid and elastic scaling images start out with a regular <code>img</code> element in the (X)HTML:</p>
<pre><code>&lt;img src="styx.jpg" alt="my cat Styx"&gt;</code></pre>
<p>Notice that this <code>img</code> element has no <code>width</code> or <code>height</code> attributes, as it normally would. You control the dimensions with CSS instead.</p>
<p>For a liquid image, create a CSS rule to set the image’s width to a percentage value:</p>
<pre><code>img {
  	width: 50%;
  	}</code></pre>
<p class="sidenote">Note: This rule will make all images 50 percent as wide as their parents. In a real page, you would probably add an <code>id</code> or <code>class</code> to the specific image you wanted to scale and use that <code>id</code> or <code>class</code> as the selector in the CSS.</p>
<p>No <code>height</code> value is necessary; the browser determines the height that will proportionately constrain the image’s dimensions. If you were more concerned about making the height of your image stay proportional to its parent’s height than you were with width, you could use the <code>height</code> property in the CSS and leave off the <code>width</code> property. Just make sure not to use the two together.</p>
<p>As with all percentage dimensions, the percentage <code>width</code> value you choose is relative to the width of the parent element. As you change the width of the parent element, the <a href="http://www.zomigi.com/demo/scale_liquid.html">image scales to match</a>.</p>
<p>If you want the image to <a href="http://www.zomigi.com/demo/scale_elastic.html">scale with the text size</a> instead of the width of the parent element, simply change the <code>width</code> value in the CSS to an em value:</p>
<pre><code>img {
  	width: 20em;
  	}</code></pre>
<p>Any time a browser scales an image, there’s going to be some distortion, but you can keep it minimal by starting out with a very large image so the browser will usually be scaling it down.</p>
<p>To assure that the browser <em>always</em> scales the image down, not up, you can set a maximum width on the image that matches its set pixel width:</p>
<pre><code>img {
  	width: 20em;
  	<em>max-width: 500px;</em>
  	}</code></pre>
<p class="tip">Tip: If the image must stay above a certain size to remain “readable,” add a pixel <code>min-width</code> value too.</p>
<p>Now the image will scale <a href="http://www.zomigi.com/demo/scale_elastic_max.html">only until it grows to 500 pixels wide</a>; thereafter it will act as any other fixed-width image.</p>
<h3>Simulate image scaling with JavaScript</h3>
<p>If you don’t want the browser to scale your images at all, yet you want them to change in size based on the amount of space available, you can use JavaScript to swap in differently sized versions of the same image. The JavaScript detects the user’s viewport size and chooses the appropriate version of the image to show. This works in the same way that <a href="http://www.clagnut.com/blog/1663/">resolution-dependent layouts</a> swap in different CSS files based on viewport size.</p>
<p>A live site that uses image-swapping to simulate scaling is <a href="http://www.artlogic.com/">Art &amp; Logic</a>. There are four illustrations under the banner on the home page. Try narrowing or widening your browser window; the images don’t scale in real time in most browsers, but as soon as you stop moving the window, they jump to a different size to match the available new space.</p>
]]></content:encoded>
			<wfw:commentRss>http://zomigi.com/blog/foreground-images-that-scale-with-the-layout/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
