The quick answer
Like colors, fonts are set with CSS. For a quick change, put the rules in a style attribute on the
element. To reuse them, give the element a class and write the rules once in a <style> block or a
stylesheet file.
The examples run in a locked box: scripts are off, and your edits go away when you reload. To keep one, copy it into the HTML Editor. They also use the fonts on your own device, so Georgia only shows up as Georgia if your device has it.
Change the font with font-family
font-family takes a list of fonts, separated by commas. The browser uses the first one it can find on
the device and skips the rest. That’s why the list should end with a generic family: a word that
means “any font of this kind”, which always exists.
| Generic family | What you get |
|---|---|
serif | Letters with small strokes at the ends, like Times New Roman or Georgia. Feels like a book or a newspaper. |
sans-serif | Letters without those strokes, like Arial or Helvetica. The most common choice for screens. |
monospace | Every letter takes the same width, like Courier New. Good for code and for numbers that line up. |
system-ui | The font your device uses for its own menus and buttons, so the page feels at home there. |
cursive, fantasy | Handwriting and decorative fonts. Every device picks something different, so you can’t count on the look. |
A word on quotes. Put font names with spaces in quotes, like "Times New Roman". Inside a
style="..." attribute, use single quotes so you don’t end the attribute by accident. And never quote a
generic family: 'sans-serif' in quotes is read as the name of a font called “sans-serif”, not as the
generic family, so you may get the browser’s default font instead.
Web-safe fonts and system fonts
“Web-safe” fonts are the ones that come with Windows and macOS (and iPhones and iPads), so they work there without a download. Android phones and most Linux computers don’t have them, and swap in their own font of the same generic family. That’s fine, as long as your list ends with one.
| Font | A good stack | Notes |
|---|---|---|
| Arial / Helvetica | Arial, Helvetica, sans-serif | Plain and very readable. Arial was made to match Helvetica’s letter widths, and Macs have both. |
| Verdana | Verdana, Geneva, sans-serif | Wide letters made for screens, easy to read at small sizes. |
| Georgia | Georgia, serif | A serif font made for screens. Good for long articles. |
| Times New Roman | "Times New Roman", Times, serif | The classic newspaper look. It looks a bit small next to other fonts at the same size. |
| Courier New | "Courier New", Courier, monospace | The typewriter look. Thin, so many people prefer a newer monospace font for code. |
| system-ui | system-ui, sans-serif | Segoe UI on Windows, San Francisco on Apple devices, Roboto on Android. Nothing to download, and the page matches the device’s own menus. |
In the system stack above, -apple-system is for older Safari versions that don’t know
system-ui yet. ui-monospace is the same idea for code, but not every browser supports it,
so the list goes on with Menlo (Mac), Consolas (Windows) and monospace.
Web fonts: Google Fonts and @font-face
A font that isn’t on people’s devices has to be downloaded. The easiest way is
Google Fonts: pick a font on fonts.google.com, copy the <link> tags it gives
you into your page’s <head>, and use the font’s name in font-family.
<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=Lobster&display=swap" rel="stylesheet">
<style>
h2 {
margin: 0 0 4px;
font-family: "Lobster", Georgia, serif;
font-size: 34px;
font-weight: 400;
}
</style>
<h2>Fresh Bread Daily</h2>
<p>The heading uses Lobster from Google Fonts.</p> This one is plain code, not a live preview, because a live preview would load the font from Google as soon as you scrolled here. Paste it into the HTML Editor to see Lobster.
On your own site, each visitor’s browser gets the font from Google. display=swap shows a fallback font
right away and switches when the web font arrives, so the text never disappears while it loads. Only ask for the
weights you use: each one adds a rule to the stylesheet, and for many fonts it’s one more file to download.
You can also host the font files yourself. Put a .woff2 file (the smallest format, and every current browser supports it) on
your site and describe it with @font-face. The font-family inside it is a name you choose,
and you then use that name like any other font:
-
The preview shows Georgia on purpose. There’s no
my-font.woff2next to this example, so the browser moves on to the next font in the list. Your visitors get the same thing when the file is missing, the path has a typo, or the download is slow. -
You need one
@font-facerule per file. With a regular and a bold file, write two rules with the samefont-familyname, one withfont-weight: 400and one withfont-weight: 700. The browser then picks the right file for<strong>and headings. -
Fonts on another domain need permission. A browser only uses a font file from another site if
that site sends an
Access-Control-Allow-Originheader. Fonts on your own site don’t have this problem. - Mind the license. Google Fonts are free to use and to host yourself, but that nice font that came with your design app usually isn’t yours to put on a website.
Font size in HTML: px, rem, em, % and keywords
Browsers start at 16px, so 1rem, 1em, 100% and medium all mean
16px until something changes that. Below, the same 20px is written four ways, plus one keyword for comparison:
| Unit | Relative to | Use it for |
|---|---|---|
px | Nothing, it’s fixed | Exact sizes, like a small label. Zooming still works, but a bigger text size set in the browser’s settings doesn’t change px text. |
rem | The root (html) font size, 16px by default |
Most text. It follows the reader’s own text size setting, and 1.25rem is 1.25rem everywhere on
the page.
|
em | The parent’s font size | Things that should grow with the text around them, like an icon in a button or padding. |
% | The parent’s font size | The same as em for font-size: 120% is 1.2em. |
| Keywords | The browser’s default | small (13px), medium (16px), large (18px), x-large (24px),
and smaller / larger for one step from the parent.
|
Watch out with em, because it multiplies. In the second example, each list sits inside the one before it, so
1.2em becomes 1.2 × 1.2 × 1.2, and the deepest item ends up about 28px. rem always looks
back at the root, so every level stays at 19.2px. When text grows or shrinks and you don’t know why, look for an
em or % on a parent.
Many people set a bigger default text size in their browser because small text is hard for them to read. Sizes
in rem follow that setting. For the same reason, don’t set html { font-size: 16px; }:
it overrides their choice. Leave it out, or use a percentage like 100%.
A font size that grows with the screen
A 40px heading that fits a laptop can wrap onto four lines on a phone. clamp() lets the size follow the
screen width, within limits you set. It takes three values: the smallest size, the size you’d like, and the largest size. The middle value uses
vw (1vw is 1% of the window’s width), so the text grows with the screen but never leaves the range.
Here the heading is never smaller than 1.25rem (20px) and never bigger than 2.5rem (40px). Keep a rem
part in the middle value, like 0.75rem + 3vw. Text sized only in vw doesn’t get bigger
when someone zooms in, which makes it hard to read for people who need larger text.
Line height and font size
line-height sets the height of each line of text, so it decides how much space sits between the
lines. Browsers use about 1.2 by default, which is tight for paragraphs. Around 1.5 to 1.7 is easier to read, and
WCAG (the web accessibility guidelines) suggests at least 1.5 inside paragraphs.
Write line-height without a unit, like 1.5. A plain number means “1.5 times this element’s own font
size”, and children work it out again for their own size. With 20px (or 1.5em), children
get a fixed height instead, so bigger text inside squashes together, as in the second example.
One font for the whole page, or for one element
font-family and font-size are inherited, just like color. Set them once on
body, and every element on the page uses them. Then add rules only for the parts that differ, like
headings.
-
Form fields don’t inherit fonts. Inputs, buttons, selects and text areas use the browser’s own
font and size. Add
button, input, select, textarea { font: inherit; }to make them match the rest of the page. -
Headings come with their own sizes: browsers make
<h1>2em,<h2>1.5em and so on, and bold. Change them withfont-sizeandfont-weightonh1,h2... More about weights in the bold text tutorial. -
For one word or a price, a
<span>with a class does it, as in the second example.
Font color
Strictly speaking, text color isn’t a font setting. It’s the color property, but you’ll usually write
it right next to the font rules:
Color names, hex, rgb and hsl, links and contrast are all covered in the text color tutorial. To find a color code, use the HTML Color Picker.
The font shorthand
font sets several font properties at once. The order matters: style and weight first (both optional),
then the size, an optional /line-height right after it, and the font family last. The size and the
family are required, or the browser ignores the whole line.
One catch: font sets every part you leave out back to its starting value. In the
second example, font: 16px Georgia, serif wipes out the bold weight and the tall line-height that the
p rule asked for. If you only want to change the size or the family, use font-size or
font-family on their own.
Why not <font face size color>?
Before CSS, fonts were set with the <font> tag: face for the font,
size from 1 to 7 and color. It has been obsolete since HTML5. Browsers still show it so
old pages don’t break, but the HTML Validator flags it, and you’d have to edit every
tag by hand to change the look. A class does the same job, with the rules in one place:
| <font> | CSS instead |
|---|---|
face="Arial" | font-family: Arial, sans-serif; |
size="1" to size="7" | x-small (10px), small (13px), medium (16px, the default 3),
large (18px), x-large (24px), xx-large (32px),
xxx-large (48px)
|
color="red" | color: red; |
The same goes for <big>, which is obsolete too: use font-size.
<small> is still fine, but it means “small print”, like a copyright line, not just smaller
text.
Questions people ask
How do I change the font in HTML?
Use the CSS font-family property. Right in the HTML: <p style="font-family: Georgia, serif;">. In a stylesheet: give the element a class and add .intro { font-family: Georgia, serif; }. List a few fonts separated by commas and end with a generic family like serif or sans-serif, so there is always something to fall back on.
How do I change the font size in HTML?
Use the CSS font-size property, for example <p style="font-size: 20px;"> or .intro { font-size: 1.25rem; } in a stylesheet. Browsers start at 16px, so 1.25rem is 20px by default. rem is a good unit for most text, because it follows the text size people choose in their browser settings.
What is the difference between px, em and rem?
px is a fixed size. rem is relative to the root (html) font size, 16px by default, so 1.5rem is 24px anywhere on the page. em is relative to the parent element, so it multiplies when you nest: 1.2em inside 1.2em is 1.44 times the starting size. Use rem for most text and em for things that should grow with the text around them, like padding in a button.
Which fonts are safe to use on every computer?
Arial, Verdana, Georgia, Times New Roman and Courier New come with Windows and macOS (Macs also have Helvetica), so they are called web-safe fonts. Android phones and most Linux computers don’t have them and use a similar font instead. That’s why a font list should end with a generic family, for example Georgia, "Times New Roman", serif. system-ui is another safe choice: it uses each device’s own font.
How do I use a Google Font on my website?
Pick a font on fonts.google.com, copy the <link> tags it gives you into the <head> of your page, then use the name in your CSS, for example font-family: "Lobster", Georgia, serif;. Keep display=swap in the link, so visitors see a fallback font while the web font downloads. You can also download the font files and host them yourself with @font-face.
How do I change the font color in HTML?
Text color is the CSS color property, for example <p style="color: #1d4ed8;"> or .note { color: #1d4ed8; }. It works with color names, hex, rgb() and hsl(). The <font color="..."> tag still shows in browsers, but it is obsolete, so use CSS instead.
How do I set one font for the whole page?
Set it on body: body { font-family: system-ui, sans-serif; font-size: 1.0625rem; line-height: 1.6; }. Fonts are inherited, so every element takes them unless it has its own rule. Form fields are the exception: add button, input, select, textarea { font: inherit; } so they match too.
Why isn’t my font showing?
The browser only uses a font it can find. Check the spelling of the name, put names with spaces in quotes, and make sure the font is installed on the device or loaded as a web font. For @font-face, check the file path, and remember that a font file on another domain only works if that site allows it. Also check that a more specific rule, or a font shorthand later on, isn’t replacing your font-family.
Can I still use the <font> tag?
Browsers still show <font face="Arial" size="5" color="red">, but the tag has been obsolete since HTML5 and validators flag it. Use CSS instead: font-family for face, font-size for size (size 5 is x-large, 24px), and color for color. A class keeps all of that in one place.
Keep going
Tools that help
- HTML Editor Paste any example and keep building on it.
- CSS Formatter Tidy up your font rules and the rest of your CSS.
- HTML Color Picker Pick a text color and copy it as HEX, RGB or HSL.
- Color Contrast Checker Check that your text is easy to read.
More tutorials
- How to Center Text, a Div or an Image in HTML & CSS
- How to Add a Line Break in HTML
- How to Add Spaces in HTML
- How to Change Text Color in HTML
- How to Change Background Color in HTML
- How to Add an Image in HTML
- What Is a URL? Meaning, Parts and Examples
- How to Redirect a Page in HTML
- How to Get an Image URL
- Canonical URL
- What Is HTML? What It Stands For and How It Works
- How to Create, Open and Run an HTML File
- How to Link CSS to HTML
- How to See the HTML Code of a Website
- How to Link JavaScript to HTML with the script Tag
- HTML hr Tag
- HTML Comments
- How to Make Text Bold in HTML
- All tutorials