Sub topic 12
Wrapping text around images and shapes
This chapter assumes you can place images and text on a grid and crop an image to a chosen size. Familiarity with basic CSS or a design tool's text wrap panel is enough for the examples.
What text wrap does
Text wrap makes body text flow around an image instead of stopping above or below it. The image sits at one side of a column, and lines of text run alongside it. Wrap works best in long-form text such as articles, reports and book pages. It works poorly in short captions or headlines.
Wrap has two parts: the object that pushes the text away, and the shape of the area it pushes. The shape is either the object's rectangular box or a custom outline.
Choosing a wrap style
| Style | Text follows | Good for |
|---|---|---|
| Box wrap | The rectangle around the image | Photos with straight edges |
| Contour wrap | The visible edge of the subject | Cutouts and illustrations |
| Shape wrap | A circle, polygon or path you define | Portraits and decorative layouts |
| No wrap | Nothing, text jumps past the image | Full-width images |
A shaped wrap in CSS
In CSS, shape-outside changes the wrap area of a floated element. The float is required. Without it, the property has no effect.
.portrait {
float: left;
width: 12rem;
height: 12rem;
margin: 0 1.5rem 1rem 0;
border-radius: 50%;
object-fit: cover;
shape-outside: circle(50%);
}Two properties do separate jobs here. border-radius crops what you see, and shape-outside shapes how the text wraps. Set both to the same shape, or the text will follow an outline that does not match the picture.
Add shape-margin to control the gap between the curve and the text. A value such as 0.5rem keeps letters from touching the edge.
Keeping the wrap readable
Wrapping can damage text if you do not check the result.
- Keep the gap even. Use the same distance on all sides, and match it to your grid's gutter.
- Watch line length. If the image takes too much of the column, the text beside it becomes very narrow. Lines under about 25 characters produce ragged edges and awkward gaps between words.
- Avoid wrapping a justified paragraph around a curve. Word spacing widens on short lines, which leaves holes.
- Keep the image edge aligned with the grid. A floated image should start at a column edge, and its top should line up with the first line of text.
- Do not wrap text around an image that carries a caption unless the caption stays attached. Put the image and caption in a
figureso they move together.
<figure>
<img src="harbor.jpg" alt="Fishing boats moored in a harbor at dawn">
<figcaption>Harbor at dawn, 6:10 a.m.</figcaption>
</figure>Common errors
- The shape has no effect. The element is not floated, or the image has not loaded, so its dimensions are unknown.
- The text overlaps the image. Check the margin, since a missing
shape-marginleaves no gap. - The wrap looks fine on a wide screen but breaks on a phone. On narrow screens, remove the float and stack the image above the text.
Exercise
A circular portrait is floated left with border-radius: 50%, but the paragraph beside it wraps along a square edge. What property is missing, and what must the element also have?
Show answer
It needs shape-outside: circle(50%). The element must also be floated (or otherwise eligible for wrapping), because shape-outside only affects floated elements. border-radius crops the visible image but does not change the wrap area.