Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

CSS Font


CSS Font property is used to control the look of texts. By the use of CSS font property you can change the text size, color, style and more. You have already studied how to make text bold or underlined. Here, you will also know how to resize your font using percentage.

These are some important font attributes:

  1. CSS Font color: This property is used to change the color of the text. (standalone attribute)
  2. CSS Font family: This property is used to change the face of the font.
  3. CSS Font size: This property is used to increase or decrease the size of the font.
  4. CSS Font style: This property is used to make the font bold, italic or oblique.
  5. CSS Font variant: This property creates a small-caps effect.
  6. CSS Font weight: This property is used to increase or decrease the boldness and lightness of the font.

1) CSS Font Color

CSS font color is a standalone attribute in CSS although it seems that it is a part of CSS fonts. It is used to change the color of the text.

There are three different formats to define a color:

  • By a color name
  • By hexadecimal value
  • By RGB

In the above example, we have defined all these formats.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6.     font-size: 100%;
  7. }
  8. h1 { color: red; }
  9. h2 { color: #9000A1; }
  10. p { color:rgb(0, 220, 98); }
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>This is heading 1</h1>
  16. <h2>This is heading 2</h2>
  17. <p>This is a paragraph.</p>
  18. </body>
  19. </html>

Output:

This is heading 1

This is heading 2

This is a paragraph.


2) CSS Font Family

CSS font family can be divided in two types:

  • Generic family: It includes Serif, Sans-serif, and Monospace.
  • Font family: It specifies the font family name like Arial, New Times Roman etc.

Serif: Serif fonts include small lines at the end of characters. Example of serif: Times new roman, Georgia etc.

Sans-serif: A sans-serif font doesn’t include the small lines at the end of characters. Example of Sans-serif: Arial, Verdana etc.

fontserif

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. font-size: 100%;
  7. }
  8. h1 { font-family: sans-serif; }
  9. h2 { font-family: serif; }
  10. p { font-family: monospace; }
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>This heading is shown in sans-serif.</h1>
  16. <h2>This heading is shown in serif.</h2>
  17. <p>This paragraph is written in monospace.</p>
  18. </body>
  19. </html>

Output:

This heading is shown in sans-serif.

This heading is shown in serif.

This paragraph is written in monospace.


3) CSS Font Size

CSS font size property is used to change the size of the font.

These are the possible values that can be used to set the font size:

Font Size Value Description
xx-small used to display the extremely small text size.
x-small used to display the extra small text size.
small used to display small text size.
medium used to display medium text size.
large used to display large text size.
x-large used to display extra large text size.
xx-large used to display extremely large text size.
smaller used to display comparatively smaller text size.
larger used to display comparatively larger text size.
size in pixels or % used to set value in percentage or in pixels.
  1. <html>
  2. <head>
  3. <title>Practice CSS font-size property</title>
  4. </head>
  5. <body>
  6. <p style=“font-size:xx-small;”>  This font size is extremely small.</p>
  7. <p style=“font-size:x-small;”>  This font size is extra small</p>
  8. <p style=“font-size:small;”>  This font size is small</p>
  9. <p style=“font-size:medium;”>  This font size is medium. </p>
  10. <p style=“font-size:large;”>  This font size is large. </p>
  11. <p style=“font-size:x-large;”>  This font size is extra large. </p>
  12. <p style=“font-size:xx-large;”>  This font size is extremely large. </p>
  13. <p style=“font-size:smaller;”>  This font size is smaller. </p>
  14. <p style=“font-size:larger;”>  This font size is larger. </p>
  15. <p style=“font-size:200%;”>  This font size is set on 200%. </p>
  16. <p style=“font-size:20px;”>  This font size is 20 pixels.  </p>
  17. </body>
  18. </html>

Output:

This font size is extremely small.

This font size is extra small

This font size is small

This font size is medium.

This font size is large.

This font size is extra large.

This font size is extremely large.

This font size is smaller.

This font size is larger.

This font size is set on 200%.

This font size is 20 pixels.


4) CSS Font Style

CSS Font style property defines what type of font you want to display. It may be italic, oblique, or normal.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. body {
  6. font-size: 100%;
  7. }
  8. h2 { font-style: italic; }
  9. h3 { font-style: oblique; }
  10. h4 { font-style: normal; }
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h2>This heading is shown in italic font.</h2>
  16. <h3>This heading is shown in oblique font.</h3>
  17. <h4>This heading is shown in normal font.</h4>
  18. </body>
  19. </html>

Output:

This heading is shown in italic font.

This heading is shown in oblique font.

This heading is shown in normal font.


5) CSS Font Variant

CSS font variant property specifies how to set font variant of an element. It may be normal and small-caps.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p { font-variant: small-caps; }
  6. h3 { font-variant: normal; }
  7. </style>
  8. </head>
  9. <body>
  10. <h3>This heading is shown in normal font.</h3>
  11. <p>This paragraph is shown in small font.</p>
  12. </body>
  13. </html>

Output:

This heading is shown in normal font.

This paragraph is shown in small font.


6) CSS Font Weight

CSS font weight property defines the weight of the font and specify that how bold a font is. The possible values of font weight may be normal, bold, bolder, lighter or number (100, 200….. upto 900).

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <p style=“font-weight:bold;”>This font is bold.</p>
  5. <p style=“font-weight:bolder;”>This font is bolder.</p>
  6. <p style=“font-weight:lighter;”>This font is lighter.</p>
  7. <p style=“font-weight:100;”>This font is 100 weight.</p>
  8. <p style=“font-weight:200;”>This font is 200 weight.</p>
  9. <p style=“font-weight:300;”>This font is 300 weight.</p>
  10. <p style=“font-weight:400;”>This font is 400 weight.</p>
  11. <p style=“font-weight:500;”>This font is 500 weight.</p>
  12. <p style=“font-weight:600;”>This font is 600 weight.</p>
  13. <p style=“font-weight:700;”>This font is 700 weight.</p>
  14. <p style=“font-weight:800;”>This font is 800 weight.</p>
  15. <p style=“font-weight:900;”>This font is 900 weight.</p>
  16. </body>
  17. </html>

Output:

This font is bold.

This font is bolder.

This font is lighter.

This font is 100 weight.

This font is 200 weight.

This font is 300 weight.

This font is 400 weight.

This font is 500 weight.

This font is 600 weight.

This font is 700 weight.

This font is 800 weight.

This font is 900 weight.

Comments

comments