CSS

Font Family :

The font family of a text is set with the font-family property.
Example :
p {
    font-family: "Times New Roman", Times, serif;
}

Font Style :

The font style of a text is set with the font-style property.
Example :
p{
font-style: normal;//italic;
}

Font Size :

The font size of a text is set with the font-size property.(16px (16px=1em))
Example :
h1 {
    font-size: 40px;
}

h2 {
   font-size: 2.5em; /* 40px/16=2.5em */
}
h3 {
   font-size: 1.875em; /* 30px/16=1.875em */
}

Font Weight :

The font-weight property specifies the weight of a font:
Example :
p {
    font-weight: normal;
}

div {
    font-weight: bold;
}

Font Variant :

The font-variant property specifies whether or not a text should be displayed in a small-caps font. Example :
p {
    font-variant: normal;
}

div {
    font-variant: small-caps;
}

All the font properties in one declaration

Example :
<head>
<style>
p {
    font: 15px arial, sans-serif;
}

div {
    font:italic bold 12px/30px Georgia, serif;
}
</style>
</head>

CSS Fonts


CSS Font Families

- The CSS font properties define the font family, boldness, size, and the style of a text.
- generic family - a group of font families with a similar look (like "Serif" or "Monospace")
- font family - a specific font family (like "Times New Roman" or "Arial")

Generic family Font family Description
SerifTimes New Roman/GeorgiaSerif fonts have small lines at the ends on some characters
Sans-serifArial/Verdana"Sans" means without - these fonts do not have the lines at the ends of characters
MonospaceCourier New/Lucida ConsoleAll monospace characters have the same width

Font Family

- The font family of a text is set with the font-family property.
- The font-family property should hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font, and so on.
- 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 a font family is more than one word, it must be in quotation marks, like: "Times New Roman".
- More than one font family is specified in a comma-separated list.

Example:


<!DOCTYPE html>
<html>
<head>
<style>
p.serif {
    font-family: "Times New Roman", Times, serif;
}

p.sansserif {
    font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<body>

<h1>CSS font-family</h1>
<p class="serif">This is a paragraph, shown in the Times New Roman font.</p>
<p class="sansserif">This is a paragraph, shown in the Arial font.</p>

</body>
</html>

Font Style

The font-style property is mostly used to specify italic text.
This property has three values:
normal - The text is shown normally
italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)

Example:


<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
    font-style: normal;
}

p.italic {
    font-style: italic;
}

p.oblique {
    font-style: oblique;
}
</style>
</head>
<body>

<p class="normal">This is a paragraph in normal style.</p>
<p class="italic">This is a paragraph in italic style.</p>
<p class="oblique">This is a paragraph in oblique style.</p>

</body>
</html>

Font Size

The font-size property sets the size of the text.
Being able to manage the text 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 the proper HTML tags, like <h1> -<h6> for headings and <p> for paragraphs.
The font-size value can be an absolute, or relative size.
Absolute size:
- Sets the text to a specified size
- Does not allow a user 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:
- Sets the size relative to surrounding elements
- Allows a user to change the text size in browsers
- Note: If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).

Example:


<!DOCTYPE html>
<html>
<head>
<style>
h1 {
    font-size: 40px;
}

h2 {
    font-size: 30px;
}

p {
    font-size: 14px;
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

</body>
</html>

Set Font Size With Em

- To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
- The em size unit is recommended by the W3C.
- 1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is 16px.
- The size can be calculated from pixels to em using this formula: pixels/16=em.
- with the em size, it is possible to adjust the text size in all browsers.

Example:


<!DOCTYPE html>
<html>
<head>
<style>
h1 {
    font-size: 2.5em; /* 40px/16=2.5em */
}

h2 {
    font-size: 1.875em; /* 30px/16=1.875em */
 }

p {
    font-size: 0.875em; /* 14px/16=0.875em */
}
</style>
</head>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
<p>Specifying the font-size in em allows all major browsers to resize the text.
Unfortunately, there is still a problem with older versions of IE. When resizing the text, it becomes larger/smaller than it should.</p>

</body>
</html>

Font Weight

The font-weight property specifies the weight of a font

Example:


<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
    font-weight: normal;
}

p.light {
    font-weight: lighter;
}

p.thick {
    font-weight: bold;
}

p.thicker {
    font-weight: 900;
}
</style>
</head>
<body>

<p class="normal">This is a paragraph.</p>
<p class="light">This is a paragraph.</p>
<p class="thick">This is a paragraph.</p>
<p class="thicker">This is a paragraph.</p>

</body>
</html>

Font Variant

The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a smaller font size than the original uppercase letters in the text.

Example:


<!DOCTYPE html>
<html>
<head>
<style>
p.normal {
    font-variant: normal;
}

p.small {
    font-variant: small-caps;
}
</style>
</head>
<body>

<p class="normal">My name is Hege Refsnes.</p>
<p class="small">My name is Hege Refsnes.</p>

</body>
</html>