CSS Text Styling: Fonts, Size, Weight, Underline, Alignment
Style text in CSS with a handful of properties: font-family picks the typeface, font-size sets the size (rem is best), font-weight: 700 makes it bold, text-decoration adds an underline, and text-align: center centres it. line-height and letter-spacing control spacing. Change any of them in the playground below and copy the CSS.
| Goal | CSS | Tailwind |
|---|---|---|
| Bold | font-weight: 700 | font-bold |
| Italic | font-style: italic | italic |
| Underline | text-decoration: underline | underline |
| Centre text | text-align: center | text-center |
| Bigger text | font-size: 1.25rem | text-xl |
| Looser lines | line-height: 1.6 | leading-relaxed (1.625) |
Updated
Instead of six separate reference pages, this one playground covers font, size, weight, style, underline details, alignment, line height and letter spacing together, writes the CSS and the Tailwind classes as you change them, and adds tested recipes for centring text, font stacks and loading web fonts.
Font stacks you can copy
- System sans:
font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; - Humanist sans:
font-family: "Segoe UI", Candara, "Trebuchet MS", Tahoma, sans-serif; - Classic serif:
font-family: Georgia, "Times New Roman", Times, serif; - Book serif:
font-family: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", serif; - Monospace:
font-family: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; - Rounded:
font-family: ui-rounded, "Arial Rounded MT Bold", "Trebuchet MS", sans-serif;
Centring text horizontally and vertically
Horizontal: text-align
text-align centres the lines inside the block. To centre the block itself, give it a width and margin-inline: auto.
<p style="text-align: center">Free delivery on orders over $40</p>
<p style="max-width: 20ch; margin-inline: auto; background: #eef2ff">A narrow block centred on the page</p>Both ways: grid with place-items
Two lines of CSS on the container centre any content in both directions, whatever its size.
<div style="display: grid; place-items: center; height: 140px; background: #0f172a; color: #fff; border-radius: 10px">
<p style="margin: 0">Centred both ways</p>
</div>Underlines that look designed
Colour, thickness, offset and style
The decoration properties change the underline without touching the text colour. text-decoration-skip-ink keeps lines from cutting through descenders.
<style>
a { color: #111; text-decoration: underline; text-decoration-color: #4F98FB;
text-decoration-thickness: 2px; text-underline-offset: 4px }
.wavy { text-decoration: underline wavy #dc2626 }
.strike { text-decoration: line-through }
</style>
<p><a href="#">Read the shipping policy</a></p>
<p><span class="wavy">Speling</span> mistakes and <span class="strike">$60</span> $45 prices</p>Loading a font with @font-face or Google Fonts
Self-hosted font with @font-face
Point src at your WOFF2 file. One block per weight and style you actually use.
@font-face {
font-family: "Fieldwork";
src: url("/fonts/fieldwork-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}
@font-face {
font-family: "Fieldwork";
src: url("/fonts/fieldwork-bold.woff2") format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}
body { font-family: "Fieldwork", system-ui, sans-serif; }Google Fonts link
Request only the weights you use. display=swap sets font-display for you.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap" rel="stylesheet">
<style>body { font-family: "Inter", system-ui, sans-serif; }</style>Readable defaults to start from
A small, sensible text baseline
Body size, line height and measure set once; headings tighter. Adjust the colours and sizes to your design.
<style>
body { font: 1.0625rem/1.65 system-ui, sans-serif; color: #1f2937 }
h2 { font-size: clamp(1.5rem, 1.1rem + 2vw, 2.25rem); line-height: 1.15; letter-spacing: -0.01em; margin: 0 0 .5rem }
p { max-width: 65ch; margin: 0 0 1rem }
small { font-size: .875rem; color: #4b5563 }
</style>
<h2>Plan your garden for spring</h2>
<p>Start with the beds that get the most sun. Tomatoes, peppers and herbs need six hours or more, while leafy greens cope with partial shade.</p>
<small>Last checked by our team in March.</small>Tailwind equivalents
| CSS | Tailwind |
|---|---|
| font-size: 0.875rem | text-sm |
| font-size: 1rem | text-base |
| font-size: 1.125rem | text-lg |
| font-size: 1.5rem | text-2xl |
| font-weight: 400 / 500 / 600 / 700 | font-normal / font-medium / font-semibold / font-bold |
| font-style: italic | italic |
| text-decoration: underline | underline |
| text-decoration-thickness: 2px | decoration-2 |
| text-underline-offset: 4px | underline-offset-4 |
| text-align: center / right / justify | text-center / text-right / text-justify |
| line-height: 1.25 / 1.5 / 1.625 | leading-tight / leading-normal / leading-relaxed |
| letter-spacing: -0.025em / 0.025em | tracking-tight / tracking-wide |
| text-transform: uppercase | uppercase |
| font-family: ui-monospace, … | font-mono |
Size units: px, rem and clamp()
Pixels give an exact size that ignores the reader's preferred text size. rem is relative to the root font size, 16px by default, so 1.125rem is 18px for most people and grows for readers who set a larger default.
clamp(min, preferred, max) is for sizes that should scale with the window. clamp(1.75rem, 1.2rem + 2.5vw, 3rem) is never smaller than 28px or larger than 48px and moves smoothly between. Keep a rem part in the middle value so browser zoom and text settings still have an effect.
Readable defaults
Most reading problems come from three settings: text that is too small, lines that are too long and lines packed too tightly. A max-width of about 65ch on paragraphs, a font size of at least 1rem and a line height near 1.6 fix most of them before any styling.
Large headings want the opposite: tighter line height, around 1.1 to 1.2, and often a slightly negative letter-spacing. Small uppercase labels read better with a little positive letter-spacing.
Loading web fonts without slowing the page
Every web font is a download that can delay text. font-display: swap shows the fallback font straight away and swaps when the web font arrives, so readers never stare at invisible text. Self-hosting WOFF2 files avoids a connection to another domain.
If you use Google Fonts, request only the families and weights you need in one link, and add the preconnect hints it suggests. Choosing a fallback in the stack with similar proportions reduces the jump when the font swaps in.
How it works
- Type your own sentence into the playground or keep the sample text.
- Change the font, size unit, weight, decoration and spacing controls. The preview and the CSS update immediately.
- Copy the CSS block, or the Tailwind classes for the same settings.
- Use the recipes further down for centring, font stacks and @font-face.
Tips
- Body text reads best around 16 to 18 pixels, with a line height of 1.5 to 1.7 and lines of roughly 60 to 75 characters.
- Set font sizes in rem so they follow the reader's browser setting. Use clamp() for headings that should grow with the screen.
- Only load the font weights you use. Each extra weight of a web font is another file to download.
- Avoid underlining text that is not a link. Readers expect underlined words to be clickable.
Frequently asked questions
How do I make text bold in CSS?
Use font-weight: bold or a number such as 600 or 700. Numbers between 100 and 900 pick from the weights the font provides. If the font only has a regular weight, the browser fakes bold by thickening the letters, which looks heavier and less crisp than a real bold file.
How do I centre text vertically?
text-align only centres horizontally. To centre vertically inside a box, make the box a flex or grid container: display: grid; place-items: center; centres the text both ways in one line. For a single line in a fixed-height bar, line-height equal to the bar height also works.
How do I change the underline colour or thickness?
Use text-decoration-color, text-decoration-thickness and text-underline-offset. For example text-decoration: underline; text-decoration-color: #4F98FB; text-decoration-thickness: 2px; text-underline-offset: 4px; gives a thicker blue underline with space below the letters.
Should line-height have a unit?
Usually not. A unitless value like 1.5 is multiplied by each element's own font size, so headings and small print inside the block keep sensible spacing. A fixed value like 24px is passed down as-is and can cramp larger text.
What is a font stack?
A comma-separated list of fonts in font-family. The browser uses the first one available and moves down the list if it is missing, ending with a generic family such as sans-serif or serif that always exists.
Related: Font pairing tool, PX to REM converter, HTML spaces and line breaks, Flexbox and grid cheat sheet, Readability checker.
Start a 7-day free trial or see all free tools. On We.Inc you get a 7-day free trial, not a free plan: card needed, $0 today, cancel before it ends and you pay nothing. AI building is on paid plans from $20 a month.
More in Guides
- CSS Transforms: Rotate, Scale, Translate and Skew
- CSS Transitions and Keyframes, With 20 Copyable Examples
- CSS Variables: From var() to Themes and @property
- CSS z-index and Stacking Contexts, Taken Apart in 3D
- How to Add Dark Mode to a Website (CSS, Tailwind, Next.js)
- Data Fetching in React and Next.js, Compared on One App
- Data URLs: data:text/html;base64 and Friends, Explained
- Debounce vs Throttle, Visualised
Product
Who It's For
- For Agencies
- For Freelancers
- For Hosting Providers
- For SaaS Platforms
- For Startups
- For Financial Services
- For Healthcare
Features
Resources
- Blog
- Help Center
- Free Business Tools
- Free UI Components
- Website Templates
- Template Collections
- Website Examples
- Guides
- Comparisons
- Alternatives
- Best Lists
- Reviews
- Pricing Guides
- Use Cases
- Glossary & Guides
- Integrations
- Developers
- Benchmark
- Windows App