Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Resizing Images

, January 01, 2002


WebReview.com: Resizing Images

Rank: 2

At a Glance

We started a new series on images last week, covering basic image use in your documents. This week, we continue with image sizing and scaling.

If you are lucky, the image you want to insert in your document is the size you need. If not, you'll need to resize the image to meet your needs.

Image size is controlled by two attributes of the <img> tag: HEIGHT and WIDTH. As you can probably guess, these attributes specify the height and width, in pixels, of the image you've placed in your document. If the image you specified with the SRC attribute does not match these sizes, it will be stretched or shrunk to fit.

To see the effects of these attributes on an image, we're going to use this simple 100 x 100 image:

To change this to a 200 x 200 image, you could add HEIGHT="200" WIDTH="200" to the <img> tag and get:

Conversely, we can make it smaller by setting HEIGHT and WIDTH to 50:

Right away, you can see the downside of image resizing: Depending on your browser, you may notice jagged edges in the color blocks in the large image, and missing pixels in the smaller image. This leads to a useful bit of advice: Don't explicitly resize images unless you absolutely have to. Instead, create the desired size using an image editor (like Photoshop), where you can repair jagged edges and missing pixels before publishing the image on your site.

If you are resizing the image, don't forget that you need not adjust the HEIGHT and WIDTH in equal amounts. You can also make the height larger and the width smaller:

Again, you will probably only use this feature when trying to achieve a specific effect with your images.

Scaling images

Most of the time, it makes more sense to scale an image instead of resizing its height and width. If you specify either HEIGHT or WIDTH, the browser will compute the other image dimension, scaled to match. Thus if you set the height to 200 pixels, with the original image being 100 pixels tall, the browser will compute the width to be twice the original width as well.

This kind of image scaling is always preferable to specifying both image dimensions. If you change the image's size, your page will still look correct because the browser will scale the image appropriately based upon its new size. If nothing else, scaling saves you the trouble of doing the math to compute both dimensions of the image.

The last kind of image scaling involves resizing the image to match the current text flow size. If you provide a percentage value to the WIDTH attribute, the browser will scale the image so that it's width matches that percentage of the text flow width. For example, using WIDTH="75%" creates an image that is scaled accordingly:

You can mix and match these, of course. Here is the image, just 10 pixels high but stretched to fill 60 percent of the text flow:

Note that setting the HEIGHT attribute to a percentage value doesn't make much sense, since HTML documents tend to have a variable length. Browser behavior varies when you do this; most use the height of the current window as the base value when computing a percentage height. In general, this is not much use, but to satisfy your curiosity, here is our image with the height set to 50 percent. What did your browser do?

All sorts of image effects are possible with image sizing and scaling. Play around with them a bit; next week, we'll take a look at image borders and alignment.

Next: Image Borders and Alignment


Previously in Tag of the Week:

Basic Image Insertion
Building Forms with Tables
Life Without Mice


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.