Fit text
Scales one line of text to fill its parent's width.
Put it in a block-level parent as wide as the line should be, one FitText per line. It sizes itself in the browser after the page loads, so the line is the inherited size until then.
One line
Release notes
import { FitText } from "@/components/ui/fit-text"
export default function FitTextOneLine() {
return (
<div className="w-full max-w-md">
<FitText className="font-medium">Release notes</FitText>
</div>
)
}
Stacked lines
Every task
shipped on
Thursday
import { FitText } from "@/components/ui/fit-text"
export default function FitTextStackedLines() {
return (
<div className="w-full max-w-md">
{["Every task", "shipped on", "Thursday"].map((line) => (
<div key={line}>
<FitText className="leading-none font-medium">{line}</FitText>
</div>
))}
</div>
)
}
Following its container
Invoice 0142
"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { FitText } from "@/components/ui/fit-text"
export default function FitTextFollowingItsContainer() {
const [narrow, setNarrow] = useState(false)
return (
<div className="flex w-full max-w-md flex-col items-start gap-4">
<div className={narrow ? "w-1/2" : "w-full"}>
<FitText className="font-medium">Invoice 0142</FitText>
</div>
<Button variant="outline" aria-pressed={narrow} onClick={() => setNarrow(!narrow)}>
Narrow container
</Button>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/fit-textbunx --bun shadcn@latest add @khaeli/fit-textInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
Accessible name
- It renders a plain
span: the text is read as written, with no role and no name of its own.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/fit-text.json