How to Modify Fonts in CSS

In the past, we used the HTML <font> tag to change font family, size, and other attributes. CSS makes it much easier. Learn how to modify font family and font size using CSS in the following steps.

  1. The font-family property is used in CSS to specify the font name to apply to an element. You can specify by font name (such as Arial, Helvetica, etc.) or font category (such as cursive, fantasy, monospace, etc.). To specify a font-family of Arial, for example, you use:
    p { font-family: Arial; }
  2. If the Arial font were not found on the end user's computer, the browser would display a default font. If you are concerned that the font name you want to use might not be found on a user's computer, you can provide a list of options, like this:
    p { font-family: Arial, Helvetica; }
    In this case, the browser will first look for Arial. If it doesn't find Arial, it will then look for Helvetica.
  3. To be extra safe, you can specify a couple of specific font family options followed by a font family category, like so:
    p { font-family: Arial, Helvetica, sans-serif; }
    This way, if neither Arial nor Helvetica is found, the browser at least knows to use some sans-serif font.
    Here is some sample code:
    <meta charset="UTF-8">
    <title>Font Family</title>
    <style type="text/css">
    	body { font-size: large; }
    	div {
    	margin: 10px;
    	padding: 10px;
    	border: 1px solid black;
    <div style="font-family: Arial, Helvetica, sans-serif;">
    	Arial, Helvetica, sans-serif
    <div style="font-family: 'Times New Roman', Times, serif;">
    	'Times New Roman', Times, serif
    <div style="font-family: 'Courier New', Courier, monospace;">
    	font-family: 'Courier New', Courier, monospace
    <div style="font-family: Verdana;">
    <div style="font-family: Comic Sans MS;">
    	Comic Sans MS
    <div style="font-family: Wingdings;">
    Which looks like this:
    Font Family
  4. CSS allows us to specify exactly how big or small we want text to appear using font-size. The units you can use are:
    Unit Description
    px Pixels
    pt Points
    in Inches
    cm Centimeters
    mm Millimeters
    pc Picas
    em Ems
    ex Exs
    % Percentage
    rem Rems

  5. In addition, font size can be defined using the following relative terms:
    • xx-large
    • x-large
    • large
    • medium
    • small
    • x-small
    • xx-small
    • smaller
    • larger
    The sizes xx-small to xx-large work similarly to font sizes 1 through 7 in HTML, though they don't match up exactly. The terms "smaller" and "larger" change the font size of an element relative to its parent element's font size. The following code illustrates this:
    <meta charset="UTF-8">
    <title>Font Sizes</title>
    <div id="smaller">
    	This text is <span style="font-size: smaller;">smaller and
    	<span style="font-size: smaller;">even smaller and
    	<span style="font-size: smaller;">even smaller</span>
    <div id="larger">
    	This text is <span style="font-size: larger;">larger and
    	<span style="font-size: larger;">even larger and
    	<span style="font-size: larger;">even larger</span>
    The above code will output the following:
    Font Size

Most experts agree that font size should be defined in relative units (e.g., em, rem, %, etc.) or in terms (e.g., large, small, etc.). This is because absolute font sizes can make pages inaccessible to people who have difficulty seeing. In most browsers, a user can change both the "zoom" of the page and, separately, the default font size. While "zooming" will increase the size of all elements (font included) on the page, changing the default browser font size won't have any effect on fonts whose sizes in CSS are specified using absolute, rather than relative, units.

Unfortunately, there is a downside to using relatively defined font sizes, which is that you have less control over design. This can cause text to wrap when you don't want it to or table cells to expand beyond what you had intended.

Author: Chris Minnick

Chris Minnick, the co-founder of WatzThis?, has overseen the development of hundreds of web and mobile projects for customers from small businesses to some of the world’s largest companies. A prolific writer, Chris has authored and co-authored books and articles on a wide range of Internet-related topics including HTML, CSS, mobile apps, e-commerce, e-business, Web design, XML, and application servers. His published books include Adventures in Coding, JavaScript For Kids For Dummies, Writing Computer Code, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW E-Commerce Designer Certification Bible, and XHTML.

About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.