How to Change Font in HTML: CSS Examples and Fixes

by .

Use the CSS font-family property to change font in HTML. For one paragraph, add a style attribute such as style="font-family: Georgia, serif;". For a page, put the family rule on body in your stylesheet. A list of family names gives the browser alternatives when your first choice is missing.

“Change the font” can mean a few things. If the letters are too small, you want font-size. If they look too thin, you want font-weight. This walkthrough starts with the typeface itself, using a small garden-notes page that needs no downloaded fonts.

How to change font in HTML with serif, sans-serif and monospace font families
One CSS property selects the family. The same words can have quite different letterforms.

How to change font in HTML for one paragraph

Start with this paragraph in an HTML file. There are two sets of quotation marks: double quotes for the style attribute and single quotes for the multiword family name inside it. Times New Roman is one name. The commas separate the alternatives.

<p style="font-family: 'Times New Roman', Times, serif;">
  A short reading note.
</p>

After saving the file, reload its browser tab. If Times New Roman is available, the paragraph requests it. If it is not, Times is next, followed by a generic serif. Nothing in that line installs a font.

What if the paragraph looks unchanged? Two serif faces can be difficult to distinguish at a glance. Temporarily request monospace. If the letters still do not change, check that you saved the right file and refreshed the right tab before investigating installed fonts.

A few words can have their own class without changing the whole paragraph. Here, a span marks the book title while keeping it inside the sentence. Use em or strong for meaningful emphasis; a different typeface alone does not describe that meaning.

<style>
  .book-title { font-family: Georgia, serif; }
</style>
<p>Read <span class="book-title">The Garden Notebook</span> next.</p>

Try a complete page before editing your site

Copy this into index.html. It is a self-contained exercise, so a missing stylesheet or webfont cannot get in the way. The button has no action; it is there to show how a form control can share the surrounding typography.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Garden notes</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      font-size: 1rem;
      line-height: 1.6;
      color: #172331;
      background: #fff;
      max-width: 65ch;
      margin: 2rem auto;
      padding: 0 1rem;
    }
    h1 { font-family: Georgia, serif; }
    .sample { font-family: monospace; }
    button, input, select, textarea { font: inherit; }
  </style>
</head>
<body>
  <h1>Garden notes</h1>
  <p>The first seedlings are ready to move outside.</p>
  <p class="sample">Bed A: peas, beans, lettuce.</p>
  <button type="button">Example button</button>
</body>
</html>

There are three visible choices here. The body requests the platform’s interface font, the heading requests Georgia, and the planting note requests monospace. The ordinary paragraph inherits its family from body. The heading does not use that inherited choice because it has a declaration of its own.

Try changing only the body family. Some text will respond while the heading and planting note keep their direct rules. That result is useful: it shows why a site-wide font change can leave isolated headings or cards looking different.

The last rule gives form controls the surrounding font settings explicitly. Browsers can otherwise apply their own control styles. Once the example behaves as expected, move its rules into a shared stylesheet using our CSS-to-HTML linking guide. To change alignment as well, see the text-centering examples. Family and alignment do different jobs.

For a short exercise, change the body family to Georgia and the h1 family to monospace. Predict the result before reloading. The ordinary paragraph and button should follow body; the heading follows its own rule, and the planting note keeps its class rule. This is a useful way to separate inheritance from the declarations applied directly to an element.

Give the browser a sensible second choice

The list after font-family is called a font stack. Order matters. Georgia, serif asks for Georgia before a generic serif; it does not mean that the browser blends two fonts. A later family can also supply an individual character that an earlier font lacks.

Generic names stay unquoted. Quoting a multiword name helps keep the intended family clear. Separate the entries with commas, rather than writing a sentence about the kind of lettering you want.

Font stack tries Meadow, then Georgia, then a generic serif fallback

Fallbacks cover missing families and missing characters; they do not install the first font.
Three useful starting points
Family value Result to look for
system-ui, sans-serif The platform’s interface style.
Georgia, serif Georgia where available, otherwise a serif.
monospace Even-width characters, useful for code or a short sample.

Penn State’s web-font guidance recommends including backup families. Choose alternatives that preserve the purpose of the text. A decorative script is a poor substitute for the reading face in a long article, even if both happen to be installed.

Read more than a heading before you decide. Check punctuation, accents and easily confused letters at body size. Serif and sans-serif are broad categories; neither label guarantees that a particular face will suit your content.

Keep size, weight and family separate while you experiment

These declarations leave Georgia in place and set the other properties explicitly. They are easier to adjust one at a time than a compact shorthand while you are learning.

.intro {
  font-family: Georgia, serif;
  font-size: 1.25rem;
  font-weight: 400;
  line-height: 1.6;
}
.intro strong { font-weight: 700; }

