How to Modify Fonts in CSS

See CSS: Tips and Tricks for similar articles.
Looking to improve your CSS skills? Check out Webucator’s CSS classes.

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:
    <!DOCTYPE HTML>
    <html>
    <head>
    <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;
    		}
    </style>
    </head>
    <body>
    <div style="font-family: Arial, Helvetica, sans-serif;">
    	Arial, Helvetica, sans-serif
    </div>
    <div style="font-family: 'Times New Roman', Times, serif;">
    	'Times New Roman', Times, serif
    </div>
    <div style="font-family: 'Courier New', Courier, monospace;">
    	font-family: 'Courier New', Courier, monospace
    </div>
    <div style="font-family: Verdana;">
    	Verdana
    </div>
    <div style="font-family: Comic Sans MS;">
    	Comic Sans MS
    </div>
    <div style="font-family: Wingdings;">
    	Wingdings
    </div>
    </body>
    </html>
    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:
    UnitDescription
    pxPixels
    ptPoints
    inInches
    cmCentimeters
    mmMillimeters
    pcPicas
    emEms
    exExs
    %Percentage
    remRems
  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:
    
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Font Sizes</title>
    </head>
    <body>
    <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>
    	</span></span>.
    </div>
    
    <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>
    	</span></span>.
    </div>
    </body>
    </html>
    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.


Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS
  24. How to Use CSS Media Queries in Responsive Design
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS (this article)
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3