Académique Documents
Professionnel Documents
Culture Documents
This text is styled with some of the text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented, aligned, and the space between characters is specified.
Text Color
The color property is used to set the color of the text. With CSS, a color is most often specified by: 1. a !" #alue - li$e %&ff''''% (. an )*+ #alue - li$e %rgb,(--,','.% /. a color name - li$e %red%
The default color for a page is defined in the body selector. Example body {color:blue;} h1 {color:#00ff00;} h2 {color:rgb(2 !0!0";} 0ote : 1or W/C compliant CSS: 2f you define the color property, you must also define the bac$ground-color property.
Text #l$g%me%t
The text-align property is used to set the hori3ontal alignment of a text. Text can be centered, or aligned to the left or right, or 4ustified. When text-align is set to %4ustify%, each line is stretched so that e#ery line has e5ual width, and the left and right margins are straight ,li$e in maga3ines and newspapers..
Example h1 {text&al$g%:ce%ter;} p'date {text&al$g%:r$ght;} p'ma$% {text&al$g%:(u)t$fy;}
Text *ecorat$o%
The text-decoration property is used to set or remo#e decorations from text. The text-decoration property is mostly used to remo#e underlines from lin$s for design purposes:
Example a {text&decorat$o%:%o%e;} 2t can also be used to decorate text: Example h1 {text&decorat$o%:o+erl$%e;} h2 {text&decorat$o%:l$%e&through;} h, {text&decorat$o%:u%derl$%e;} h- {text&decorat$o%:bl$%.;}
0ote: 2t is not recommended to underline text that is not a lin$, as this often confuses users.
Text Tra%)format$o%
The text-transform property is used to specify uppercase and lowercase letters in a text. 2t can be used to turn e#erything into uppercase or lowercase letters, or capitali3e the first letter of each word.
Example
p'upperca)e {text&tra%)form:upperca)e;} p'lo/erca)e {text&tra%)form:lo/erca)e;} p'cap$tal$0e {text&tra%)form:cap$tal$0e;}
Text 1%de%tat$o%
The text-indentation property is used to specify the indentation of the first line of a text. Example p {text&$%de%t: 0px;}
CSS 3o%t
CSS font properties define the font family, boldness, si3e, and the style of a text.
2roperty fo%t fo%t&fam$ly fo%t&)$0e fo%t&)tyle fo%t&/e$ght *e)cr$pt$o% Sets all the font properties in one declaration Specifies the font family for text Specifies the font si3e of text Specifies the font style for text Specifies the weight of a font
9n computer screens, sans-serif fonts are considered easier to read than serif fonts.
ge%er$c fam$ly - a group of font families with a similar loo$ ,li$e %Serif% or %:onospace%. fo%t fam$ly - a specific font family ,li$e %Times 0ew )oman% or %6rial%.
3o%t fam$ly *e)cr$pt$o% Serif fonts ha#e small lines at the ends on some characters %Sans% means without - these fonts do not ha#e the lines at the ends of characters 6ll monospace characters ha#e the same width
3o%t 3am$ly
The font family of a text is set with the font-family property. The font-family property should hold se#eral font names as a %fallbac$% system. 2f 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 pic$ a similar font in the generic family, if no other fonts are a#ailable.
6ote: 2f the name of a font family is more than one word, it must be in 5uotation mar$s, li$e font-family: %Times 0ew )oman%. :ore than one font family is specified in a comma-separated list: Example p=font-family:%Times 0ew )oman%, Times, serif>?
3o%t Style
The font-style property is mostly used to specify italic text. This property has three #alues: normal - The text is shown normally italic - The text is shown in italics obli5ue - The text is %leaning% ,obli5ue is #ery similar to italic, but less supported.
Example p'%ormal {fo%t&)tyle:%ormal;} p'$tal$c {fo%t&)tyle:$tal$c;} p'obl$7ue {fo%t&)tyle:obl$7ue;}
3o%t S$0e
The font-si3e property sets the si3e of the text. +eing able to manage the text si3e is important in web design. owe#er, you should not use font si3e ad4ustments to ma$e paragraphs loo$ li$e headings, or headings loo$ li$e paragraphs. 6lways use the proper T:< tags, li$e @h1A - @hBA for headings and @pA for paragraphs. The font-si3e #alue can be an absolute or relati#e si3e. #b)olute )$0e: 1. Sets the text to a specified si3e (. Coes not allow a user to change the text si3e in all browsers ,bad for accessibility reasons. /. 6bsolute si3e is useful when the physical si3e of the output is $nown 8elat$+e )$0e: 1. Sets the si3e relati#e to surrounding elements (. 6llows a user to change the text si3e in browsers 6ote: The default si3e for normal text, li$e paragraphs, is 1Bpx ,1BpxD1em.. Set 3o%t S$0e 9$th 2$xel)
Setting the text si3e with pixels, gi#es you full control o#er the text si3e: Example h1 {fo%t&)$0e:-0px;}
h2 {fo%t&)$0e:,0px;} p {fo%t&)$0e:1-px;} The example abo#e allows 1irefox, Chrome, and Safari to resi3e the text, but %ot 1%ter%et Explorer. The text can be resi3ed in all browsers using the 3oom tool ,howe#er, this resi3es the entire page, not 4ust the text.. The default text si3e in browsers is 1emD1Bpx. Example h1 {fo%t&)$0e:2' em;} :; -0px:1<=2' em ;: h2 {fo%t&)$0e:1'>? em;} :; ,0px:1<=1'>? em ;: p {fo%t&)$0e:0'>? em;} :; 1-px:1<=0'>? em ;:
Example
body {fo%t&)$0e:100A;} h1 {fo%t&)$0e:2' em;} h2 {fo%t&)$0e:1'>? em;} p {fo%t&)$0e:0'>? em;} 9ur code now wor$s greatE 2t shows the same text si3e in all browsers, and allows all browsers to 3oom or resi3e the textE
CSS B$%.)
<in$s can be styled in different ways. Styl$%g B$%.) <in$s can be styled with any CSS property ,e.g. color, font-family, bac$ground, etc... Special for lin$s are that they can be styled differently depending on what state they are in. The four lin$s states are:
a:l$%. - a normal, un#isited lin$ a:+$)$ted - a lin$ the user has #isited a:ho+er - a lin$ when the user mouses o#er it a:act$+e - a lin$ the moment it is clic$ed
Example a:l$%. {color:#330000;} :; u%+$)$ted l$%. ;: a:+$)$ted {color:#003300;} :; +$)$ted l$%. ;: a:ho+er {color:#330033;} :; mou)e o+er l$%. ;: a:act$+e {color:#000033;} :; )elected l$%. ;: When setting the style for se#eral lin$ states, there are some order rules:
a:ho#er :FST come after a:lin$ and a:#isited a:acti#e :FST come after a:ho#er
Text *ecorat$o%
The text-decoration property is mostly used to remo#e underlines from lin$s: Example a:l$%. {text&decorat$o%:%o%e;} a:+$)$ted {text&decorat$o%:%o%e;} a:ho+er {text&decorat$o%:u%derl$%e;} a:act$+e {text&decorat$o%:u%derl$%e;}
4ac.grou%d Color
The bac$ground-color property specifies the bac$ground color for lin$s: Example a:l$%. {bac.grou%d&color:#4233CC;} a:+$)$ted {bac.grou%d&color:#3333> ;} a:ho+er {bac.grou%d&color:#33?0-*;} a:act$+e {bac.grou%d&color:#33?0-*;}
CSS B$)t)
2n T:<, there are two types of lists: unordered lists - the list items are mar$ed with bullets ordered lists - the list items are mar$ed with numbers or letters With CSS, lists can be styled further, and images can be used as the list item mar$er. The CSS list properties allow us to: Set different list item mar$ers for ordered lists Set different list item mar$ers for unordered lists Set an image as the list item mar$er #ll CSS B$)t 2ropert$e)
2roperty l$)t&)tyle l$)t&)tyle&$mage l$)t&)tyle&po)$t$o% *e)cr$pt$o% Sets all the properties for a list in one declaration Specifies an image as the list-item mar$er Specifies if the list-item mar$ers should appear inside or outside the content flow
l$)t&)tyle&type
The example abo#e does not display e5ually in all browsers. 2! and 9pera will display the image-mar$er a little bit higher than 1irefox, Chrome, and Safari. 2f you want the image-mar$er to be placed e5ually in all browsers, a crossbrowser solution is explained below.
Cro))bro/)er Solut$o%
The following example displays the image-mar$er e5ually in all browsers: Example ul { l$)t&)tyle&type: %o%e; padd$%g: 0px; marg$%: 0px; } l$ { bac.grou%d&$mage: url()7purple'g$f"; bac.grou%d&repeat: %o&repeat; bac.grou%d&po)$t$o%: 0px px; padd$%g&left: 1-px; } Example expla$%ed: 1or ul: a. Set the list-style-type to none to remo#e the list item mar$er b. Set both padding and margin to 'px ,for cross-browser compatibility. 1or li: a. Set the F)< of the image, and show it only once ,no-repeat.
b. 7osition the image where you want it ,left 'px and down -px. c. 7osition the text in the list with padding-left
CSS Table)
The loo$ of an T:< table can be greatly impro#ed with CSS: Compa%y 6lfreds 1utter$iste +erglunds snabb$Gp !rnst andel 2sland Trading Co%tact :aria 6nders Cou%try *ermany :exico 6ustria FH
Table 4order)
To specify table borders in CSS, use the border property. The example below specifies a blac$ border for table, th, and td elements: Example table, th, td = border: 1px solid blac$> ? 0otice that the table in the example abo#e has double borders. This is because both the table, th, and td elements ha#e separate borders. To display a single border for the table, use the border-collapse property.
Collap)e 4order)
The border-collapse property sets whether the table borders are collapsed into a single border or separated:
Example
table = border-collapse:collapse> ? table,th, td = border: 1px solid blac$> ?
Example
table = width:1''I> ? th = height:-'px> ?
The text-align property sets the hori3ontal alignment, li$e left, right, or center:
Example
td = text-align:right> ? The #ertical-align property sets the #ertical alignment, li$e top, bottom, or middle:
Example
td = height:-'px> #ertical-align:bottom> ?
Table 2add$%g
To control the space between the border and content in a table, use the padding property on td and th elements: Example td = padding:1-px> ?
Table Color
The example below specifies the color of the borders, and the text and bac$ground color of th elements: Example table, td, th = border:1px solid green> ? th = bac$ground-color:green> color:white> ?
Darg$% - Clears an area around the border. The margin does not ha#e a bac$ground color, it is completely transparent 4order - 6 border that goes around the padding and content. The border is affected by the bac$ground color of the box 2add$%g - Clears an area around the content. The padding is affected by the bac$ground color of the box Co%te%t - The content of the box, where text and images appear
2n order to set the width and height of an element correctly in all browsers, you need to $now how the box model wor$s.
Example
width:(('px> padding:1'px> border:-px solid gray> margin:'px> The total width of an element should always be calculated li$e this: Total element width D width K left padding K right padding K left border K right border K left margin K right margin The total height of an element should always be calculated li$e this: Total element height D height K top padding K bottom padding K top border K bottom border K top margin K bottom margin
Example
@EC9CTM7! html 7F+<2C %-88W/C88CTC " T:< 1.' Transitional88!0% %http:88www.w/.org8T)8xhtml18CTC8xhtml1-transitional.dtd%A @htmlA @headA @style typeD%text8css%A di#.ex = width:(('px> padding:1'px> border:-px solid gray> margin:'px> ? @8styleA @8headA
CSS 4order
CSS +order 7roperties
The CSS border properties allow you to specify the style and color of an elementJs border.
+order Style
The border-style property specifies what $ind of border to display. 0one of the border properties will ha#e 60M effect unless the border-style property is setE
border&)tyle +alue):
none: Cefines no border dotted: Cefines a dotted border dashed: Cefines a dashed border solid: Cefines a solid border double: Cefines two borders. The width of the two borders are the same as the border-width #alue groo#e: Cefines a /C groo#ed border. The effect depends on the border-color #alue ridge: Cefines a /C ridged border. The effect depends on the border-color #alue inset: Cefines a /C inset border. The effect depends on the border-color #alue
outset: Cefines a /C outset border. The effect depends on the border-color #alue
4order 9$dth
The border-width property is used to set the width of the border. The width is set in pixels, or by using one of the three pre-defined #alues: thin, medium, or thic$. 6ote: The %border-width% property does not wor$ if it is used alone. Fse the %border-style% property to set the borders first.
Example
p.one = border-style:solid> border-width:-px> ? p.two =
border-style:solid> border-width:medium> ?
4order Color
The border-color property is used to set the color of the border. The color can be set by:
name - specify a color name, li$e %red% )*+ - specify a )*+ #alue, li$e %rgb,(--,','.% ex - specify a hex #alue, li$e %&ff''''%
Mou can also set the border color to %transparent%. 6ote: The %border-color% property does not wor$ if it is used alone. Fse the %border-style% property to set the borders first.
Example
p.one = border-style:solid> border-color:red> ? p.two = border-style:solid> border-color:&NLbf(1> ?
Example
p = border-top-style:dotted> border-right-style:solid> border-bottom-style:dotted> border-left-style:solid> ? The example abo#e can also be set with a single property:
Example
border-style:dotted solid>
border&)tyle:dotted )ol$d double da)hed; o top border is dotted o right border is solid o bottom border is double o left border is dashed border&)tyle:dotted )ol$d double; o top border is dotted o right and left borders are solid o bottom border is double border&)tyle:dotted )ol$d; o top and bottom borders are dotted o right and left borders are solid border&)tyle:dotted; o all four borders are dotted
The border-style property is used in the example abo#e. owe#er, it also wor$s with borderwidth and border-color.
Example
border:-px solid red> When using the border property, the order of the #alues are:
2t does not matter if one of the #alues abo#e are missing ,although, border-style is re5uired., as long as the rest are in the specified order.
border-bottom border-bottom-color border-bottom-style border-bottom-width border-color border-left border-left-color border-left-style border-left-width border-right border-right-color border-right-style border-right-width border-style border-top border-top-color border-top-style border-top-width border-width
Sets all the bottom border properties in one declaration Sets the color of the bottom border Sets the style of the bottom border Sets the width of the bottom border Sets the color of the four borders Sets all the left border properties in one declaration Sets the color of the left border Sets the style of the left border Sets the width of the left border Sets all the right border properties in one declaration Sets the color of the right border Sets the style of the right border Sets the width of the right border Sets the style of the four borders Sets all the top border properties in one declaration Sets the color of the top border Sets the style of the top border Sets the width of the top border Sets the width of the four borders
CSS Darg$%
The margin clears an area around an element ,outside the border.. The margin does not ha#e a bac$ground color, and is completely transparent. The top, right, bottom, and left margin can be changed independently using separate properties. 6 shorthand margin property can also be used, to change all margins at once.
2o))$ble Jalue)
Jalue *e)cr$pt$o% The browser sets the margin. auto The result of this is dependant of the browser length Cefines a fixed margin ,in pixels, pt, em, etc.. % Cefines a margin in I of the containing element 2t is possible to use negati#e #alues, to o#erlap content.
marg$%:2 px 0px ? px 100px; o top margin is (-px o right margin is -'px o bottom margin is O-px o left margin is 1''px marg$%:2 px 0px ? px; o top margin is (-px o right and left margins are -'px o bottom margin is O-px marg$%:2 px 0px; o top and bottom margins are (-px o right and left margins are -'px marg$%:2 px; o all four margins are (-px
CSS 2add$%g
The padding clears an area around the content ,inside the border. of an element. The padding is affected by the bac$ground color of the element. The top, right, bottom, and left padding can be changed independently using separate properties. 6 shorthand padding property can also be used, to change all paddings at once.
2o))$ble Jalue)
Jalue length % *e)cr$pt$o% Cefines a fixed padding ,in pixels, pt, em, etc.. Cefines a padding in I of the containing element
padd$%g:2 px 0px ? px 100px; o top padding is (-px o right padding is -'px o bottom padding is O-px o left padding is 1''px
padd$%g:2 px 0px ? px; o top padding is (-px o right and left paddings are -'px o bottom padding is O-px padd$%g:2 px 0px; o top and bottom paddings are (-px o right and left paddings are -'px padd$%g:2 px; o all four paddings are (-px
6e)t$%g Selector)
2t is possible to apply a style for a selector within a selector. 2n the example below, one style is specified for all p elements, one style is specified for all elements with classD%mar$ed%, and a third style is specified only for p elements with classD%mar$ed%: Example p= color:blue> text-align:center> ? .mar$ed= bac$ground-color:red> ? .mar$ed p = color:white> ?
Example
h1.hidden =#isibility:hidden>? display:none hides an element, and it will not ta$e up any space. The element will be hidden, and the page will be displayed as the element is not there:
Example
h1.hidden =display:none>?
@spanA @aA
CSS 2o)$t$o%$%g
*ec$de /h$ch eleme%t to d$)play $% fro%tK Eleme%t) ca% o+erlapK
2o)$t$o%$%g
The CSS positioning properties allow you to position an element. 2t can also place an element behind another, and specify what should happen when an elementJs content is too big. !lements can be positioned using the top, bottom, left, and right properties. owe#er, these properties will not wor$ unless the position property is set first. They also wor$ differently depending on the positioning method. There are four different positioning methods.
Stat$c 2o)$t$o%$%g
T:< elements are positioned static by default. 6 static positioned element is always positioned according to the normal flow of the page. Static positioned elements are not affected by the top, bottom, left, and right properties.
3$xed 2o)$t$o%$%g
6n element with fixed position is positioned relati#e to the browser window. 2t will not mo#e e#en if the window is scrolled: Example p'po)Lf$xed { po)$t$o%:f$xed; top:,0px; r$ght: px; } 6ote: 2!O and 2!L support the fixed #alue only if a EC9CTM7! is specified. 1ixed positioned elements are remo#ed from the normal flow. The document and other elements beha#e li$e the fixed positioned element does not exist. 1ixed positioned elements can o#erlap other elements.
8elat$+e 2o)$t$o%$%g
6 relati#e positioned element is positioned relati#e to its normal position. Example h2'po)Lleft{ po)$t$o%:relat$+e; left:&20px; } h2'po)Lr$ght{ po)$t$o%:relat$+e; left:20px; } The content of relati#ely positioned elements can be mo#ed and o#erlap other elements, but the reser#ed space for the element is still preser#ed in the normal flow. Example h2'po)Ltop { po)$t$o%:relat$+e; top:& 0px; } )elati#ely positioned elements are often used as container bloc$s for absolutely positioned elements.
#b)olute 2o)$t$o%$%g
6n absolute position element is positioned relati#e to the first parent element that has a position other than static. 2f no such element is found, the containing bloc$ is @htmlA: Example h2{ po)$t$o%:ab)olute; left:100px; top:1 0px; } 6bsolutely positioned elements are remo#ed from the normal flow. The document and other elements beha#e li$e the absolutely positioned element does not exist. 6bsolutely positioned elements can o#erlap other elements.
H+erlapp$%g Eleme%t)
When elements are positioned outside the normal flow, they can o#erlap other elements. The 3-index property specifies the stac$ order of an element ,which element should be placed in front of, or behind, the others.. 6n element can ha#e a positi#e or negati#e stac$ order: Example $mg { po)$t$o%:ab)olute; left:0px; top:0px; 0&$%dex:&1 } 6n element with greater stac$ order is always in front of an element with a lower stac$ order. 6ote: 2f two positioned elements o#erlap, without a 3-index specified, the element positioned last in the T:< code will be shown on top.
CSS 3loat
With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it. 1loat is #ery often used for images, but it is also useful when wor$ing with layouts.
1or aligning text, see the CSS Text chapter. 2n this chapter we will show you how to hori3ontally align bloc$ elements for layout purposes.
= margin:'> padding:'> ? .container = position:relati#e> width:1''I> ? .right = position:absolute> right:'px> width:/''px> bac$ground-color:&b'e'eB> ?
CSS 2)eudo&cla))e)
CSS pseudo-classes are used to add special effects to some selectors.
Sy%tax
The syntax of pseudo-classes: )elector:p)eudo&cla)) {property:+alue;} CSS classes can also be used with pseudo-classes: )elector'cla)):p)eudo&cla)) {property:+alue;}
#%chor 2)eudo&cla))e)
<in$s can be displayed in different ways in a CSS-supporting browser: Example a:l$%. {color:#330000;} :; u%+$)$ted l$%. ;: a:+$)$ted {color:#003300;} :; +$)$ted l$%. ;: a:ho+er {color:#330033;} :; mou)e o+er l$%. ;: a:act$+e {color:#000033;} :; )elected l$%. ;: 6ote: a:ho#er :FST come after a:lin$ and a:#isited in the CSS definition in order to be effecti#eEE 6ote: a:acti#e :FST come after a:ho#er in the CSS definition in order to be effecti#eEE 6ote: 7seudo-class names are not case-sensiti#e.
p:first-child = color:blue> ? @8styleA @8headA @bodyA @pA2 am a strong man.@8pA @pA2 am a strong man.@8pA @8bodyA @8htmlA
@8headA @bodyA @pA2 am a @iAstrong@8iA man. 2 am a @iAstrong@8iA man.@8pA @pA2 am a @iAstrong@8iA man. 2 am a @iAstrong@8iA man.@8pA @8bodyA @8htmlA
CSS 2)eudo&eleme%t)
CSS pseudo-elements are used to add special effects to some selectors.
Sy%tax
The syntax of pseudo-elements: selector:pseudo-element =property:#alue>? CSS classes can also be used with pseudo-elements: selector.class:pseudo-element =property:#alue>?
font properties color properties bac$ground properties word-spacing letter-spacing text-decoration #ertical-align text-transform line-height clear
font properties color properties bac$ground properties margin properties padding properties border properties text-decoration #ertical-align ,only if %float% is %none%. text-transform line-height float clear
Dult$ple 2)eudo&eleme%t)
Se#eral pseudo-elements can also be combined. 2n the following example, the first letter of a paragraph will be red, in an xx-large font si3e. The rest of the first line will be blue, and in small-caps. The rest of the paragraph will be the default font si3e and color: Example p:first-letter = color:&ff''''> font-si3e:xx-large> ? p:first-line = color:&''''ff> font-#ariant:small-caps> ?
The following example inserts an image before each @h1A element: Example h1:before = content:url,smiley.gif.> ?
6 na#igation bar needs standard T:< as a base. 2n our examples we will build the na#igation bar from a standard T:< list. 6 na#igation bar is basically a list of lin$s, so using the @ulA and @liA elements ma$es perfect sense: Example @ulA @liA@a hrefD%default.asp%A ome@8aA@8liA @liA@a hrefD%news.asp%A0ews@8aA@8liA @liA@a hrefD%contact.asp%AContact@8aA@8liA @liA@a hrefD%about.asp%A6bout@8aA@8liA @8ulA 0ow letJs remo#e the bullets and the margins and padding from the list: Example ul = list-style-type:none> margin:'> padding:'> ? !xample explained:
list-style-type:none - )emo#es the bullets. 6 na#igation bar does not need list mar$ers Setting margins and padding to ' to remo#e browser default settings
The code in the example abo#e is the standard code used in both #ertical, and hori3ontal na#igation bars.
display:bloc$ - Cisplaying the lin$s as bloc$ elements ma$es the whole lin$ area clic$able ,not 4ust the text., and it allows us to specify the width
width:B'px - +loc$ elements ta$e up the full width a#ailable by default. We want to specify a B' px width
6ote: 6lways specify the width for @aA elements in a #ertical na#igation bar. 2f you omit the width, 2!B can produce unexpected results.
display:inline> - +y default, @liA elements are bloc$ elements. ere, we remo#e the line brea$s before and after each list item, to display them on one line
float:left - use float to get bloc$ elements to slide next to each other display:bloc$ - Cisplaying the lin$s as bloc$ elements ma$es the whole lin$ area clic$able ,not 4ust the text., and it allows us to specify the width width:B'px - Since bloc$ elements ta$e up the full width a#ailable, they cannot float next to each other. We specify the width of the lin$s to B'px
1mage 5allery
The following image gallery is created with CSS:
Example
@htmlA @headA @style typeD%text8css%A di#.img = margin:(px> border:1px solid &''''ff> height:auto> width:auto> float:left> text-align:center> ? di#.img img = display:inline> margin:/px>
border:1px solid &ffffff> ? di#.img a:ho#er img = border:1px solid &''''ff> ? di#.desc = text-align:center> font-weight:normal> width:1('px> margin:(px> ? @8styleA @8headA @bodyA @di# classD%img%A @a targetD%Pblan$% hrefD%$lematisPbig.htm%A @img srcD%$lematisPsmall.4pg% altD%Hlematis% widthD%11'% heightD%N'% 8A @8aA @di# classD%desc%A6dd a description of the image here@8di#A @8di#A @di# classD%img%A @a targetD%Pblan$% hrefD%$lematis(Pbig.htm%A @img srcD%$lematis(Psmall.4pg% altD%Hlematis% widthD%11'% heightD%N'% 8A @8aA @di# classD%desc%A6dd a description of the image here@8di#A @8di#A @di# classD%img%A @a targetD%Pblan$% hrefD%$lematis/Pbig.htm%A @img srcD%$lematis/Psmall.4pg% altD%Hlematis% widthD%11'% heightD%N'% 8A @8aA @di# classD%desc%A6dd a description of the image here@8di#A @8di#A @di# classD%img%A @a targetD%Pblan$% hrefD%$lematisRPbig.htm%A @img srcD%$lematisRPsmall.4pg% altD%Hlematis% widthD%11'% heightD%N'% 8A @8aA @di# classD%desc%A6dd a description of the image here@8di#A @8di#A @8bodyA @8htmlA
6ote: This is not yet a CSS standard. owe#er, it wor$s in all modern browsers, and is a part of the W/C CSS / recommendation.
<oo$ at the following source code: @img srcD%$lematis.4pg% widthD%1-'% heightD%11/% altD%$lematis% styleD%opacity:'.R>filter:alpha,opacityDR'.% 8A 1irefox uses the property opac$ty:x for transparency, while 2! uses f$lter:alpha(opac$ty=x". T$p: The CSS/ syntax for transparency is opac$ty:x. 2n 1irefox ,opacity:x. x can be a #alue from '.' - 1.'. 6 lower #alue ma$es the element more transparent. 2n 2! ,filter:alpha,opacityDx.. x can be a #alue from ' - 1''. 6 lower #alue ma$es the element more transparent.
The source code loo$s li$e this: @img srcD%$lematis.4pg% styleD%opacity:'.R>filter:alpha,opacityDR'.% onmouseo#erD%this.style.opacityD1>this.filters.alpha.opacityD1''% onmouseoutD%this.style.opacityD'.R>this.filters.alpha.opacityDR'% 8A @img srcD%$lematis(.4pg% styleD%opacity:'.R>filter:alpha,opacityDR'.% onmouseo#erD%this.style.opacityD1>this.filters.alpha.opacityD1''% onmouseoutD%this.style.opacityD'.R>this.filters.alpha.opacityDR'% 8A We see that the first line of the source code is similar to the source code in !xample 1. 2n addition, we ha#e added an onmouseo#er attribute and an onmouseout attribute. The onmouseo#er attribute defines what will happen when the mouse pointer mo#es o#er the image. 2n this case we want the image to 09T be transparent when we mo#e the mouse pointer o#er it. The syntax for this in 1irefox is: th$)')tyle'opac$ty=1 and the syntax in 2! is: th$)'f$lter)'alpha'opac$ty=100. When the mouse pointer mo#es away from the image, we want the image to be transparent again. This is done in the onmouseout attribute.
height:1L'px> margin:/'px -'px> bac$ground-color:&ffffff> border:1px solid blac$> 8S for 2! S8 filter:alpha,opacityDB'.> 8S CSS/ standard S8 opacity:'.B> ? di#.transbox p = margin:/'px R'px> font-weight:bold> color:&''''''> ? @8styleA @8headA @bodyA @di# classD%bac$ground%A @di# classD%transbox%A @pAThis is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. This is some text that is placed in the transparent box. @8pA @8di#A @8di#A @8bodyA @8htmlA 1irst, we create a di# element ,classD%bac$ground%. with a fixed height and width, a bac$ground image, and a border. Then we create a smaller di# ,classD%transbox%. inside the first di# element. The %transbox% di# ha#e a fixed width, a bac$ground color, and a border and it is transparent. 2nside the transparent di#, we add some text inside a p element.
2nstead of using three separate images, we use this single image ,%imgPna#sprites.gif%.: With CSS, we can show 4ust the part of the image we need. 2n the following example the CSS specifies which part of the %imgPna#sprites.gif% image to show: Example img.home = width:RBpx> height:RRpx> bac$ground:url,imgPna#sprites.gif. ' '> ? Example expla$%ed:
@img classD%home% srcD%imgPtrans.gif% 8A - 9nly defines a small transparent image because the src attribute cannot be empty. The displayed image will be the bac$ground image we specify in CSS width:RBpx>height:RRpx> - Cefines the portion of the image we want to use bac$ground:url,imgPna#sprites.gif. ' '> - Cefines the bac$ground image and its position ,left 'px, top 'px.
This is the easiest way to use image sprites, now we want to expand it by using lin$s and ho#er effects.
Example
&na#list=position:relati#e>? &na#list li=margin:'>padding:'>list-style:none>position:absolute>top:'>? &na#list li, &na#list a=height:RRpx>display:bloc$>? &home=left:'px>width:RBpx>? &home=bac$ground:url,JimgPna#sprites.gifJ. ' '>? &pre#=left:B/px>width:R/px>? &pre#=bac$ground:url,JimgPna#sprites.gifJ. -ROpx '>? &next=left:1(Npx>width:R/px>? &next=bac$ground:url,JimgPna#sprites.gifJ. -N1px '>? Example expla$%ed:
&na#list=position:relati#e>? - position is set to relati#e to allow absolute positioning inside it &na#list li=margin:'>padding:'>list-style:none>position:absolute>top:'>? - margin and padding is set to ', list-style is remo#ed, and all list items are absolute positioned &na#list li, &na#list a=height:RRpx>display:bloc$>? - the height of all the images are RRpx
&home=left:'px>width:RBpx>? - 7ositioned all the way to the left, and the width of the image is RBpx &home=bac$ground:url,imgPna#sprites.gif. ' '>? - Cefines the bac$ground image and its position ,left 'px, top 'px. &pre#=left:B/px>width:R/px>? - 7ositioned B/px to the right ,&home width RBpx K some extra space between items., and the width is R/px. &pre#=bac$ground:url,JimgPna#sprites.gifJ. -ROpx '>? - Cefines the bac$ground image ROpx to the right ,&home width RBpx K 1px line di#ider. &next=left:1(Npx>width:R/px>?- 7ositioned 1(Npx to the right ,start of &pre# is B/px K &pre# width R/px K extra space., and the width is R/px. &next=bac$ground:url,JimgPna#sprites.gifJ. no-repeat -N1px '>? - Cefines the bac$ground image N1px to the right ,&home width RBpx K 1px line di#ider K &pre# width R/px K 1px line di#ider .
Since the list item contains a lin$, we can use the :ho#er pseudo-class &home a:ho#er=bac$ground: transparent url,imgPna#spritesPho#er.gif. ' -R-px>? 1or all three ho#er images we specify the same bac$ground position, only R-px further down
Ded$a Type)
Some CSS properties are only designed for a certain media. 1or example the %#oice-family% property is designed for aural user agents. Some other properties can be used for different media types. 1or example, the %font-si3e% property can be used for both screen and print media, but perhaps with different #alues. 6 document usually needs a larger font-si3e on a screen than on paper, and sans-serif fonts are easier to read on the screen, while serif fonts are easier to read on paper.
See it yourself E 2f you are using :o3illa81irefox or 2!-K and print this page, you will see that the paragraph under %:edia Types% will be displayed in another font, and ha#e a smaller font si3e than the rest of the text.