CSS Text on Border
Two methods for placing text on a border in CSS: a <fieldset> with a <legend>, and a heading whose ::before and ::after draw the line.
- Level
- Beginner
- Published
- Updated
- Reading time
- 7 min
Try it
4 live demos
Your browser can't run this demo
It needs :has() and color-mix(), which this browser doesn't support yet. It works in Chrome 111+, Edge 111+, Firefox 121+ and Safari 16.2+.
Needs :has() and color-mix()
fieldset + legend
Plan details
- Storage
- 20 GB
- Members
- 5 seats
- Billing
- Monthly
heading + ::before and ::after lines
Two ways to sit a title on the line
Other ways to build it
or
- Are we still on for Friday?
- Yes, see you at noon.
New
- Can we move it to one?
The "or" divider
See the codeType an address without the @, then press Tab to see the error state.
A label in a notch
See the codeBackground patch
The patch hides the line, and hides the gradient with it.
Patch: fails
fieldset + legend: works
Line method
The lines stop short of the words.
Line method: works
On a gradient
See the codeBuild it
8 snippets to copy
CSS Text on Border
HTML
<div class="tb">
<fieldset class="tb-align">
<legend>Title position</legend>
<label><input class="tb-left" type="radio" name="tb-align" checked> Left</label>
<label><input class="tb-center" type="radio" name="tb-align"> Center</label>
<label><input class="tb-right" type="radio" name="tb-align"> Right</label>
</fieldset>
<!-- one: a real fieldset and legend -->
<fieldset class="tb-fieldset">
<legend class="tb-legend">Delivery address</legend>
<label class="tb-row">Street <input type="text" placeholder="12 Main Street"></label>
<label class="tb-row">City <input type="text" placeholder="Springfield"></label>
</fieldset>
<!-- two: a card whose heading draws the top line -->
<section class="tb-card" aria-labelledby="tb-card-title">
<h3 class="tb-title" id="tb-card-title">Plan details</h3>
<div class="tb-body">
<p>Storage: 20 GB. Members: 5 seats. Billing: monthly.</p>
</div>
</section>
</div>
CSS
.tb {
display: grid;
gap: 1.5rem;
max-width: 24rem;
padding: 1.5rem;
font-family: system-ui, sans-serif;
color: #f0f0f0;
/* a gradient, to show that nothing is painted behind either title */
background: linear-gradient(135deg, #222b16, #0c0c0d 55%, #0e2330);
}
.tb-align {
margin: 0;
padding: 0;
border: 0;
}
/* One: a real fieldset. The browser cuts the border where the
legend sits, so it works on any background. */
.tb-fieldset {
/* a fieldset will not shrink below its content by default */
min-inline-size: 0;
margin: 0;
padding: .35rem 1.25rem 1.25rem;
border: 2px solid #b8ff57;
border-radius: 12px;
}
/* the line runs through the middle of the legend's box, and the
gap is the legend's border box: padding is the space either side */
.tb-legend {
padding: 0 .6rem;
font-weight: 700;
line-height: 1.75rem;
}
.tb-row {
display: grid;
gap: .3rem;
margin-top: .75rem;
font-size: .8rem;
}
.tb-row input {
padding: .5rem .7rem;
border: 1px solid #2a2a2d;
border-radius: 8px;
font: inherit;
color: inherit;
background: #1c1c1e;
}
/* Two: a card. The heading is a flex row whose ::before and
::after are the top line and the two top corners; the body
draws the sides and the bottom. */
.tb-card {
--tb-before: 0 0 calc(1.25rem + 2px);
--tb-after: 1 1 0;
}
.tb-title {
display: flex;
align-items: flex-end;
gap: .6rem;
margin: 0;
font-size: 1rem;
line-height: 1.75rem;
}
/* half the 1.75rem row plus half the 2px line, so the line
runs through the middle of the words */
.tb-title::before,
.tb-title::after {
content: "";
box-sizing: border-box;
min-width: 12px;
height: calc(.875rem + 1px);
border-top: 2px solid #b8ff57;
}
.tb-title::before {
flex: var(--tb-before);
border-left: 2px solid #b8ff57;
border-top-left-radius: 12px;
}
.tb-title::after {
flex: var(--tb-after);
border-right: 2px solid #b8ff57;
border-top-right-radius: 12px;
}
.tb-body {
padding: .35rem 1.25rem 1.25rem;
border: 2px solid #b8ff57;
border-top: 0;
border-radius: 0 0 12px 12px;
}
.tb-body p {
margin: .4rem 0 0;
}
/* the radios move both titles: auto margins for the legend,
the flex split for the card */
.tb:has(.tb-center:checked) .tb-legend {
margin-inline: auto;
}
.tb:has(.tb-right:checked) .tb-legend {
margin-inline-start: auto;
}
.tb:has(.tb-center:checked) .tb-card {
--tb-before: 1 1 0;
--tb-after: 1 1 0;
}
.tb:has(.tb-right:checked) .tb-card {
--tb-before: 1 1 0;
--tb-after: 0 0 calc(1.25rem + 2px);
}
The "or" divider
A flex row whose ::before and ::after each have flex: 1 and a border-top, so they grow equally and the line stops at the word. Shown here as the "or" between two sign-in buttons and as a colored "New" divider in a message list.
HTML
<div class="tb-signin">
<button class="tb-btn tb-btn-main" type="button">Continue with email</button>
<p class="tb-or">or</p>
<button class="tb-btn" type="button">Send me a sign-in link</button>
</div>
<p class="tb-or tb-or-new">New</p>
CSS
.tb-signin {
display: grid;
gap: .9rem;
max-width: 20rem;
padding: 1.25rem;
border-radius: 12px;
background: #1c1c1e;
}
.tb-btn {
padding: .65rem 1rem;
border: 1px solid #2a2a2d;
border-radius: 8px;
font: inherit;
font-weight: 600;
color: #f0f0f0;
background: #141415;
}
.tb-btn-main {
border-color: #b8ff57;
color: #0c0c0d;
background: #b8ff57;
}
/* a flex row: the ::before and ::after grow to fill the space
either side of the word, and their top border is the line */
.tb-or {
display: flex;
align-items: center;
gap: .75rem;
margin: 0;
font-size: .8rem;
color: #88888f;
}
.tb-or::before,
.tb-or::after {
content: "";
flex: 1;
border-top: 1px solid #2a2a2d;
}
/* the same divider in a color, for unread messages */
.tb-or-new {
font-weight: 700;
color: #ff6b6b;
}
.tb-or-new::before,
.tb-or-new::after {
border-top-color: #ff6b6b;
}
A label in a notch
The same build as the card, where the label row draws the top line and corners and the input draws the rest. Every piece reads two custom properties for the line width and color, so :focus-within changes both by redefining those properties on the wrapper. The outline thickens from 1px to 2px and turns green, and the input's padding compensates so the text never moves. The :user-invalid pseudo-class turns the outline coral when the field is invalid, but only once the reader has typed and left the field. The label stays in the notch. For a label that floats up from inside the field, see CSS floating labels.
HTML
<div class="tb-field">
<label class="tb-notch" for="tb-email">Email</label>
<input class="tb-notch-input" id="tb-email" type="email"
placeholder="you@example.com">
</div>
CSS
/* every piece reads these two, so the whole outline changes at once */
.tb-field {
--tb-w: 1px;
--tb-c: #88888f;
display: flex;
flex-direction: column;
max-width: 20rem;
}
/* the label row draws the top line and the corners */
.tb-notch {
display: flex;
align-items: flex-end;
gap: .35rem;
font-size: .8rem;
font-weight: 600;
line-height: 1.25rem;
color: #88888f;
}
/* half the 1.25rem row plus half the line */
.tb-notch::before,
.tb-notch::after {
content: "";
box-sizing: border-box;
height: calc(.625rem + var(--tb-w) / 2);
border-top: var(--tb-w) solid var(--tb-c);
}
.tb-notch::before {
flex: 0 0 .65rem;
border-left: var(--tb-w) solid var(--tb-c);
border-top-left-radius: 8px;
}
.tb-notch::after {
flex: 1;
border-right: var(--tb-w) solid var(--tb-c);
border-top-right-radius: 8px;
}
/* the input draws the sides and the bottom */
.tb-notch-input {
box-sizing: border-box;
width: 100%;
/* gives back what the thicker focus line takes, so the text never moves */
padding: .2rem calc(.9rem - var(--tb-w)) calc(.8rem - var(--tb-w));
border: var(--tb-w) solid var(--tb-c);
border-top: 0;
border-radius: 0 0 8px 8px;
font: inherit;
color: #f0f0f0;
background: transparent;
outline: none;
}
.tb-field:focus-within {
--tb-w: 2px;
--tb-c: #b8ff57;
}
.tb-field:focus-within .tb-notch {
color: #b8ff57;
}
/* only after the reader has typed and left the field */
.tb-field:has(:user-invalid) {
--tb-c: #ff6b6b;
}
.tb-field:has(:user-invalid) .tb-notch {
color: #ff6b6b;
}
On a gradient
The common shortcut, a heading lifted onto the border with position: absolute and a solid background patch to cover the line, shows as a dark rectangle because the patch color does not match the gradient behind it. The fieldset legend and the line method from the main demo both work, because they draw no background behind the words and the gradient stays visible around the letters.
HTML
<div class="tb-photo">
<!-- the shortcut that fails -->
<div class="tb-patch">
<h4 class="tb-patch-title">Background patch</h4>
<p>The patch hides the line, and hides the gradient with it.</p>
</div>
<!-- the fieldset and the card from the main example, which work -->
<fieldset class="tb-fieldset">
<legend class="tb-legend">Legend</legend>
<p>The browser leaves a gap in the border.</p>
</fieldset>
<section class="tb-card" aria-labelledby="tb-grad-title">
<h4 class="tb-title" id="tb-grad-title">Line method</h4>
<div class="tb-body"><p>The lines stop short of the words.</p></div>
</section>
</div>
CSS
/* adds to the main example: .tb-fieldset, .tb-legend, .tb-card,
.tb-title and .tb-body come from there, with white in place
of the green */
.tb-photo {
display: grid;
gap: 1.5rem;
max-width: 24rem;
padding: 2rem 1.5rem 1.5rem;
border-radius: 14px;
color: #ffffff;
background:
radial-gradient(circle at 15% 20%, #d6336c 0, transparent 42%),
radial-gradient(circle at 85% 30%, #0e7c7b 0, transparent 45%),
radial-gradient(circle at 50% 100%, #b5560c 0, transparent 50%),
linear-gradient(135deg, #4b1d8f, #1d3fa8);
text-shadow: 0 1px 3px rgba(0, 0, 0, .7);
}
/* the shortcut: lift the heading onto the border and cover the
line with a solid patch. It only vanishes on a matching color. */
.tb-patch {
position: relative;
margin-top: calc(.875rem - 1px);
padding: 1.2rem 1.25rem 1.25rem;
border: 2px solid #ffffff;
border-radius: 12px;
}
.tb-patch-title {
position: absolute;
/* the middle of the title on the middle of the 2px line */
top: -1px;
left: 1.25rem;
translate: 0 -50%;
margin: 0;
padding: 0 .6rem;
font-size: 1rem;
line-height: 1.75rem;
background: #141415;
}
.tb-photo p {
margin: .4rem 0 0;
}
.tb-photo .tb-fieldset,
.tb-photo .tb-title::before,
.tb-photo .tb-title::after,
.tb-photo .tb-body {
border-color: #ffffff;
}
Understand it
How it works and 7 properties
Two ways to place a title on a border line. The first is a real <fieldset> and <legend>, where the browser cuts the border gap automatically. The second is a heading whose ::before and ::after draw the line on each side, with flex controlling the split. Both work on any background because neither paints behind the text.
Both methods work on any background, including gradients, patterns and images, because neither one paints a solid patch behind the text. The radios move both titles at once. A fieldset uses margin-inline on its legend, and the card uses the flex split.
How it works
The fieldset method is a form group, and the browser cuts its border where the legend sits. Nothing is painted behind the legend: the gap is automatic. margin-inline: auto centers it or margin-inline-start: auto pushes it right. The line method draws the title as a flex row, where the ::before and ::after pseudo-elements sit at the row's bottom edge with a border-top running across them. Their flex values decide how wide each one grows, which puts the title left, center or right without moving it in the DOM.
The fieldset's gap is the legend's border box, which means its padding is the visible space on each side of the words. With padding: 0 .6rem the line stops about 10 pixels before the letters. Margin moves the whole box sideways, but margin is not part of the gap, so adding margin does not widen the space around the text. The line runs through the vertical middle of the legend's box.
A fieldset defaults to min-inline-size: min-content, which means it will not shrink below the width of its widest piece of content. On a narrow screen, if an input inside the fieldset is wider than the viewport, the fieldset stays as wide as the input and spills sideways. The fix is min-inline-size: 0, which lets the fieldset shrink.
Floating the legend breaks the border gap. A floated legend drops inside the fieldset as an ordinary float, and the browser draws the top border as an unbroken line. That rules out float as a way to move the legend sideways, which is fine because auto margins already do the job without side effects.
The card builds its outline from three pieces. The heading is a flex row with align-items: flex-end, so its ::before and ::after sit at the bottom of the row. Each pseudo-element is calc(.875rem + 1px) tall, which is half the 1.75rem row height plus half the 2px line, so the line lands on the row's center. The ::before draws the left corner and the start of the line with border-left and border-top-left-radius. The ::after draws the right corner and the rest of the line. The body below has border-top: 0 and draws the other three sides, so the pseudo-elements' side borders and the body's side borders meet with no gap.
Position comes from flex on the pseudo-elements. For a left-aligned title, ::before is flex: 0 0 calc(1.25rem + 2px), which keeps it fixed at the padding width plus the border width, and ::after is flex: 1 1 0, which tells it to grow and take the rest of the space. Center sets both to flex: 1 1 0 so they split the space equally. Right swaps the values. A min-width: 12px on both keeps a corner visible on each side even when a very long title pushes one side narrow.
Accessibility notes
The fieldset groups related form controls, and the legend names the group.
The card is a <section> with aria-labelledby pointing at its heading, which makes it a landmark region named by that heading. The heading itself is a real <h3>, so it appears in the page outline. The pseudo-elements that draw the line are decorative and have no content, so they do not appear in the accessibility tree.
The position picker is a fieldset with real radio buttons, clipped to 1px with clip-path: inset(50%) rather than hidden, so they stay in the accessibility tree and keep one tab stop for the group. Arrow keys move between the radios, which is the standard behavior for a radio group. The labels are the visible pills, and clicking one checks its radio.
Mistakes worth avoiding
- Floating the legend to style it. A floated legend is no longer the border legend, so the browser draws the top border as an unbroken line and the gap vanishes.
- Using a background patch on a gradient or image. The patch hides the line and hides the background with it, showing as a solid block instead of blending in.
- Forgetting
min-inline-size: 0on the fieldset. It defaults tomin-content, so a wide input forces the fieldset to overflow on a narrow screen. - Expecting the legend's margin to widen the gap. Margin moves the legend sideways but does not change the space around the text.
- Absolutely positioning the title in the line method. An absolutely positioned element is taken out of flow, so it can overlap the content below instead of pushing it down.
Ship it
4 features in 4 browsers, 5 questions
Browser support
| Feature | ||||
|---|---|---|---|---|
:has() | 105 | 121 | 15.4 | 105 |
flexbox | 21 | 28 | 6.1 | 12 |
gap on flexbox | 84 | 63 | 14.1 | 84 |
:user-invalid | 119 | 88 | 16.5 | 119 |
The fieldset and legend elements are supported everywhere, and so are ::before and ::after with flexbox. The :has() selector is what reads the radios and moves both titles, and it works in Chrome and Edge 105, Firefox 121, and Safari 15.4. The gap property on a flex container is in Chrome and Edge 84, Firefox 63, and Safari 14.1. The :user-invalid pseudo-class in the notch variant is in Chrome and Edge 119, Firefox 88, and Safari 16.5. It matches an invalid input only after the reader has typed and left the field, which is less noisy than :invalid alone.
What you can build with it
- Form sections. A fieldset with a legend grouping address fields, payment details, or account settings, where the browser draws the gap for free.
- Settings cards. A heading on the border of a card showing plan details, usage stats, or preferences.
- Dividers with text. A horizontal rule with a word in the middle, like "or" between sign-in buttons or "New" in a message thread.
- Outlined text fields. A label sitting in a notch at the top of an input, built the same way as the card and reacting to focus and validation states.
- Pricing tables. A plan name on the border of each tier's feature list, where the title is part of the box rather than floating above it.
Frequently asked questions
How do you put text on a border in CSS?
<fieldset> with a <legend>. Or make the heading a flex row whose ::before and ::after pseudo-elements draw the line on each side of the words.How does a fieldset legend create the gap?
Can I center a fieldset legend?
margin-inline: auto on the legend centers it, and margin-inline-start: auto pushes it to the right edge.Why does my fieldset overflow on mobile?
min-inline-size: min-content, so it stays as wide as its widest child even when the container is narrower. Set min-inline-size: 0 to let it shrink.How do I position the title in the line method?
flex on the ::before and ::after pseudo-elements. flex: 0 0 <width> on ::before and flex: 1 1 0 on ::after puts the title on the left. Swap them for right. Set both to flex: 1 1 0 for center.