CSS Introduction
CSS Introduction
HTML / XHTML
What is CSS?
CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles are normally stored in Style Sheets Styles were added to HTML 4 ! to solve a problem External Style Sheets can save a lot of wor" #$ternal Style Sheets are stored in CSS files Multi%le style definitions will cascade into one
CSS demo
&n HTML document can be dis%layed with different styles: See how it wor"s
inside an HTML element inside the head section of an HTML %age in an e$ternal /SS file
ip! #ven multi%le e$ternal style sheets can be referenced inside a single HTML document
Cascading order " What style will be used when there is more than one style specified for an # M$ element?
7enerally s%ea"ing we can say that all the styles will 8cascade8 into a new 8virtual8 style sheet by the following rules' where number four has the highest %riority: * , 4 Browser default #$ternal style sheet 9nternal style sheet 0in the head section1 9nline style 0inside an HTML element1
So' an inline style 0inside an HTML element1 has the highest %riority' which means that it will override a style defined inside the (head) tag' or in an e$ternal style sheet' or in a browser 0a default value1 9f the lin" to the e$ternal style sheet is %laced after the internal style sheet in HTML (head)' the e$ternal style sheet will override the internal style sheet6
CSS Syntax
Syntax
The /SS synta$ is made u% of three %arts: a selector' a %ro%erty and a value:
selector :%ro%erty:value; The selector is normally the HTML element/tag you wish to define' the %ro%erty is the attribute you wish to change' and each %ro%erty can ta"e a value The %ro%erty and value are se%arated by a colon' and surrounded by curly braces: body :color:blac"; %ote! 9f the value is multi%le words' %ut <uotes around the value: % :font2family:8sans serif8; %ote! 9f you want to s%ecify more than one %ro%erty' you must se%arate each %ro%erty with a semicolon The e$am%le below shows how to define a center aligned %aragra%h' with a red te$t color: % :te$t2align:center=color:red; To ma"e the style definitions more readable' you can describe one %ro%erty on each line' li"e this: % : te$t2align:center= color:blac"= font2family:arial ;
&rouping
>ou can grou% selectors Se%arate each selector with a comma 9n the e$am%le below we have grou%ed all the header elements &ll header elements will be dis%layed in green te$t color: h*'h-'h,'h4'h?'h@ : color:green ;
he class Selector
+ith the class selector you can define different styles for the same ty%e of HTML element Say that you would li"e to have two ty%es of %aragra%hs in your document: one right2aligned %aragra%h' and one center2aligned %aragra%h Here is how you can do it with styles:
% right :te$t2align:right; % center :te$t2align:center; >ou have to use the class attribute in your HTML document: (% classA8right8)This %aragra%h will be right2aligned (/%) (% classA8center8)This %aragra%h will be center2aligned (/%) %ote! To a%%ly more than one class %er given element' the synta$ is: (% classA8center bold8)This is a %aragra%h (/%) The %aragra%h above will be styled by the class 8center8 &B. the class 8bold8 >ou can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class 9n the e$am%le below' all HTML elements with classA8center8 will be center2aligned: center :te$t2align:center; 9n the code below both the h* element and the % element have classA8center8 This means that both elements will follow the rules in the 8 center8 selector: (h* classA8center8)This heading will be center2aligned(/h*) (% classA8center8)This %aragra%h will also be center2aligned (/%) .o %' start a class name with a number6 9t will not wor" in Mo3illa/Cirefo$
he id Selector
>ou can also define styles for HTML elements with the id selector The id selector is defined as a F The style rule below will match the element that has an id attribute with a value of 8green8: Fgreen :color:green;
The style rule below will match the % element that has an id with a value of 8%ara*8: %F%ara* : te$t2align:center= color:red ; .o %' start an 9. name with a number6 9t will not wor" in Mo3illa/Cirefo$
CSS Comments
/omments are used to e$%lain your code' and may hel% you when you edit the source code at a later date & comment will be ignored by browsers & /SS comment begins with 8/G8' and ends with 8G/8' li"e this: /GThis is a commentG/ % : te$t2align:center= /GThis is another commentG/ color:blac"= font2family:arial ;
&n e$ternal style sheet can be written in any te$t editor The file should not contain any html tags >our style sheet should be saved with a css e$tension &n e$am%le of a style sheet file is shown below: hr :color:sienna; % :margin2left:-!%$; body :bac"ground2image:url08images/bac"4! gif81; .o not leave s%aces between the %ro%erty value and the units6 8margin2left:-! %$8 0instead of 8margin2left:-!%$81 will only wor" in 9#@' but it will not wor" in Cirefo$ or 4%era
Inline Styles
&n inline style loses many of the advantages of style sheets by mi$ing content with %resentation Hse this method s%aringly' such as when a style is to be a%%lied to a single occurrence of an element To use inline styles you use the style attribute in the relevant tag The style attribute can contain any /SS %ro%erty The e$am%le shows how to change the color and the left margin of a %aragra%h: (% styleA8color:sienna=margin2left:-!%$8)This is a %aragra%h (/%)
CSS +ackground
The /SS bac"ground %ro%erties define the bac"ground effects of an element
bac"ground2attachment
bac"ground2color
bac"ground2image bac"ground2%osition
* *
bac"ground2re%eat
CSS ext
The /SS te$t %ro%erties define the a%%earance of te$t:
text example
This e$am%le includes some te$t formatting %ro%erties The heading uses the te$t2align' te$t2transform' and color %ro%erties The %aragra%h is indented and aligned' and the underline is removed from the 8Try it yourself8 lin"
ext Color
The color %ro%erty is used to set the color of the te$t The color can be set by:
name 2 s%ecify a color name' li"e 8red8 J7B 2 s%ecify an J7B value' li"e 8rgb0-??'!'!18 He$ 2 s%ecify a he$ value' li"e 8Fff!!!!8
Example
body :color:blue; h* :color:F!!ff!!; h- :color:rgb0-??'!'!1;
Cor +,/ com%liant /SS: 9f you define the color %ro%erty' you must also define the bac"ground2 color %ro%erty
ext (lignment
The te$t2align %ro%erty is used to set the hori3ontal alignment of a te$t Te$t can be centered' or aligned to the left or right' or 5ustified +hen te$t2align is set to 85ustify8' each line is stretched so that every line has e<ual width' and the left and right margins are straight 0li"e in maga3ines and news%a%ers1
Example
h* :te$t2align:center; % date :te$t2align:right; % main :te$t2align:5ustify;
ext ,ecoration
The te$t2decoration %ro%erty is used to set or remove decorations from te$t The te$t2decoration %ro%erty is mostly used to remove underlines from lin"s for design %ur%oses:
Example
a :te$t2decoration:none;
Example
h* :te$t2decoration:overline; h- :te$t2decoration:line2through; h, :te$t2decoration:underline; h4 :te$t2decoration:blin";
9t is not recommended to underline te$t that is not a lin"' as this often confuse users
ext ransformation
The te$t2transform %ro%erty is used to s%ecify u%%ercase and lowercase letters in a te$t 9t can be used to turn everything into u%%ercase or lowercase letters' or ca%itali3e the first letter of each word
Example
% u%%ercase :te$t2transform:u%%ercase; % lowercase :te$t2transform:lowercase; % ca%itali3e :te$t2transform:ca%itali3e;
ext Indentation
The te$t2indentation %ro%erty is used to s%ecify the indentation of the first line of a te$t
Example
% :te$t2indent:?!%$;
letter2s%acing te$t2align
te$t2decoration
te$t2indent te$t2shadow
te$t2transform
unicode2bidi
white2s%ace
word2s%acing
% 9ncrease or decrease the s%ace between characters normal length &ligns the te$t in an element left right center 5ustify &dds decoration to te$t none underline overline line2through blin" 9ndents the first line of te$t in an element length % none color length /ontrols the letters in an element none ca%itali3e u%%ercase lowercase normal embed bidi2override Sets how white s%ace inside an element is handled normal %re nowra% 9ncrease or decrease the s%ace between words normal length
* *
CSS .ont
/SS font %ro%erties define the font family' boldness' si3e' and the style of a te$t
4n com%uter screens' sans2serif fonts are considered easier to read than serif fonts
generic family 2 a grou% of font families with a similar loo" 0li"e 8Serif8 or 8Monos%ace81 font family 2 a s%ecific font family 0li"e 8Times Bew Joman8 or 8&rial81 .ont family ,escription fonts have small lines at the ends on some Joman Serif characters
Sans2serif
Monos%ace
Times Bew Georgia 8Sans8 means without 2 these fonts do not have Arial the lines at the ends of characters Verdana Courier New &ll monos%ace characters has the same width Luc ida Conso le
.ont .amily
The font family of a te$t is set with the font2family %ro%erty The font2family %ro%erty can hold several font names as a 8fallbac"8 system 9f the browser does not su%%ort the first font' it tries the ne$t font Start with the font you want' and end with a generic family' to let the browser %ic" a similar font in the generic family' if no other fonts are available %ote: 9f the name of a font family is more than one word' it must be in <uotation mar"s' li"e font2family: 8Times Bew Joman8 More than one font family is s%ecified in a comma2se%arated list:
Example
%:font2family:8Times Bew Joman8'7eorgia'Serif;
.ont Style
The font2style %ro%erty is mostly used to s%ecify italic te$t This %ro%erty has three values:
normal 2 The te$t is shown normally italic 2 The te$t is shown in italics obli<ue 2 The te$t is 8leaning8 0obli<ue is very similar to italic' but less su%%orted1
Example
% normal :font2style:normal; % italic :font2style:italic; % obli<ue :font2style:obli<ue;
.ont Si/e
The font2si3e %ro%erty sets the si3e of the te$t Being able to manage the te$t si3e is im%ortant in web design However' you should not use font si3e ad5ustments to ma"e %aragra%hs loo" li"e headings' or headings loo" li"e %aragra%hs &lways use the %ro%er HTML tags' li"e (h*) 2 (h@) for headings and (%) for %aragra%hs The font2si3e value can be an absolute' or relative si3e &bsolute si3e:
Sets the te$t to a s%ecified si3e .oes not allow a user to change the te$t si3e in all browsers 0bad for accessibility reasons1 &bsolute si3e is useful when the %hysical si3e of the out%ut is "nown
Jelative si3e:
Sets the si3e relative to surrounding elements &llows a user to change the te$t si3e in browsers
9f you do not s%ecify a font si3e' the default si3e for normal te$t' li"e %aragra%hs' is *@%$ 0*@%$A*em1
Example
h* :font2si3e:4!%$; h- :font2si3e:,!%$; % :font2si3e:*4%$;
The e$am%le above allows Cirefo$' /hrome' and Safari to resi3e the te$t' but not Internet Explorer The te$t can be resi3ed in all browsers using the 3oom tool 0however' this resi3es the entire %age' not 5ust the te$t1
Example
h* :font2si3e:- ?em; /G 4!%$/*@A- ?em G/ h- :font2si3e:* IK?em; /G ,!%$/*@A* IK?em G/ % :font2si3e:! IK?em; /G *4%$/*@A! IK?em G/
9n the e$am%le above' the te$t si3e in em is the same as the %revious e$am%le in %i$els However' with the em si3e' it is %ossible to ad5ust the te$t si3e in all browsers Hnfortunately' there is still a %roblem with 9# +hen resi3ing the te$t' it becomes larger than it should when made larger' and smaller than it should when made smaller
Example
body :font2si3e:*!!L; h* :font2si3e:- ?em; h- :font2si3e:* IK?em; % :font2si3e:! IK?em;
4ur code now wor"s great6 9t shows the same te$t si3e in all browsers' and allows all browsers to 3oom or resi3e the te$t6
)roperty font
,escription & shorthand %ro%erty for setting all of the %ro%erties for a font in one declaration
font2family font2si3e
font2si3e2ad5ust font2stretch
font2style
font2variant font2weight
-alues font-style font-variant font-weight font-size/line-height font-family ca%tion icon menu message2bo$ small2ca%tion status2bar & %rioriti3ed list of font family names and/or family-name generic family names for an element generic-family Sets the si3e of a font $$2small $2small small medium large $2large $$2large smaller larger length % S%ecifies an as%ect value for an element that none will %reserve the $2height of the first2choice font number /ondenses or e$%ands the current font2family normal wider narrower ultra2condensed e$tra2condensed condensed semi2condensed semi2e$%anded e$%anded e$tra2e$%anded ultra2e$%anded Sets the style of the font normal italic obli<ue .is%lays te$t in a small2ca%s font or a normal normal font small2ca%s Sets the weight of a font normal bold
CSS *
* *
* *
bolder lighter *!! -!! ,!! 4!! ?!! @!! K!! I!! M!!
he +ox Model
The image below illustrates the bo$ model:
Margin 2 /lears an area around the border The margin does not have a bac"ground color' and it is com%letely trans%arent +order 2 & border that lies around the %adding and content The border is affected by the bac"ground color of the bo$ )adding 2 /lears an area around the content The %adding is affected by the bac"ground color of the bo$ Content 2 The content of the bo$' where te$t and images a%%ear
O -!%$ 0left and right margin1 A ,!!%$ 9magine that you only had -?!%$ of s%ace LetNs ma"e an element with a total width of -?!%$:
Example
width:--!%$= %adding:*!%$= border:?%$ solid gray= margin:!%$=
The total width of an element should always be calculated li"e this: Total element width A width O left %adding O right %adding O left border O right border O left margin O right margin The total height of an element should always be calculated li"e this: Total element height A height O to% %adding O bottom %adding O to% border O bottom border O to% margin O bottom margin
Example
(6.4/T>P# html PHBL9/ 82//+,///.T. XHTML * ! Transitional//#B8 8htt%://www w, org/TJ/$html*/.T./$html*2transitional dtd8) (html) (head) (style ty%eA8te$t/css8) div e$ : width:--!%$= %adding:*!%$= border:?%$ solid gray= margin:!%$= ; (/style) (/head)
CSS +order
The /SS border %ro%erties define the borders around an element
border2bottom
setting all of the %ro%erties for the bottom border in one declaration Sets the color of the bottom border Sets the style of the bottom border Sets the width of the bottom border
border2color
border2left
Sets the color of the four borders' can have from one to four colors & shorthand %ro%erty for setting all of the %ro%erties for the left border in one declaration Sets the color of the left border Sets the style of the left border Sets the width of the left border
border-left-width border-style border-color border-color border-style thin medium thic" length border-right-width border-style border-color border-color border-style
4 4 4
* * *
@ @ 4
border2right
& shorthand %ro%erty for setting all of the %ro%erties for the right border in one declaration Sets the color of the right border Sets the style of the right border Sets the width of the right border
4 4 4
* * *
@ @ 4
border2style
thin medium thic" length Sets the style of the four none borders' can have from one hidden to four styles dotted dashed solid double
border2to%
& shorthand %ro%erty for setting all of the %ro%erties for the to% border in one declaration Sets the color of the to% border Sets the style of the to% border Sets the width of the to% border
4 4 4
* * *
@ @ 4
border2width
thin medium thic" length & shorthand %ro%erty for thin setting the width of the four medium borders in one declaration' thic" can have from one to four length values
CSS 'utlines
The /SS outline %ro%erties is used to draw a line around an element' outside the border edge
outline2color outline2style
Sets the color of the outline around an element Sets the style of the outline around an element
outline2width
color invert none dotted dashed solid double groove ridge inset outset thin medium thic" length
CSS Margin
The /SS margin %ro%erties define the s%ace around elements
margin2bottom
element margin2left Sets the left margin of an element Sets the right margin of an element Sets the to% margin of an element
margin2right
margin2to%
CSS )adding
The /SS %adding %ro%erties define the s%ace between the element border and the element content
4 4 4 4
* * * *
4 4 4 4
* * * *
CSS $ist
The /SS list %ro%erties allow you to %lace the list2item mar"er' change between different list2 item mar"ers' or set an image as the list2item mar"er
4 4 4
* * *
@ @ 4
* * *
mar"er2offset
CSS able
The /SS table %ro%erties allow you to set the layout of a table
border2s%acing
length length
?M *
ca%tion2side
em%ty2cells
table2layout
to% bottom left right Sets whether or not to show show em%ty cells in a table 0only hide for the 8se%arated borders8 model1 Sets the algorithm used to auto dis%lay the table cells' rows' fi$ed and columns
?M *
?M *
CSS ,imension
The /SS dimension %ro%erties allow you to control the height and width of an element 9t also allows you to increase the s%ace between two lines
line2height
ma$2height
ma$2width
2 2 4
* * *
@ @ 4
CSS Classification
The /SS classification %ro%erties allow you to s%ecify how and where to dis%lay an element
cursor
dis%lay
float
%osition
visibility
table2row table2column2grou% table2column table2cell table2ca%tion Sets where an image or a left te$t will a%%ear in another right element none Places an element in a static' static relative' absolute or fi$ed relative %osition absolute fi$ed Sets if an element should be visible visible or invisible hidden colla%se
CSS )ositioning
The /SS %ositioning %ro%erties allows you to %osition an element
cli%
left
overflow
element Sets what ha%%ens if the content of an element overflow its area
%osition
right
to%
vertical2align
32inde$
visible hidden scroll auto Places an element in a static' static relative' absolute or fi$ed relative %osition absolute fi$ed Sets how far the right edge auto of an element is to the % left/right of the right edge of length the %arent element Sets how far the to% edge of auto an element is above/below % the to% edge of the %arent length element Sets the vertical alignment ofbaseline an element sub su%er to% te$t2to% middle bottom te$t2bottom length % Sets the stac" order of an auto element number
CSS )seudo"classes
/SS %seudo2classes are used to add s%ecial effects to some selectors
Syntax
The synta$ of %seudo2classes: selector:%seudo2class :%ro%erty:value; /SS classes can also be used with %seudo2classes: selector class:%seudo2class :%ro%erty:value;
(nchor )seudo"classes
Lin"s can be dis%layed in different ways in a /SS2su%%orting browser: a:lin" :color:FCC!!!!; /G unvisited lin" G/ a:visited :color:F!!CC!!; /G visited lin" G/ a:hover :color:FCC!!CC; /G mouse over lin" G/ a:active :color:F!!!!CC; /G selected lin" G/ %ote! a:hover MHST come after a:lin" and a:visited in the /SS definition in order to be effective66 %ote! a:active MHST come after a:hover in the /SS definition in order to be effective66 %ote! Pseudo2class names are not case2sensitive
Example
(html) (head) (style ty%eA8te$t/css8) %:first2child : color:blue ; (/style) (/head) (body) (%)9 am a strong man (/%) (%)9 am a strong man (/%) (/body) (/html)
Example
(html) (head) (style ty%eA8te$t/css8) % ) i:first2child : font2weight:bold ; (/style) (/head) (body) (%)9 am a (i)strong(/i) man 9 am a (i)strong(/i) man (/%) (%)9 am a (i)strong(/i) man 9 am a (i)strong(/i) man (/%) (/body) (/html)
Example
(html) (head) (style ty%eA8te$t/css8) %:first2child i : color:blue ; (/style) (/head) (body) (%)9 am a (i)strong(/i) man 9 am a (i)strong(/i) man (/%) (%)9 am a (i)strong(/i) man 9 am a (i)strong(/i) man (/%) (/body) (/html)
(%)Some te$t (< langA8no8)& <uote in a %aragra%h(/<) Some te$t (/%) (/body) (/html)
)seudo"classes
+rowser support! 9#: 9nternet #$%lorer' C: Cirefo$' B: Betsca%e W2C! The number in the 8+,/8 column indicates in which /SS recommendation the %ro%erty is defined 0/SS* or /SS-1 )seudo"class :active :focus :hover :lin" :visited :first2child :lang )urpose &dds s%ecial style to an activated element &dds s%ecial style to an element while the element has focus &dds s%ecial style to an element when you mouse over it &dds s%ecial style to an unvisited lin" &dds s%ecial style to a visited lin" &dds s%ecial style to an element that is the first child of some other element &llows the author to s%ecify a language to use in a s%ecified element IE . % W2C 4 * I * 2 *?I 4 , , K 2 * * * * * K 4 4 K I * * * -
Image &allery
The following image gallery is created with /SS: 9mage gallery The source code loo"s li"e this: (html) (head) (style ty%eA8te$t/css8) div img : margin:-%$=
border:*%$ solid F!!!!ff= height:auto= width:auto= float:left= te$t2align:center= ; div img img : dis%lay:inline= margin:,%$= border:*%$ solid Fffffff= ; div img a:hover img : border:*%$ solid F!!!!ff= ; div desc : te$t2align:center= font2weight:normal= width:*-!%$= margin:-%$= ; (/style) (/head) (body) (div classA8img8) (a targetA8Qblan"8 hrefA8"lematisQbig htm8) (img srcA8"lematisQsmall 5%g8 altA8Slematis8 widthA8**!8 heightA8M!8 /) (/a) (div classA8desc8)&dd a descri%tion of the image here(/div) (/div) (div classA8img8) (a targetA8Qblan"8 hrefA8"lematis-Qbig htm8) (img srcA8"lematis-Qsmall 5%g8 altA8Slematis8 widthA8**!8 heightA8M!8 /) (/a) (div classA8desc8)&dd a descri%tion of the image here(/div) (/div) (div classA8img8) (a targetA8Qblan"8 hrefA8"lematis,Qbig htm8) (img srcA8"lematis,Qsmall 5%g8 altA8Slematis8 widthA8**!8 heightA8M!8 /) (/a) (div classA8desc8)&dd a descri%tion of the image here(/div) (/div) (div classA8img8)
(a targetA8Qblan"8 hrefA8"lematis4Qbig htm8) (img srcA8"lematis4Qsmall 5%g8 altA8Slematis8 widthA8**!8 heightA8M!8 /) (/a) (div classA8desc8)&dd a descri%tion of the image here(/div) (/div) (/body) (/html)
Loo" at the following source code: (img srcA8"lematis 5%g8 widthA8*?!8 heightA8**,8 altA8"lematis8 styleA8o%acity:! 4=filter:al%ha0o%acityA4!18 /) Cirefo$ uses the %ro%erty opacity!x for trans%arency' while 9# uses filter!alpha7opacity8x9 ip! The /SS, synta$ for trans%arency is opacity!x
9n Cirefo$ 0o%acity:$1 $ can be a value from ! ! 2 * ! & lower value ma"es the element more trans%arent 9n 9# 0filter:al%ha0o%acityA$11 $ can be a value from ! 2 *!! & lower value ma"es the element more trans%arent
The source code loo"s li"e this: (img srcA8"lematis 5%g8 styleA8o%acity:! 4=filter:al%ha0o%acityA4!18 onmouseoverA8this style o%acityA*=this filters al%ha o%acityA*!!8 onmouseoutA8this style o%acityA! 4=this filters al%ha o%acityA4!8 /) (img srcA8"lematis- 5%g8 styleA8o%acity:! 4=filter:al%ha0o%acityA4!18 onmouseoverA8this style o%acityA*=this filters al%ha o%acityA*!!8 onmouseoutA8this style o%acityA! 4=this filters al%ha o%acityA4!8 /) +e see that the first line of the source code is similar to the source code in #$am%le * 9n addition' we have added an onmouseover attribute and an onmouseout attribute The onmouseover attribute defines what will ha%%en when the mouse %ointer moves over the image 9n this case we want the image to B4T be trans%arent when we move the mouse %ointer over it The synta$ for this in Cirefo$ is: this*style*opacity86 and the synta$ in 9# is: this*filters*alpha*opacity86;; +hen the mouse %ointer moves away from the image' we want the image to be trans%arent again This is done in the onmouseout attribute
(html) (head) (style ty%eA8te$t/css8) div bac"ground : width:?!!%$= height:-?!%$= bac"ground:url0"lematis 5%g1 re%eat= border:-%$ solid blac"= ; div transbo$ : width:4!!%$= height:*I!%$= margin:,!%$ ?!%$= bac"ground2color:Fffffff= border:*%$ solid blac"= /G for 9# G/ filter:al%ha0o%acityA@!1= /G /SS, standard G/ o%acity:! @= ; div transbo$ % : margin:,!%$ 4!%$= font2weight:bold= color:F!!!!!!= ; (/style) (/head) (body) (div classA8bac"ground8) (div classA8transbo$8) (%)This is some te$t that is %laced in the trans%arent bo$ This is some te$t that is %laced in the trans%arent bo$ This is some te$t that is %laced in the trans%arent bo$ This is some te$t that is %laced in the trans%arent bo$ This is some te$t that is %laced in the trans%arent bo$ (/%) (/div) (/div) (/body) (/html)
Cirst' we create a div element 0classA8bac"ground81 with a fi$ed height and width' a bac"ground image' and a border Then we create a smaller div 0classA8transbo$81 inside the first div element This div also have a fi$ed width' a bac"ground image' and a border 9n addition we ma"e this div trans%arent 9nside the trans%arent div' we add some te$t inside a % element
Media ypes
Some /SS %ro%erties are only designed for a certain media Cor e$am%le the 8voice2family8 %ro%erty is designed for aural user agents Some other %ro%erties can be used for different media ty%es Cor e$am%le' the 8font2si3e8 %ro%erty can be used for both screen and %rint media' but %erha%s with different values & document usually needs a larger font2si3e on a screen than on %a%er' and sans2serif fonts are easier to read on the screen' while serif fonts are easier to read on %a%er
he <media =ule
The Tmedia rule allows different style rules for different media in the same style sheet The style in the e$am%le below tells the browser to dis%lay a *4 %i$els Uerdana font on the screen But if the %age is %rinted' it will be in a *! %i$els Times font Botice that the font2weight is set to bold' both on screen and on %a%er: (html) (head) (style) Tmedia screen : % test :font2family:verdana'sans2serif=font2si3e:*4%$; ; Tmedia %rint : % test :font2family:times'serif=font2si3e:*!%$; ; Tmedia screen'%rint : % test :font2weight:bold; ;
(/style) (/head) (body) (/body) (/html) See it yourself > 9f you are using Mo3illa/Cirefo$ or 9# ?O and %rint this %age' you will see that the %aragra%h under 8Media Ty%es8 will be dis%layed in another font' and have a smaller font si3e than the rest of the te$t
CSS ,on?t
Here are some technologies you should try to avoid when using /SS
The following HTML file has a (style) element that defines a behavior for the (h*) element: (html) (head) (style ty%eA8te$t/css8) h* : behavior:url0behave htc1 ; (/style) (/head) (body) (h*)Mouse over me666(/h*) (/body) (/html) The XML document 8behave htc8 is shown below:
(head) (style ty%eA8te$t/css8) Fty%ing : behavior:url0behaveQty%ing htc1= font2family:8courier new8= ; (/style) (/head) (body) (s%an idA8ty%ing8 s%eedA8*!!8)9#? introduced .HTML behaviors Behaviors are a way to add .HTML functionality to HTML elements with the ease of /SS (br /)(br /)How do behaviors wor"W(br /) By using XML we can lin" behaviors to any element in a web %age and mani%ulate that element (/%) (/s%an) (/body) (/html) The XML document 8ty%ing htc8 is shown below:
(attach forA8window8 eventA8onload8 handlerA8beginTy%ing8 /) (method nameA8ty%e8 /) (scri%t ty%eA8te$t/5avascri%t8) var i'te$t*'te$t-'te$tLength't= function beginTy%ing01 : iA!= te$t*Aelement innerTe$t= te$tLengthAte$t* length= element innerTe$tA88= te$t-A88= tAwindow set9nterval0element idO8 ty%e018's%eed1= ; function ty%e01 : te$t-Ate$t-Ote$t* substring0i'iO*1= element innerTe$tAte$t-= iAiO*= if 0iAAte$tLength1 : clear9nterval0t1= ; ; (/scri%t)
Cor more information on /SS' %lease ta"e a loo" at our /SS e$am%les and our /SS reference