2001 Prentice Hall, Inc. All rights reserved. Declared.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.2: declared.html --!---;ؒ.;ig;-16.;. 6;.-16;.12:; d-1;.1e;la-;.1;re-1;.1d;.htm;-16.;l ;ê.1 ;ÿ.1 ; -;.1; ;-16.; ;ê.1 ;-16.; ;---1;.10; !--Declaring a style sheet in the header section. --!---;ؒ.;鷬-16.;lar;i-16;.1ng; a-1;.1 ;sty-;.1;le-1;.1 ;shee;-16.;t i;n-16;.1 t;he-1;.1 ;hea-;.1;Þr ;-16.;sec;t-16;.1io;-16.;n. ;---1;.10; htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Styles placed in the head apply to all elements in the document. A style class named special is created. Style classes inherit the More style properties include font type ( font-family ) and background color ( background-color Use the style element to create an embedded CSS. 2001 Prentice Hall, Inc. All rights reserved. Declared.html body-16.;od;y-16;.100; !--This class attribute applies the .blue style --!-16;.1--;j.8T-;.1;his ;-16.;la;s-16;.1s ;-16.;tt;ri-1;.1b;ute-;.1; app;-16.;lie;s-16;.1 t;he-1;.1 ;.bl-;.1;ue-1;.1 ;styl;-16.; -;--16;.100; h1 class = "special" Deitel& Associates, Inc. -16.;/h1;/h1 pp-16;.100; Deitel& Associates, Inc. is an internationally recognized corporate training and publishing organization The styles associated with the special class are applied to the header and paragraph elements. 2001 Prentice Hall, Inc. All rights reserved. Program Output Notice the styles defined in the CSS are applied to all paragraphs, headers, and bolded text. Elements that have the special class applied have the class’s styles as well as the CSS styles applied. 2001 Prentice Hall, Inc. All rights reserved. Advanced.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig 6.3: advanced.html !---;ؒ.;ig;-16.; 6.;-16;.1: ;-1;.1v; nc-;.1;í-1;.1.;html;-16.; ;ê.1 -; -- A style class is defined specifically for elements. The style is applied if the element’s class attribute is set to nodec hover psuedoclass defined for the element is activated dynamically when the user rolls over the element with a mouse. element for elements is defined to have bold red font. Elements of an unordered list in another nested unordered list will be underlined and have a left-hand margin of 15 pixels. 2001 Prentice Hall, Inc. All rights reserved. Advanced.html uu-16;.1l0;l lili-1;.10; Milk /l/l-1;.1i;i lili-1;.10; Bread -16.;li0;li White bread -16.;/li;/li -16.;li0;li Rye bread /l/l-1;.1i;i -16.;li0;li Whole wheat bread /l/l-1;.1i;i /ul/ul-;.1; /l/l-1;.1i;i lili-1;.10; Rice /l/l-1;.1i;i lili-1;.10; Potatoes -16.;/li;/li lili-1;.10; Pizza eถ.;m00;m with mushrooms //-16;.1em;em-16.;/li;/li //-16;.1ul;ul pp-16;.100;a class = "nodec" href= "http://www.food.com" Go to the Grocery store //-16;.1a0;a//-16;.1p0;p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml According to the CSS defined, the three elements listed under bread should be underlined and indented 15 pixels from the left margin when rendered. The text placed between the tags should be bolded and colored red when rendered. When the user scrolls over the text anchoring the link, the hover class will be activated and the text will change to the style defined by the hover class. 2001 Prentice Hall, Inc. All rights reserved. Program Output The elements in the nested unordered list are underlined and indented 15 pixels from the left margin. By applying the hover psuedoclass to this anchor, the anchor’s background and text color changes when the use r rolls over it with the mouse. 2001 Prentice Hall, Inc. All rights reserved. Styles.css /* Fig. 6.4: styles.css */ 2001 Prentice Hall, Inc. All rights reserved. External.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.5: external.html !---;ؒ.;ig;-16.;. 6;.-16;.15:; e-1;.1x;ter-;.1;na-1;.1l;.htm;-16.;l ;ê.1 ;ÿ.1 ;--00; -- element is used to reference an external style sheet. rel attribute is used to define the linking relationship. type attribute defines the MIME type. 2001 Prentice Hall, Inc. All rights reserved. External.html Program Output pp-16;.100; ahref= "http://www.food.com" Go to the Grocery store -16.;/a0;/a //-16;.1p0;p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml The documents rendered with an external CSS should be the same as those rendered with an internal CSS. 2001 Prentice Hall, Inc. All rights reserved. 6.6 W3C CSS Validation Service Fig. 6.6Validating a CSS doc ument. (Courtesy of World Wide Web Consortium (W3C).) 2001 Prentice Hall, Inc. All rights reserved. 6.6 W3C CSS Validation Service Fig. 6.7CSS validation results. (Courtesy of World Wide Web Consortium (W3C).) 2001 Prentice Hall, Inc. All rights reserved. Positioning.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig 6.8: positioning.html --!---;ؒ.;ig;-16.; 6.;-16;.1: ;po-1;.1s;iti-;.1;on-1;.1i;ng.h;-16.;tml;ê.1 ;ÿ.1 ; --;.1; !--Absolute positioning of elements --!---;ؒ.;ফs;-16.;olu;t-16;.1e ;po-1;.1s;iti-;.1;on-1;.1i;ng o;-16.;ǰ ;l-1;.1e;men-;.1;ts -;-16.;-00; htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Absolute Positioning /titl/-16;.1ti;tl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; pp-16;.100;img src= "i.gif" style = "position: absolute; top: 0px; left: 0px; z-index: 1" alt = "First positioned image" / /p/p-1;.10; p style = "position: absolute; top: 50px; left: 50px; z-index: 3; font-size: 20pt;" Positioned Text pp-16;.100;img src= "circle.gif" style = "position: absolute; top: 25px; left: 100px; z-index: 2 alt = "Second positioned image" ///-16;.1p0;p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml position property of the style element allows for positioning of an element. z-index property allows layering of multiple images.The images are layered such that images with lower z-indexes are placed under images with higher ones. 2001 Prentice Hall, Inc. All rights reserved. Program Output The effect of the z-index property is several images layered on top of one another. 2001 Prentice Hall, Inc. All rights reserved. Positioning2.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.9: positioning2.html !---;ؒ.;ig;-16.;. 6;.-16;.19:; p-1;.1o;sit-;.1;io-1;.1n;ing2;-16.;.ht;m-16;.1l ;ÿ.1 ; -;.1;--00;-- !--Relative positioning of elements !---;ؒ.; Rel;-16.;ti;v-16;.1e ;po-1;.1s;iti-;.1;on-1;.1i;ng o;-16.;ǰ ;l-1;.1e;men-;.1;ts ;-16.;--0;-- htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Relative Positioning /titl/-16;.1ti;tl-1;.1e;e style type = "text/css" font-size: 1.3em font-family: verdana,arial, sans-serif span color: red font-size: .6em height: .super position: relative -1ex .sub position: relative bottom: .shiftleft position: relative left: -1ex .shiftright position: relative right: -1ex /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d Relative positioning places an element relative to a reference point on the page. For instance, when this class is applied the element will be displayed –1cm relative to the left of where it would be placed. 2001 Prentice Hall, Inc. All rights reserved. Positioning2.htm Program Output body-16.;od;y-16;.100; pp-16;.100; The text at the end of this sentence span class = "super" is in superscript /sp/-16;.1sp;-16.;n0;an -16.;/p0;/p pp-16;.100; The text at the end of this sentence span class = "sub" is in subscript /span/-16;.1sp; n-1;.10; pp-16;.100; The text at the end of this sentence span class = "shiftleft" is shifted left //-16;.1sp; n00;span /p/p-1;.10; pp-16;.100; The text at the end of this sentence span class = "shiftright" is shifted right /spa/spa;-16.;n00;n -16.;/p0;/p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml The text in red has the shiftleft class applied, and is shifted left relative to where it would have initially been placed. 2001 Prentice Hall, Inc. All rights reserved. Background.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.10: background.html --!---;ؒ.;ig;-16.;. 6;.-16;.110;: -1;.1b;¬k-;.1;gr-1;.1o;und.;-16.;htm;l-16;.1 ;ÿ.1 ; -;.1; ;-16.; ;--16;.1-0; !--Adding background images and indentation --!---;ؒ.;髝-16.;ing;ê.1ba;k-1;.1g;rou-;.1;nd-1;.1 ;imag;-16.;s ; -16;.1nd; i-1;.1n;Þn-;.1;tati;-16.;on ;--16;.1-0; htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Background Images /tit-16.;/ti;t-16;.1le;le style type = "text/css" body background-image: url(logo.gif) background-position: bottom right background-repeat: no-repeat background-attachment: fixed p font-size: 18pt color: #aa5588 text-indent: 1em font-family: arial, sans-serif .dark font-weight: bold /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d background-image property assigns a background to the body of the page. background–position property assigns a location for the image on the page. If set to repeat , the background- repeat property will tile the image as the background. The value of the background- attachment property determines if the image moves as the user scrolls the page. The font format specified will be applied to all elements. 2001 Prentice Hall, Inc. All rights reserved. Background.html rogram Output body-16.;od;y-16;.100; pp-16;.100; This example uses the background-image, background-position and background-attachment styles to place the span class = "dark" Deitel & Associates, Inc. /s/s-1;.1p; n00;pan logo in the bottom, right corner of the page. Notice how the logo stays in the proper position when you resize the browser window. //-16;.1p0;p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml Note that no formatting needed to be set in the element itself because it has already been defined in the CSS. 2001 Prentice Hall, Inc. All rights reserved. Width.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.11: width.html --!---;ؒ.;ig;-16.;. 6;.-16;.111;: -1;.1w;idt-;.1;h.-1;.1h;tml ;-16.; ;ê.1 ;ÿ.1 ; -;.1; ;-16.; ;ê.1--;-16.;က width height attributes of style allow the user to indicate the percentage of the width and height of the screen the element can occupy. 2001 Prentice Hall, Inc. All rights reserved. Width.html Program Output div style = "width: 20%; height: 30%; overflow: scroll" This box is only twenty percent of the width and thirty percent of the height. What do we do if it overflows? Set the overflow property to scroll! /div-16.;/di;v-16;.100; /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml This will set the overflow attribute to add scroll bars for text that overflows. 2001 Prentice Hall, Inc. All rights reserved. Floating.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.12: floating.html --!---;ؒ.;ig;-16.;. 6;.-16;.112;: -1;.1f;loa-;.1;ti-1;.1n;g.ht;-16.;ml ;ê.1 ;ÿ.1 ; -;.1; --;-16.;က !--Floating elements and element boxes --!---;ؒ.;lo;-16.;ti;n-16;.1g ;l-1;.1e;men-;.1;ts-1;.1 ; nd ;-16.;le;m-16;.1en;t -1;.1b;oxe-;.1;s --;-16.;က htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Flowing Text Around Floating Elements /ti/ti-;.1;tle0;tle style type = "text/css" background-color: #ffccff margin-bottom: .5em font-size: 1.5em width: text-align: justify /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; div style = "text-align: center" Deitel& Associates, Inc. /div-16.;/di;v-16;.100; div style = "float: right; margin: .5em; text-align: right" Corporate Training and Publishing /div-16.;/di;v-16;.100; margin property specifies the distance between the edge of the element and any other element on the page. The float propertyallows you to move an element to one side of the screen such that other content in the document then flows around the floated element. 2001 Prentice Hall, Inc. All rights reserved. Floating.html pp-16;.100; Deitel& Associates, Inc. is an internationally recognized corporate training and publishing organization clear property can be used to interrupt the flow of text around a floating element. 2001 Prentice Hall, Inc. All rights reserved. Program Output A floating element with 5 em padding. 2001 Prentice Hall, Inc. All rights reserved. 6.10 Test Flow and Box Model Margin Border Padding Fig. 6.13Box model for block-level elements. 2001 Prentice Hall, Inc. All rights reserved. Borders.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.14: borders.html !---;ؒ.;ig;-16.;. 6;.-16;.114;: -1;.1b;ord-;.1;r-1;.1s;.htm;-16.;l ;ê.1 ;ÿ.1-; -- border-width property is the width of the border around an element. border-style property is the style of border used. border-color property is the color of the border. 2001 Prentice Hall, Inc. All rights reserved. Borders.html Program Output /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; div class = "thick groove" This text has a border //-16;.1di;v000;div div class = "medium groove" This text has a border /div-16.;/di;v-16;.100; div class = "thin groove" This text has a border /d/d-1;.1i;v000;iv p class = "thin red inset" A thin red line... //-16;.1p0;p p class = "medium blue outset" And a thicker blue line /p/p-1;.10; /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml A sampling of the different types of borders that can be specified. 2001 Prentice Hall, Inc. All rights reserved. Borders2.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.15: borders2.html !---;ؒ.;ig;-16.;. 6;.-16;.115;: -1;.1b;ord-;.1;r-1;.1s;.ht;-16.;ml ;ê.1 -; -- !--Various border-styles !---;ؒ.; Var;-16.;iou;s-16;.1 b;or-1;.1d;r--;.1;st-1;.1y;les ;-16.; ;ê.1 -; -- htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; Borders /title/-16;.1ti;tle-;.1; style type = "text/css" body background-color: #ccffcc text-align: center margin-bottom: .3em width: 50% position: relative left: 25% padding: .3em /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; div style = "border-style: solid" Solid border /div-16.;/di;v-16;.100; div style = "border-style: double" Double border /d/d-1;.1i;v000;iv div style = "border-style: groove" Groove border /d/d-1;.1i;v000;iv div style = "border-style: ridge" Ridge border /div-16.;/di;v-16;.100; div style = Specifying a border directly through the style attribute of the element. 2001 Prentice Hall, Inc. All rights reserved. Program Output 2001 Prentice Hall, Inc. All rights reserved. User_absolute.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.16: user_absolute.html --!---;ؒ.;ig;-16.;. 6;.-16;.116;: -1;.1u;ser-;.1;_a-1;.1b;solu;-16.;te.;h-16;.1tm;l -1;.1 ; ---;.1; !--User styles --!---;ؒ.; Use;-16.;r s;t-16;.1yl;s-1;.1 ; -;.1;ÿ.1 ; ;-16.; ;ê.1 ;ÿ.1 ; ---;.1; htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; User Styles /title/t-1;.1i;tle-;.1; style type = "text/css" .note font-size: /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; pp-16;.100; Thanks for visiting my Web site. I hope you enjoy it. //-16;.1p0;pp class = "note" Please Note: This site will be moving soon. Please check periodically for updates. -16.;/p0;/p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml Author defined style sheets are ones defined within the page. By using absolute measurement ( in this case) the developer will override user defined style sheets. 2001 Prentice Hall, Inc. All rights reserved. Program Output Developer defined class applied to this element decrease the font size of the text. 2001 Prentice Hall, Inc. All rights reserved. Userstyles.css /* Fig. 6.17:userstyles.css*/ User defined style sheets are usually external. A user’s style sheet is not linked to a page, they are set in the browser’s options. 2001 Prentice Hall, Inc. All rights reserved. Setting the user’s style sheet in IE. 2001 Prentice Hall, Inc. All rights reserved. Fig. 6.19Web page with user styles applied. 2001 Prentice Hall, Inc. All rights reserved. User_relative.html ?xmlversion = "1.0" !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" !--Fig. 6.20: user_relative.html --!---;ؒ.;ig;-16.;. 6;.-16;.120;: -1;.1u;ser-;.1;_r-1;.1e;lati;-16.;ve.;h-16;.1tm;l -1;.1 ; ---;.1; !--User styles --!---;ؒ.; Use;-16.;r s;t-16;.1yl;s-1;.1 ; -;.1;ÿ.1 ; ;-16.; ;ê.1 ;ÿ.1 ; ---;.1; htmlxmlns= "http://www.w3.org/1999/xhtml" head-16.;hea; -16;.100; titlet-16;.1it;le-1;.10; User Styles /title/t-1;.1i;tle-;.1; style type = "text/css" .note font-size: .75em /styl/-16;.1st;yl-1;.1e;e /hea-16.;/he; -16;.1d0;d body-16.;od;y-16;.100; pp-16;.100; Thanks for visiting my Web site. I hope you enjoy it. //-16;.1p0;pp class = "note" Please Note: This site will be moving soon. Please check periodically for updates. -16.;/p0;/p /bod-16.;/bo; -16;.1y0;y /ht/ht-;.1;ml00;ml By using relative measurements (in this case, ), the developer will not override user defined style sheet formats. When rendered the font size displayed will be .75 percent times the value of the font size defined in the user style sheet. 2001 Prentice Hall, Inc. All rights reserved. Program Output Output before relative measurement is used to define the font inthe document. 2001 Prentice Hall, Inc. All rights reserved. Fig. 6.21Using relative measurements in author styles. Output when relative measurement is used. By using relative measurements the user defined styles are not overwritten.