8/4/13
CSS Font
Search w3schools.com: Select Language
HOME
HTML
C SS
JAVASC RIPT
JQUERY
XML
ASP.NET
PHP
SQL
MORE...
R EFER ENC ES
EXAMPLES
FO R UM
ABO UT
Get Certified
Study Web Technologies and get a diploma at w3schools.com
New Upcoming Projects 1/2/3/4 BHK Flats/Apts in ur Budge t Se arch by Locality,Price ,Am e nitie s. www.99acre s.com /Ne w_Proje cts
SHARE THIS PAGE
2/3/4 BHK in Noida Sec-78 Starts from R s 65.9 Lacs, Approve d By Noida Authority. Enquire Now! HDFC R ED.com /Noida-Mahahgun-Mode rne
Like Welcome to Adlabs Imagica Indias First Inte rnational Standard The m e Park . Book Your Tick e ts Now! www.adlabsim agica.com /Book _Now 76k
CSS Basic
CSS HOME CSS Introduction CSS Syntax CSS Id & Class CSS How To
CSS Font
Previous
CSS font properties define the font family, boldness, size, and the style of a text.
WEB HOSTING
Next Chapter
Best Web Hosting eUK Web Hosting UK Reseller Hosting Domain, Hosting & Email
CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables
Difference Between Serif and Sans-serif Fonts
WEB BUILDING
XML Editor - Free Trial! FREE Website BUILDER FREE Website C reator Best Website Templates
CSS Box Model
CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding On computer screens, sans-serif fonts are considered easier to read than serif fonts.
STATISTICS
Browser Statistics OS Statistics Display Statistics
CSS Font Families
In CSS, there are two types of font family names: 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 Serif Font family Description Serif fonts have small lines at the ends on some characters "Sans" means without - these fonts do not have the lines at the ends of characters All monospace characters have the same width
CSS Advanced
CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Summary
Times New Roman Georgia Arial Verdana C o u r i e rN e w L u c i d aC o n s o l e
Sans-serif
Monospace
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. 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 font-family: "Times New Roman". More than one font family is specified in a comma-separated list:
CSS Examples
CSS Examples CSS Quiz CSS Certificate
CSS References
CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX
Example
p { f o n t f a m i l y : " T i m e sN e wR o m a n " ,T i m e s ,s e r i f ; }
Try it yourself For more commonly used font combinations, look at our Web Safe Font Combinations.
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
p . n o r m a l{ f o n t s t y l e : n o r m a l ; } p . i t a l i c{ f o n t s t y l e : i t a l i c ; } p . o b l i q u e{ f o n t s t y l e : o b l i q u e ; }
Try it yourself
w3schools.com/css/css_font.asp
1/3
8/4/13
CSS Font
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 If you do not specify a font size, the default size for normal text, like paragraphs, is 16px (16px=1em).
Set Font Size With Pixels
Setting the text size with pixels gives you full control over the text size:
Example
h 1{ f o n t s i z e : 4 0 p x ; } h 2{ f o n t s i z e : 3 0 p x ; } p{ f o n t s i z e : 1 4 p x ; }
Try it yourself The example above allows Internet Explorer 9, Firefox, Chrome, Opera, and Safari to resize the text. Note: The example above does not work in IE, prior version 9. The text can be resized in all browsers using the zoom tool (however, this resizes the entire page, not just the text).
Set Font Size With Em
To avoid the resizing problem with older versions of Internet Explorer, 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
Example
h 1{ f o n t s i z e : 2 . 5 e m ; }/ *4 0 p x / 1 6 = 2 . 5 e m* / h 2{ f o n t s i z e : 1 . 8 7 5 e m ; }/ *3 0 p x / 1 6 = 1 . 8 7 5 e m* / p{ f o n t s i z e : 0 . 8 7 5 e m ; }/ *1 4 p x / 1 6 = 0 . 8 7 5 e m* /
Try it yourself In the example above, the text size in em is the same as the previous example in pixels. However, with the em size, it is possible to adjust the text size in all browsers. Unfortunately, there is still a problem with older versions of IE. The text becomes larger than it should when made larger, and smaller than it should when made smaller.
Use a Combination of Percent and Em
The solution that works in all browsers, is to set a default font-size in percent for the <body> element:
Example
b o d y{ f o n t s i z e : 1 0 0 % ; } h 1{ f o n t s i z e : 2 . 5 e m ; } h 2{ f o n t s i z e : 1 . 8 7 5 e m ; } p{ f o n t s i z e : 0 . 8 7 5 e m ; }
Try it yourself Our code now works great! It shows the same text size in all browsers, and allows all browsers to zoom or resize the text!
More Examples
Set the boldness of the font This example demonstrates how to set the boldness of a font. Set the variant of the font This example demonstrates how to set the variant of a font.
w3schools.com/css/css_font.asp
2/3
8/4/13
CSS Font
All the font properties in one declaration This example demonstrates how to use the shorthand property for setting all of the font properties in one declaration.
All CSS Font Properties
Property font font-family font-size font-style font-variant font-weight Description Sets all the font properties in one declaration Specifies the font family for text Specifies the font size of text Specifies the font style for text Specifies whether or not a text should be displayed in a small-caps font Specifies the weight of a font
Previous
Next Chapter
Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial
Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML C olors
Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples
Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz
Color Picker
Statistics
Browser Statistics Browser OS Browser Display
RE P O RT E RRO R
HO ME
TO P
P RI N T
FO RU M
A BO U T
A D V E RT I SE WI T H U S
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.
w3schools.com/css/css_font.asp
3/3