<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t" -->

---
title: Shrink-wrap Text with `position-anchor` | daily.dev
description: A CSS technique uses the anchor-name property and position-anchor to shrink-wrap a background around wrapped inline text, mimicking the iOS chat bubble look....
canonical: https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Shrink-wrap Text with `position-anchor` | daily.dev
og:description: A CSS technique uses the anchor-name property and position-anchor to shrink-wrap a background around wrapped inline text, mimicking the iOS chat bubble look....
og:url: https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t
og:image: https://api.daily.dev/og/posts/RFPl54n7t.png
og:image:alt: Shrink-wrap Text with `position-anchor`
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Shrink-wrap Text with `position-anchor`

**[CodePen](https://daily.dev/sources/codepen)** · 3 min read · 3 upvotes · 1 comments

## Summary

A CSS technique uses the anchor-name property and position-anchor to shrink-wrap a background around wrapped inline text, mimicking the iOS chat bubble look. Since inline-size: fit-content or min-content don't size correctly to wrapped lines, an anchor is added to a span containing the text, and a pseudo-element on the parent is positioned and sized using anchor-size(height) and anchor-size(width), letting padding, borders, and shadows be applied without affecting text flow. The approach works in most modern browsers and degrades gracefully.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.codepen.io/2026/09/11/shrink-wrap-text-with-position-anchor>

## Questions this post answers

### How can I make a background shrink-wrap around wrapped text in CSS, like iOS chat bubbles?

Add anchor-name: --text to an inline span wrapping the text, then create a ::before pseudo-element on the parent with position-anchor: --text, place-self: anchor-center, and width/height set via anchor-size(width) and anchor-size(height). This sizes the background exactly to the wrapped text lines, unlike inline-size: fit-content or min-content, which either fail to size to wrapped text or squish to the longest word.

_daily.dev surfaces practical CSS techniques like this for developers polishing text-heavy UI details._

### Why doesn't inline-size: fit-content work for shrink-wrapping a background around wrapped text?

inline-size: fit-content and inline-size: min-content don't size correctly to text that has wrapped across multiple lines; fit-content doesn't adapt properly to the wrapped shape, while min-content awkwardly squishes the container to the width of the longest word rather than matching the actual wrapped text block.

_Developers debugging odd CSS sizing behavior can find focused fixes like this through daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> Nice workaround for something fit-content really sounds like it should already handle

## Similar posts on daily.dev

- [Getting Started with Anchor Positioning • Josh W. Comeau](https://daily.dev/posts/getting-started-with-anchor-positioning-josh-w-comeau-gk8wgqtjg) · Josh W Comeau · 83 upvotes · 3 comments
- [CSS does your tooltip positioning now](https://daily.dev/posts/css-does-your-tooltip-positioning-now-4wx8q4s5u) · Matt Smith · 9 upvotes · 2 comments

---

Tags: [#css](https://daily.dev/tags/css), [#ui-design](https://daily.dev/tags/ui-design)

[View this post on daily.dev](https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Shrink-wrap Text with `position-anchor`","url":"https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t"},"datePublished":"2026-09-11T22:51:31.645Z","dateModified":"2026-09-11T22:51:51.565Z","description":"A CSS technique uses the anchor-name property and position-anchor to shrink-wrap a background around wrapped inline text, mimicking the iOS chat bubble look....","image":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","thumbnailUrl":"https://media.daily.dev/image/upload/s--0_ODbtD2--/f_auto/v1722860399/public/Placeholder%2008","isAccessibleForFree":true,"articleSection":"CodePen","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"CodePen","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/codepen","url":"https://daily.dev/sources/codepen"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":3},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"css,ui-design","timeRequired":"PT3M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"CodePen","item":"https://daily.dev/sources/codepen"},{"@type":"ListItem","position":3,"name":"Shrink-wrap Text with `position-anchor`"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t","comment":[{"@type":"Comment","text":"Nice workaround for something fit-content really sounds like it should already handle","datePublished":"2026-09-13T10:21:08.684Z","url":"https://daily.dev/posts/RFPl54n7t#c-OTbkeWpAV","author":{"@type":"Person","name":"Agustin Barrientos","url":"https://daily.dev/agustinbarrientos","image":"https://media.daily.dev/image/upload/s--5ayxQnqn--/f_auto/v1788281802/avatars/avatar_wQYYVe5Tbj0NJ7C7qPoa8?_a=BAMAMicg0"}}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/shrink-wrap-text-with-position-anchor--rfpl54n7t#faq","mainEntity":[{"@type":"Question","name":"How can I make a background shrink-wrap around wrapped text in CSS, like iOS chat bubbles?","acceptedAnswer":{"@type":"Answer","text":"Add anchor-name: --text to an inline span wrapping the text, then create a ::before pseudo-element on the parent with position-anchor: --text, place-self: anchor-center, and width/height set via anchor-size(width) and anchor-size(height). This sizes the background exactly to the wrapped text lines, unlike inline-size: fit-content or min-content, which either fail to size to wrapped text or squish to the longest word. daily.dev surfaces practical CSS techniques like this for developers polishing text-heavy UI details."}},{"@type":"Question","name":"Why doesn't inline-size: fit-content work for shrink-wrapping a background around wrapped text?","acceptedAnswer":{"@type":"Answer","text":"inline-size: fit-content and inline-size: min-content don't size correctly to text that has wrapped across multiple lines; fit-content doesn't adapt properly to the wrapped shape, while min-content awkwardly squishes the container to the width of the longest word rather than matching the actual wrapped text block. Developers debugging odd CSS sizing behavior can find focused fixes like this through daily.dev."}}]}
```

