CSS Fonts: Difference Between Serif and Sans Serif Fonts
CSS Fonts: Difference Between Serif and Sans Serif Fonts
CSSFonts
CSS Fonts
Previous
NextChapter
TheCSSfontpropertiesdefinethefontfamily,boldness,size,andthestyleofa
text.
Generic
family
Fontfamily
Description
Serif
TimesNew
Roman
Georgia
Seriffontshavesmalllinesattheends
onsomecharacters
Sansserif
Arial
"Sans"meanswithoutthesefontsdo
nothavethelinesattheendsof
1/8
5/24/2016
CSSFonts
Monospace
Verdana
characters
CourierNew
Lucida
Console
Allmonospacecharactershavethe
samewidth
Note:Oncomputerscreens,sansseriffontsareconsideredeasiertoread
thanseriffonts.
Font Family
Thefontfamilyofatextissetwiththe fontfamily property.
The fontfamily propertyshouldholdseveralfontnamesasa"fallback"system.Ifthe
browserdoesnotsupportthefirstfont,ittriesthenextfont,andsoon.
Startwiththefontyouwant,andendwithagenericfamily,toletthebrowserpicka
similarfontinthegenericfamily,ifnootherfontsareavailable.
Note:Ifthenameofafontfamilyismorethanoneword,itmustbeinquotation
marks,like:"TimesNewRoman".
Morethanonefontfamilyisspecifiedinacommaseparatedlist:
Example
p{
fontfamily:"TimesNewRoman",Times,serif;
}
TryitYourself
Forcommonlyusedfontcombinations,lookatourWebSafeFontCombinations.
Font Style
2/8
5/24/2016
CSSFonts
Example
p.normal{
fontstyle:normal;
}
p.italic{
fontstyle:italic;
}
p.oblique{
fontstyle:oblique;
}
TryitYourself
Font Size
The fontsize propertysetsthesizeofthetext.
Beingabletomanagethetextsizeisimportantinwebdesign.However,youshouldnot
usefontsizeadjustmentstomakeparagraphslooklikeheadings,orheadingslooklike
paragraphs.
AlwaysusetheproperHTMLtags,like<h1><h6>forheadingsand<p>for
paragraphs.
Thefontsizevaluecanbeanabsolute,orrelativesize.
Absolutesize:
3/8
5/24/2016
CSSFonts
Setsthetexttoaspecifiedsize
Doesnotallowausertochangethetextsizeinallbrowsers(badforaccessibility
reasons)
Absolutesizeisusefulwhenthephysicalsizeoftheoutputisknown
Relativesize:
Setsthesizerelativetosurroundingelements
Allowsausertochangethetextsizeinbrowsers
Note:Ifyoudonotspecifyafontsize,thedefaultsizefornormaltext,
likeparagraphs,is16px(16px=1em).
Example
h1{
fontsize:40px;
}
h2{
fontsize:30px;
}
p{
fontsize:14px;
}
TryitYourself
Tip:Ifyouusepixels,youcanstillusethezoomtooltoresizetheentirepage.
5/24/2016
CSSFonts
Toallowuserstoresizethetext(inthebrowsermenu),manydevelopersuseem
insteadofpixels.
TheemsizeunitisrecommendedbytheW3C.
1emisequaltothecurrentfontsize.Thedefaulttextsizeinbrowsersis16px.So,the
defaultsizeof1emis16px.
Thesizecanbecalculatedfrompixelstoemusingthisformula:pixels/16=em
Example
h1{
fontsize:2.5em;/*40px/16=2.5em*/
}
h2{
fontsize:1.875em;/*30px/16=1.875em*/
}
p{
fontsize:0.875em;/*14px/16=0.875em*/
}
TryitYourself
Intheexampleabove,thetextsizeinemisthesameasthepreviousexamplein
pixels.However,withtheemsize,itispossibletoadjustthetextsizeinallbrowsers.
Unfortunately,thereisstillaproblemwitholderversionsofIE.Thetextbecomeslarger
thanitshouldwhenmadelarger,andsmallerthanitshouldwhenmadesmaller.
5/8
5/24/2016
CSSFonts
Example
body{
fontsize:100%;
}
h1{
fontsize:2.5em;
}
h2{
fontsize:1.875em;
}
p{
fontsize:0.875em;
}
TryitYourself
Ourcodenowworksgreat!Itshowsthesametextsizeinallbrowsers,andallowsall
browserstozoomorresizethetext!
Font Weight
The fontweight propertyspecifiestheweightofafont:
Example
p.normal{
fontweight:normal;
}
p.thick{
fontweight:bold;
6/8
5/24/2016
CSSFonts
}
TryitYourself
Font Variant
The fontvariant propertyspecifieswhetherornotatextshouldbedisplayedina
smallcapsfont.
Inasmallcapsfont,alllowercaselettersareconvertedtouppercaseletters.However,
theconverteduppercaselettersappearsinasmallerfontsizethantheoriginal
uppercaselettersinthetext.
Example
p.normal{
fontvariant:normal;
}
p.small{
fontvariant:smallcaps;
}
TryitYourself
More Examples
Allthefontpropertiesinonedeclaration
Thisexampledemonstrateshowtousetheshorthandpropertyforsettingallofthefont
propertiesinonedeclaration.
7/8
5/24/2016
CSSFonts
Exercise2
Exercise3
Exercise4
Exercise5
Description
font
Setsallthefontpropertiesinonedeclaration
fontfamily
Specifiesthefontfamilyfortext
fontsize
Specifiesthefontsizeoftext
fontstyle
Specifiesthefontstylefortext
fontvariant
Specifieswhetherornotatextshouldbedisplayedina
smallcapsfont
fontweight
Specifiestheweightofafont
Previous
NextChapter
Copyright19992015byRefsnesData.AllRightsReserved.
8/8