See also the index of all tips.
On this page:
There is one problem here, and that is that the image may be too wide. In this case, the image is always 136 px wide and the figure is 30% of the surrounding text. So if you make the window narrower, it may be that the image overflows the figure (try it!).
If you know the width of all images in the document, you can add a minimum width to figure, like this:
figure { min-width: 150px; }
Another way is to scale the image itself. That's what we have done with the image on the right here. As you can maybe see if you make the window very wide, JPEG images don't scale very well. But if the image is a diagram or a graph in SVG format, scaling in fact works beautifully. Here is the mark-up we used:
<figure> <p><img class=scaled src="st-tropez.jpg" alt="St. Tropez"> <figcaption>Saint Tropez and its fort in the evening sun</figcaption> </figure>
And this is the style sheet:
figure { float: right; width: 30%; text-align: center; font-style: italic; font-size: smaller; text-indent: 0; border: thin silver solid; margin: 0.5em; padding: 0.5em; } img.scaled { width: 100%; }
The only addition is the last rule: it makes the image as wide as the inside of the figure (the area inside the border and the padding).
Scale model of the Eiffel tower in Parc Mini-France
HTML4, unlike HTML5, doesn't have an element that inserts a figure with a caption. It was proposed (see HTML3), but didn't made it into HTML4. Here is one way to simulate a figure element:
<div class=figure> <p><img src="eiffel.jpg" width="136" height="200" alt="Eiffel tower"> <p>Scale model of the Eiffel tower in Parc Mini-France </div>
Then in the style sheet you use the class "figure" to format the figure the way you want. For example, to float the figure to the right, in a space equal to 30% of the width of the surrounding paragraphs, these rules will do the trick:
div.figure { float: right; width: 30%; text-align: center; font-style: italic; font-size: smaller; text-indent: 0; border: thin silver solid; margin: 0.5em; padding: 0.5em; }
As before, only the first two declarations (float and width) are essential, the rest is just for decoration.
To avoid that wide images overflow the figure, and if you know the width of all images in the document, you can add a minimum width to the DIV, e.g., like this:
div.figure { min-width: 150px; }
Saint Tropez and its fort in the evening sun
To scale the image to the width of the figure instead (as we have done with the image on the right), you can add a CLASS attribute and a CSS rule, very similar to the HTML5 example above. Here is the mark-up we used:
<div class=figure> <p><img class=scaled src="st-tropez.jpg" alt="St. Tropez"> <p>Saint Tropez and its fort in the evening sun </div>
And this is the style sheet:
div.figure { float: right; width: 30%; border: thin silver solid; margin: 0.5em; padding: 0.5em; text-align: center; font-style: italic; font-size: smaller; text-indent: 0; } img.scaled { width: 100%; }
The only addition is the last rule: it makes the image as wide as the inside of the DIV (the area inside the border and the padding).
Created 17 January 2001;
Last updated Wed 06 Jan 2021 05:40:49 AM UTC