PandaText / Articles / How to wrap text around an image in HTML and CSS

How to wrap text around an image in HTML and CSS

Search for how to wrap text around an image in HTML or CSS and nearly every answer floats the image to one side. That covers most layouts, and with shape-outside the edge can even curve into a circle. What no shipping browser does is put the image in the middle of a paragraph and flow the text on both sides of it.

I wanted exactly that for PandaText: a circle in the middle, each line starting on the left and continuing on the right, and a caret that can move through all of it. The picture further down is Sonnet 18 in the PandaText web editor, laid out that way.

Wrap text on one side

Put the image in the paragraph, float it left or right, and give it a width. The line boxes fill the space beside the float. MDN's shape-outside example floats the image left and sets shape-outside: circle(50%). MDN marks that property widely available since January 2020.

circle(50%) curves the outline of that float, but the image still sits on the left or the right. For an image inside a line, a plain img on the baseline is enough. That's what the last line of the picture below does: “An image sits on the line”, the small picture, then “like a letter.”

CSS can't put text on both sides

This is the layout I mean. The first line of the sonnet is split by the circle: on the left it reads “Shall I compare thee to a summer's day? Thou art”, and on the right, “more lovely and more temperate.” Under the sonnet there is a Japanese line, an Arabic line set on the right, “Accents and emoji: café” with a flag and a panda, and the inline image.

Sonnet 18 in the PandaText web editor. A circle sits in the middle of the paragraph, and the verse continues on the left and the right. Below are a Japanese line, an Arabic line, a line with emoji, and a small image between “An image sits on the line” and “like a letter.”
Sonnet 18 in the PandaText web editor, with the circle in the middle of the paragraph.

CSS has a spec for it, the exclusions draft of 22 October 2025. Its wrap-flow: both lets inline content flow on all sides of the exclusion, and if a browser implemented exclusions together with CSS shapes, shape-outside would set the outline. None does. On Can I use, Chrome, Safari and Firefox are unsupported up to the newest versions listed, Chrome 161, Safari 27.3 and Firefox 163. The only support was in Internet Explorer 10 and 11 and in Edge 12 to 18, and Edge 79 and later are unsupported. Global usage comes to 0.17%.

MDN's shapes guide lists shape-inside and shape-padding in the same module, and no browser supports those either.

The workaround in the CSS-Tricks article on shape-outside fakes a centered image: split it in half, float the halves toward the middle, and split the copy into two columns. The catch is that you read the whole left column first. Editing is worse. contenteditable can edit text beside a float, because the browser built those line boxes. A circle in the middle gets none, so the caret has nowhere to sit.

Pretext finds both gaps, and you paint them

Pretext (@chenglou/pretext) takes line breaking away from the browser. It measures text with the browser's canvas, breaks lines from those widths and gives you strings back. Its float sample passes one width to each line, the column minus the image beside it, which is what a CSS float already does.

Shapes are in the dynamic-layout demo, which turns a logo into a polygon. The README says the demos don't ship in the npm package. Its wrap-geometry.ts can return two gaps for one row, a left one and a right one. I expected the page to paint both. It keeps the widest gap and calls layoutNextLine once.

For both sides you do it yourself: break one line for the left gap and another for the right, and paint them on the same baseline, with a DOM node, canvas fillText or SVG. You also draw the circle and handle the keyboard. The demo's lines can be selected, because they are text in the page. For editing, the README's note about long text edited live describes a textarea split into paragraphs: the page remeasures the paragraph that changed, then sets the height, and there is no caret in that example.

In Pretext, the small image on the last line would be the rich-inline API, a box that is only a width, where you paint the image. The README says this is not a general CSS inline formatting engine. Pretext gives no bidi levels or visual order either, so an Arabic line comes out in the order you paint it. Outside a browser you supply your own measureText, and there is no text field for iOS or Android.

Editing it in PandaText

In PandaText the whole picture is one editable document. The caret moves through the lines on both sides of the circle, and the Japanese line, the Arabic line and the inline image are part of the same text.

The same editor runs on iOS, macOS and Android. Typing uses the platform keyboard, and inline images are available on every platform. The web docs cover the browser build.

Sources

Related articles