Menu

Shrink-wrap Text with `position-anchor` – CodePen
📰

Shrink-wrap Text with `position-anchor` – CodePen

#codepen#anchor#size#position#background
Long article (631 words) — no AI summary yet.
Reading 0:00
15s threshold

Normal text wrapping can leave big gaps in your container, which can become even more exaggerated with the new-ish CSS values of text-wrap: balance and text-wrap: pretty . You almost certainly want to use balance or pretty because they do a great job preventing “dangling” lines in your text, even with icons .

But what can we do about all that leftover space around the wrapped text?

You’d think inline-size: fit-content or inline-size: min-content would be the ticket, but those don’t size to wrapped text or awkwardly squish to the longest word.

Look at the chat bubbles in iOS. They conform nicely to the text, whether it’s a short line or wrapped.

Surely we can accomplish that with CSS, right? Right???

Roman Komarov’s experimental technique involving animation timelines felt a bit too complicated. I couldn’t find any other passable techniques, but I had an idea…

Roman kindly pointed out that he already posted about a technique using position-anchor to shrink wrap back in 2023 !…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.