To apply the intro rule, give a paragraph the class name intro. Writing .intro in the stylesheet does not select every opening paragraph automatically. The class attribute in your HTML is what connects that paragraph to the rule. If the change reaches no text at all, this connection is one of the first things to check.

At a 16-pixel root size, 1.25rem equals 20 pixels. A different root size changes that calculation. Leave room for the text to wrap and grow; a fixed-height box can clip enlarged text.

The shorter font property is useful too, but omitted settings can reset earlier values. If italics or a weight disappear unexpectedly, inspect later font declarations before adding more overrides. Keep a small set of heading and body rules rather than a new size for every paragraph.

Font choices can differ between print and screens. For example, UCLA’s typography guidance distinguishes its print face from Helvetica, Arial and Roboto in digital use. Your site’s choices need to work in the medium where readers will encounter them.

A custom font needs a file as well as a name

Suppose you want a face called Meadow. Simply adding that name to a stack will not download it. The following example uses @font-face to register an alias and point it at a regular WOFF2 file. You must supply a suitable licensed file yourself; this guide does not include one.

@font-face {
  font-family: "Meadow";
  src: url("../fonts/meadow-regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
.reading {
  font-family: "Meadow", Georgia, serif;
}

The assumed layout has styles/styles.css beside a fonts directory containing meadow-regular.woff2. The URL starts at the CSS file’s location. Its two dots go up one directory before entering fonts.

A stylesheet in the styles folder uses ../fonts/ to reach the sibling fonts folder

Resolve the font path from the external stylesheet, not from the HTML page.

The alias and the filename serve different purposes. In this example, the family declared inside @font-face is Meadow, so the reading class requests Meadow. The URL points to the actual file. Renaming that file in a folder without updating the URL breaks the request, even though the family alias in the CSS still looks correct.

With swap, fallback text can appear while the font downloads. When the new face arrives, different character widths can move line breaks. Check the fallback layout as carefully as the preferred layout.

This rule describes a regular face at weight 400. Register a separate static bold file at its appropriate weight, such as 700, when the design needs it. A regular static file is not a whole weight range. Variable-font descriptors should match the range the file actually supports.

Before uploading a font, check its web licence. The University of Texas typography page lists desktop access separately from additional web licences. Follow the terms supplied with your chosen file; a desktop download alone does not establish permission to self-host it.

Find the winning rule before changing another one

Inspect the exact text that looks wrong. The matched rules show where its declarations came from; computed settings show the resulting values. A crossed-out declaration lost to another rule. Rendered-font information answers a different question: which face actually supplied the letters?

What to check next
Symptom Useful check
No visible change Confirm the stylesheet loaded and the selector matches.
Only one element differs Look for its own family rule or inline style.
Custom face falls back Inspect the font request, path and family alias.
Bold looks wrong Compare the requested weight with the loaded face.
Old styling persists Inspect the stylesheet response for your saved changes.

A direct rule on a child takes precedence over an inherited family from its parent. Making the parent selector more specific does not change that relationship. This is a common reason for a heading to resist an otherwise successful body-font change.

For a file on another origin, inspect cross-origin permission and any content security policy restrictions. Read the actual network response and console message. A blocked resource needs a server or policy fix, not another spelling of its family name.

A successful stylesheet request and a successful font request are separate checks. Look for the font file itself in the network panel, not just styles.css. A wrong path may return an error document instead of a usable font. Inspect the response and any decoding error before replacing the family stack with a longer list of guesses.

On WordPress, begin with the theme’s typography controls or supported CSS workflow. An editor may remove a style block when saving. Compare the saved markup and stylesheet with what reached the browser. Adding !important repeatedly cannot restore a missing rule.

Finally, narrow the window and enlarge the text. Check the heading, controls and long words. Block a custom font request to see whether the page remains usable without it. Remove temporary debugging rules when you are done.

Questions about changing fonts

Do I need a separate CSS file?

No. A style block or inline declaration can keep CSS in the HTML document. A shared file is convenient when several pages use the same typography.

Should I use the font tag?

Use CSS for new work. When updating old font elements, preserve the surrounding paragraphs, headings and emphasis while moving their presentation into CSS.

Will every reader see exactly the same face?

No. Installed fonts, download failures, character support and user preferences can affect the result. Provide alternatives and check that the text remains readable.

Check your understanding

Which property changes the typeface?

Font-family. Font-size changes the size, which is a separate choice.

Does naming Meadow download a font?

No. The name needs an available installed family or a matching font-face definition with a usable resource.

Where does an external stylesheet’s font URL start?

At the stylesheet’s location. Start in the styles directory in this example, then follow ../fonts/ to its sibling.

Comments are closed.