Advanced Images



index
Disabled back button Next Section
printable version

Section 1: Figures and figcaption

The <figure> element is intended to be used in conjunction with the <figcaption> element to mark up diagrams, illustrations, photos, and code examples (among other things). The <figcaption> element defines a caption for a <figure> element, and can be placed as the first or last child of the <figure> element.

Section 2: More on Centering Images

You should recall that centering images requires that you set the display property to "block" and set the left and right margins to "auto".

display:block;
margin-left:auto;
margin-right:auto;

You also probably want to style the caption.

figure.centered
{
margin: 0px auto;
}
figcaption.centered
{
font-size:.7em;
color:black;
text-align: center;
}
figure.centered img
{
display:block;
margin-left:auto;
margin-right:auto;
}

<figure class="centered">
<img src="figures/pigHula.gif" alt="Hula pig." >
<figcaption class="centered">Pig doing the hula.</figcaption>
</figure>

And here is the result:

Hula pig.
Pig doing the hula, hula, hula, hula.

You can even use intrinsic sizing and still center the minimally sized figure.

figure.centered2
{
width: -webkit-min-content;
width: -moz-min-content;
width: min-content;
margin-left:auto;
margin-right:auto;
}

Here is the result:

Hula pig.
Pig doing the hula, hula, hula, hula.

Resources

Section 3: More on Image Size

Recall that to display a full-size image it is usually adequate to specify only the width in units like pixels or ems, but if you specify the width using a percentage, the browser will interpret that as the percentage of the container in which the image is placed, not as a percentage of its natural size.

There is a new sizing approach in CSS called "intrinsic".

Here is an example from StackOverflow:

figure
{
width: intrinsic; /* Safari/WebKit uses a non-standard name */
width: -moz-max-content; /* Firefox/Gecko */
width: -webkit-max-content; /* Chrome */
}
img
{
width: 50%;
}

<figure>
<img src="example.png" alt="example image.">
</figure>

Here is a quarter-size pig:

quarter size image.

Compare it to the full-size pig:

full size image.

Intrinsic sizing is not available in many browsers: link.

Section 4: CSS for Images: Layout
float property

The float property allows text to flow around other elements.

Section 5: CSS for Images: Margins

Margins can be used to prevent text from abutting an image.

<img class="floatLeft" src = "annieFace.gif" >

.floatLeft
{
float: left;
margin-right: 0.75em;
margin-bottom: 0.3em;
}

See this link for the demo above.

Section 6: CSS for Images: Background

The background properties include color, image, repeat, position, and attachment and apply to various elements (body, paragraph, table, etc.) W3C Link

Section 7: Image Compression and Optimization

If you did not read the Graphic Design notes, you should at least review Image Compression and Image Optimization.