Message bubble CSS: chat bubbles with tails and grouped runs
A message bubble in CSS is a box with rounded corners, a width cap and a side to sit on. The starter below adds grouped runs and a tail drawn with two pseudo-elements, and you can paste it into a page and run it.
Free skills to start. Pro is $79 a year for every skill, or $9.99 a month for 10. See pricing
What is a message bubble css?
A chat bubble is not a special element. It is a block with `border-radius`, padding, a background colour and a `max-width`. Short messages hug their text and long ones wrap. Your messages sit on the right and the other person's sit on the left, which in a flex column is one alignment value per side.
What makes a thread look like a messaging app is the grouping. Messages from one speaker in a row form a run. Inside a run the corners that face the neighbouring bubble get a small radius, so the run reads as one block, and only the last bubble gets a tail. A few lines of script mark each bubble as first or last in its run, and the CSS does the rest.
The tail is drawn with `::before` and `::after`. One pseudo-element is a scrap of the bubble colour with a rounded corner, the other is a scrap of the page colour that carves the curve. MDN says generated content is not reliably read by screen readers, so the tail stays decorative and every word lives in real elements. A pseudo-element is also not rendered unless `content` has a valid value other than `normal` or `none`, so each one needs `content: ""`.
How the effect works
Sizing uses `max-width: min(80%, 36rem)`. The `min()` function takes the smallest of its arguments and accepts mixed units, so the bubble is capped at 80% of its container on a phone and at 36rem on a wide screen. Percentages in `max-width` resolve against the width of the containing block.
Corner radii have a rule that most tutorials skip. The CSS Backgrounds spec says that when adjacent radii add up to more than a side, browsers must "proportionally reduce" all radii until none overlap. That is why a very large radius on a one-line bubble still gives a clean pill, but it also means a huge radius can shrink corners you meant to keep small. Pick a radius close to the line height instead.
A bubble that plays or animates in is moving content. WCAG 2.2.2 asks for a way to pause, stop or hide movement that starts automatically, lasts more than five seconds and runs alongside other content. A single pop-in is far shorter, but a looping typing indicator is not, so give it a stop control, and wrap any motion in a `prefers-reduced-motion` check.
01
Build the list
Put each message in a list item with a `data-from` value. Add a visually hidden speaker name to the first item of each run so a screen reader hears who is talking.
02
Style one bubble
Give it `border-radius`, padding and `max-width: min(80%, 36rem)`. Send your side right with `justify-content: flex-end` on its row and the other side left.
03
Mark the runs
In script, compare each item's `data-from` with its neighbours and add `is-first` and `is-last`. Run it again after you append a message.
04
Tighten the inner corners
On the side facing the speaker, set a small radius on every corner that touches another bubble of the same run, and a square corner on the last one where the tail goes.
05
Draw the tail
On `.is-last`, add `::before` in the bubble colour with a rounded outer corner and `::after` in the page colour with a smaller rounded corner. Put both behind the text with a stacking context.
HTML
Chat bubbles with runs and tails
Type in the box and press Enter to add a message from you. Change the colours in the :root block. The tail only matches if the ::after colour equals the chat background.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Message bubble CSS</title>
<style>
:root {
--bg: #f4efe6;
--them: #ffffff;
--me: #2f6fed;
--ink: #1c1b1a;
--r: 18px;
--r-in: 4px;
}
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #d9d2c4; font: 16px/1.4 system-ui, sans-serif; color: var(--ink); }
.chat { width: min(92vw, 520px); background: var(--bg); border-radius: 16px; padding: 16px; }
.thread { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.msg { display: flex; }
.msg.is-first { margin-top: 12px; }
.msg:first-child { margin-top: 0; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
.bubble {
position: relative;
isolation: isolate;
max-width: min(80%, 36rem);
padding: 8px 14px;
border-radius: var(--r);
overflow-wrap: anywhere;
}
.msg[data-from="them"] { justify-content: flex-start; }
.msg[data-from="me"] { justify-content: flex-end; }
[data-from="them"] .bubble { background: var(--them); }
[data-from="me"] .bubble { background: var(--me); color: #fff; }
/* inner corners of a run */
[data-from="them"]:not(.is-first) .bubble { border-top-left-radius: var(--r-in); }
[data-from="them"]:not(.is-last) .bubble { border-bottom-left-radius: var(--r-in); }
[data-from="me"]:not(.is-first) .bubble { border-top-right-radius: var(--r-in); }
[data-from="me"]:not(.is-last) .bubble { border-bottom-right-radius: var(--r-in); }
/* tail on the last bubble of a run */
.is-last .bubble::before,
.is-last .bubble::after { content: ""; position: absolute; bottom: 0; z-index: -1; }
.is-last .bubble::before { width: 20px; height: 20px; }
.is-last .bubble::after { width: 10px; height: 20px; background: var(--bg); }
[data-from="them"].is-last .bubble { border-bottom-left-radius: 0; }
[data-from="them"].is-last .bubble::before { left: -7px; background: var(--them); border-bottom-right-radius: 16px; }
[data-from="them"].is-last .bubble::after { left: -10px; border-bottom-right-radius: 10px; }
[data-from="me"].is-last .bubble { border-bottom-right-radius: 0; }
[data-from="me"].is-last .bubble::before { right: -7px; background: var(--me); border-bottom-left-radius: 16px; }
[data-from="me"].is-last .bubble::after { right: -10px; border-bottom-left-radius: 10px; }
.composer { display: flex; gap: 8px; }
.composer input { flex: 1; font: inherit; padding: 8px 12px; border: 1px solid #b9b2a4; border-radius: 999px; background: #fff; }
.composer button { font: inherit; padding: 8px 16px; border: 0; border-radius: 999px; background: var(--me); color: #fff; cursor: pointer; }
.composer :focus-visible { outline: 3px solid #1c1b1a; outline-offset: 2px; }
@media (prefers-reduced-motion: no-preference) {
.msg.is-new .bubble { animation: pop 0.18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
}
</style>
</head>
<body>
<main class="chat">
<ol class="thread" id="thread" aria-label="Conversation">
<li class="msg" data-from="them"><span class="bubble">Did the build pass?</span></li>
<li class="msg" data-from="them"><span class="bubble">I saw a red check an hour ago.</span></li>
<li class="msg" data-from="me"><span class="bubble">Yes, it is green now.</span></li>
<li class="msg" data-from="me"><span class="bubble">The fix was one missing import, and I added a test so it cannot slip back in again.</span></li>
<li class="msg" data-from="them"><span class="bubble">Great, shipping it.</span></li>
</ol>
<form class="composer" id="form">
<input id="text" type="text" placeholder="Write a message" aria-label="Message" autocomplete="off">
<button type="submit">Send</button>
</form>
</main>
<script>
const thread = document.getElementById('thread');
const names = { me: 'You', them: 'Ana' };
function markRuns() {
const items = [...thread.children];
items.forEach((li, i) => {
const from = li.dataset.from;
const first = !items[i - 1] || items[i - 1].dataset.from !== from;
const last = !items[i + 1] || items[i + 1].dataset.from !== from;
li.classList.toggle('is-first', first);
li.classList.toggle('is-last', last);
const old = li.querySelector('.sr');
if (old) old.remove();
if (first) {
const sr = document.createElement('span');
sr.className = 'sr';
sr.textContent = names[from] + ': ';
li.prepend(sr);
}
});
}
document.getElementById('form').addEventListener('submit', (e) => {
e.preventDefault();
const input = document.getElementById('text');
const value = input.value.trim();
if (!value) return;
const li = document.createElement('li');
li.className = 'msg is-new';
li.dataset.from = 'me';
const bubble = document.createElement('span');
bubble.className = 'bubble';
bubble.textContent = value;
li.append(bubble);
thread.append(li);
input.value = '';
markRuns();
});
markRuns();
</script>
</body>
</html>A starting point written for this page, free to use in your own projects. The skills below build the finished version with checks, fallbacks and a recorded video.
What to get right on a real page
01
Use textContent for message text
Messages are user input. Set them with `textContent`, as the starter does, not `innerHTML`, so a pasted tag cannot become markup.
02
Match the ::after colour to the background
The tail's inner curve is a scrap painted in the page colour. If the chat sits on a gradient or an image, that scrap shows as a patch. Draw the tail with a `clip-path` or an SVG shape instead.
03
Keep the radius near the line height
Radii that overlap are scaled down together by the spec's rule, so an oversized value can quietly flatten the small inner corners of a run. Use values like 18px and 4px.
04
Keep words out of pseudo-elements
MDN says generated content is not reliably announced by screen readers. Use `::before` and `::after` only for the tail shape, never for names, times or message text.
05
Cap width with min()
`max-width: min(80%, 36rem)` keeps a bubble readable on a wide screen and inside a narrow container without a media query.
06
Honour reduced motion
Put pop-in and typing animations inside `@media (prefers-reduced-motion: no-preference)`, so people who asked for less motion see the message at once.
Skills that build a message bubble css
The bubbles on this page are the starting point. If you need a whole chat section for a landing page, with typing, tool calls and playback, this skill ships one.
- Chat Demo SectionClaude CodeCodexCursor+3 more agents
Page Kits · Pro
Chat Demo Section
Scripted AI chat demo from chat.json · CSS message bubbles with tails · typing dots, tool calls, streaming reply, code with Copy · pauses off screen · HTML, embed and React component
It builds a chat demo section for an AI product's landing page from one chat.json. Its message bubbles are plain CSS: runs of one speaker share tight inner corners, and the last bubble of a run gets a tail made of two pseudo-elements. The whole transcript is real text in the HTML from the first paint, and reduced motion shows the finished conversation.
Worth knowing: A user message is at most 280 characters and an assistant message 640, and the conversation is 2 to 12 messages.
Needs: Node.js 18+, and Chrome for the checks; ffmpeg only for the clip
See what Chat Demo Section makes
Install
Build a message bubble css with your agent
Each skill is a SKILL.md folder, so it installs the same way in Claude Code, Codex, Cursor and other agents. The example below uses Chat Demo Section; each skill page has its own prompt.
1. Paste the install prompt
Your agent runs the installer. The first time, it shows a sign-in link so you can approve it in the browser.
Install the "Chat Demo Section" skill from Lookbk (https://lookbk.dev/skills/chat-demo-kit). 1. Run: curl -fsSL https://lookbk.dev/cli.js | node - add chat-demo-kit 2. If it prints a sign-in link, show it to me. When I say I approved it, run the same command again. 3. When the install finishes, read the SKILL.md it installed and tell me what you need from me to make the first result.
Or run it in a terminal
curl -fsSL https://lookbk.dev/cli.js | node - add chat-demo-kit2. Start a new session
Agents load skills when a session starts. The installer writes the folder where your agent looks:
- Claude Code
- ~/.claude/skills/chat-demo-kit
- Codex
- ~/.agents/skills/chat-demo-kit
- Cursor
- ~/.agents/skills/chat-demo-kit
3. Ask for the result
Describe it in plain words, for example:
Use the Chat Demo Section skill to add a chat demo to our landing page in the console style: a support lead asks our agent why refunds failed overnight, it calls our payments and logs tools, answers with the cause and a SQL fix, and offers the runbook as a file. Give me the embed and the Next.js component.
Another agent? Install a skill in any agent
FAQ
Questions
Still stuck? The guides cover installing, agents and licensing in more detail.
Read the guidesHow do I make a chat bubble with a tail in CSS?
Give the last bubble of a run a square corner on the speaker's side. Add `::before` in the bubble colour with a rounded outer corner, and `::after` in the page colour with a smaller rounded corner to cut the curve. Both need `content: ""`, or they are not rendered.
How do I put my messages on the right and theirs on the left?
Make the list a flex column and the item a flex row. Use `justify-content: flex-end` for your messages and `flex-start` for theirs. The starter does this with a `data-from` attribute.
Why does a big border-radius still look right on a short bubble?
The CSS Backgrounds spec says that when radii overlap, browsers proportionally reduce all of them until none do. A one-line bubble with a large radius therefore ends up a pill. The same rule can shrink corners you wanted small.
Should the tail be a pseudo-element or an image?
A pseudo-element is enough when the chat background is a flat colour. MDN advises against relying on generated content for meaning, which is fine here because the tail carries none. On a gradient or photo, use a `clip-path` or SVG instead.
Do message bubbles need to respect reduced motion?
If they animate in, yes. `prefers-reduced-motion: reduce` means the user asked their device to minimize non-essential motion, so wrap the pop-in in the `no-preference` query. Anything that loops for more than five seconds also needs a pause control under WCAG 2.2.2.
