CSS font properties define the font family, boldness, size, and text style
CSS Font Family
In CSS, there are two types of font family names:
- generic family - a family group of fonts with the same look (like "Serif" or "Monospace")
- Font family - a specific font family ( such as "Times New Roman" or "Arial" )
| Generic Family | Font Family | Description |
|---|---|---|
| Serif | Times New Roman Georgia | Serif fonts have small lines at the end on some characters |
| Sans-serif | Arial Verdana | "Sans" means without - these fonts do not have a line at the end of the character |
| Monospace | Courier New Lucida Console | All monospace characters have the same width |
Font Family
Text font family made by the font-family property.
Font-family property should hold several font names as a "fallback". If your browser does not support the first font, it tries the next font.
Start with the font you want, and end with a generic family, to let the browser pick a similar font in the generic family, if no other fonts are available.
Note: If the name of the font family is more than one word, it must be in quotation marks, like font-family: "Times New Roman"
More than one font family is specified in a comma-separated list:
example :
p {font-family: "Times New Roman", Times, serif;}
Font Style
Font-style property is widely used to determine the italic text.
This property has three values:
- Normal - text displayed normally
- oblique - The text is shown in italics
- slant - this text "leaning" (oblique is very similar to the oblique, but less supported)
example :
p.normal {font-style: normal;}
p.italic {font-style: italic;}
p.oblique {font-style: italic;}
Font Size
Font-size property sets the text size.
Being able to adjust the Font size is important in web design. However, you should not use font size adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use proper HTML tags, such as :
<h1> – <h6> untuk heading dan paragraf <p>
Nilai font-size bisa menjadi ukuran absolut, atau relatif
- Absolute Size: Set text to a certain size Do not allow users to change the text size in all browsers (bad for accessibility reasons) Absolute size is useful when the physical size of the output is known
- Relative Size: Set the size relative to the surrounding elements Allows the user to change the text size in the browser If you do not set the font size, the standard size for normal text, such as paragraphs, is 16px (16px = 1em)
Set the Font Size to Pixels
Set the text size with pixels, giving you full control over the text size:
example :
h1 {font-size: 40px;}
h2 {font-size: 30px;}
p {font-size: 14px;}
Note: The example above allows Firefox, Chrome, and Safari to resize the text, but not Internet Explorer.
Set the Font Size to Em
To avoid problems with the size of Internet Explorer, many developers use em instead of pixels.
Em size unit is recommended by W3C.
1em is equal to the current font size. The default text size in browsers is 16px. Thus, the default size of 1em is 16px.
The size of the pixel can be calculated for them by using this formula: pixels / 16 = em
example :
h1 {font-size: 2.5em;} / * 40px/16 = 2.5em * /Note: In the above example, the size of the text in them is the same as the previous example in pixels. However, with their size, it is possible to adjust the text size in all browsers.
h2 {font-size: 1.875em;} / * 30px/16 = 1.875em * /
p {font-size: 0.875em;} / * 14px/16 = 0.875em * /
Unfortunately, there is still a problem with IE. When changing the size of the text, to be larger than it should be if made larger, and smaller than it should be when made smaller.
Use combination Percent and Em
The solution that works in all browsers, is to set the default font-size in percent for the body element:
example :
body {font-size: 100%;}
h1 {font-size: 2.5em;}
h2 {font-size: 1.875em;}
p {font-size: 0.875em;}
Note: The code now works great! It shows the same text size in all browsers, and allows all browsers to enlarge or change the text size!

Post a Comment