Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS Cascading Style Sheets Cascading Style Sheets 1.

Similar presentations


Presentation on theme: "CSS Cascading Style Sheets Cascading Style Sheets 1."— Presentation transcript:

1 CSS Cascading Style Sheets Cascading Style Sheets 1

2 2 Style Sheets Describe the evolution of style sheets from print media to the webDescribe the evolution of style sheets from print media to the web List advantages of using cascading style sheetsList advantages of using cascading style sheets Create style sheets that configure common page and text propertiesCreate style sheets that configure common page and text properties Use inline stylesUse inline styles Use embedded style sheetsUse embedded style sheets Use external style sheetsUse external style sheets Use pseudo-classesUse pseudo-classes

3 Overview of Cascading Style Sheets (CSS) See what is possible with CSS: See what is possible with CSS: Visit Style Sheets Style Sheets used for years in Desktop Publishingused for years in Desktop Publishing apply typographical styles and spacing to printed mediaapply typographical styles and spacing to printed media CSS CSS provides the functionality of style sheets (and much more) for web developersprovides the functionality of style sheets (and much more) for web developers a flexible, cross-platform, standards-based language developed by the W3C.a flexible, cross-platform, standards-based language developed by the W3C. 3

4 4 CSS Advantages Greater typography and page layout control Greater typography and page layout control Style is separate from structure Style is separate from structure Styles can be stored in a separate document and linked to from the web page Styles can be stored in a separate document and linked to from the web page Potentially smaller documents Potentially smaller documents No need for tags No need for tags Easier site maintenance Easier site maintenance

5 5 Types of Cascading Style Sheets Inline Styles Inline Styles Inline styles are coded in the body of the web page as an attribute of an XHTML tag. The style only applies to the specific element that contains it as an attributeInline styles are coded in the body of the web page as an attribute of an XHTML tag. The style only applies to the specific element that contains it as an attribute Embedded Styles Embedded Styles Embedded styles are defined in the header of a web page. These style instructions apply to the entire web page document.Embedded styles are defined in the header of a web page. These style instructions apply to the entire web page document. External Styles External Styles External Styles are coded in a separate text file. This text file is linked to the web page by using a tag in the header section.External Styles are coded in a separate text file. This text file is linked to the web page by using a tag in the header section. Imported Styles Imported Styles Imported Styles are similar to External Styles in that they are coded in a separate text file. We’ll concentrate on the other types of styles in this text.Imported Styles are similar to External Styles in that they are coded in a separate text file. We’ll concentrate on the other types of styles in this text.

6 6 CSS Syntax Style sheets are composed of "Rules" that describe the styling to be applied. Style sheets are composed of "Rules" that describe the styling to be applied. Each Rule contains a Selector and a Declaration Each Rule contains a Selector and a Declaration

7 7 CSS Syntax Sample body { color: blue; background-color: yellow; } background-color: yellow; } This could also be written using hexadecimal color values as shown below. body { color: #0000FF; background-color: #FFFF00; } background-color: #FFFF00; }

8 8 Common Formatting CSS Properties See Table 9.1 Common CSS Properties, including: See Table 9.1 Common CSS Properties, including: background-colorbackground-color colorcolor font-familyfont-family font-sizefont-size font-weightfont-weight font-stylefont-style text-decorationtext-decoration line-heightline-height text-aligntext-align background-imagebackground-image

9 9 Using Inline Styles Inline Styles are coded as attributes on XHTML tags. Inline Styles are coded as attributes on XHTML tags. The following code will set the text color of a tag to a shade of red: The following code will set the text color of a tag to a shade of red: This is displayed as a red heading This is displayed as a red heading The following code sets the text in the heading to red and italic. The following code sets the text in the heading to red and italic. This is displayed as a red heading in italic style This is displayed as a red heading in italic style

10 Hexadecimal Color Values # is used to indicate a hexadecimal value # is used to indicate a hexadecimal value Hex value pairs range from 00 to FF Hex value pairs range from 00 to FF Three hex value pairs describe an RGB color Three hex value pairs describe an RGB color # black#FFFFFF white #FF0000 red#00FF00 green #0000FF blue#CCCCCC grey 10

11 Configuring Color with Inline CSS (1) Inline CSS Inline CSS Configured in the body of the Web pageConfigured in the body of the Web page Use the style attribute of an XHTML tagUse the style attribute of an XHTML tag Apply only to the specific elementApply only to the specific element The Style Attribute The Style Attribute Value: one or more style declaration property and value pairsValue: one or more style declaration property and value pairs Example: configure red color text in an element: Heading text is red Heading text is red 11

12 Configuring Color with Inline CSS (2) Example 2: configure the red text in the heading configure a gray backgroundin the heading configure a gray backgroundin the heading Separate style rule declarations with ; This is displayed as a red heading with gray background This is displayed as a red heading with gray background 12

13 13 Questions 1. List three reasons to use CSS on a web page. 2. When designing a page to use other than the default colors for text and background, explain why it is a good reason to configure both properties: text color and background color. 3. Write the code to configure an inline style attached to a tag. Configure as follows: background color set to a light green color, text set to a dark green color, font set to Arial or sans-serif, size set to larger, font weight set to bold.

14 CSS Embedded Styles Configured in the header section of a Web page. Configured in the header section of a Web page. Use the XHTML element Use the XHTML element Apply to the entire Web page document Apply to the entire Web page document Style declarations are contained between the opening and closing tags Style declarations are contained between the opening and closing tags The type attribute indicates the MIME type of text/css The type attribute indicates the MIME type of text/css Example: Configure a Web page with white text on a black background Example: Configure a Web page with white text on a black background 14 body { background-color: #000000; color: #FFFFFF; } body { background-color: #000000; color: #FFFFFF; }

15 15 Embedded Styles Apply to an entire web page. Apply to an entire web page. Placed within a tag located in the header section of a web page. Placed within a tag located in the header section of a web page. The opening tag begins the embedded style rules. The opening tag begins the embedded style rules. The closing tag ends the area containing embedded style rules. The closing tag ends the area containing embedded style rules. When using the tag, there is no need for the style attribute. When using the tag, there is no need for the style attribute. The tag does use a type attribute that should have the value of "text/css". The tag does use a type attribute that should have the value of "text/css". body { background-color: #000000; color: #FFFFFF; font-family:Arial,sans-serif; }

16 CSS Embedded Styles body { background-color: #E6E6FA; color: #191970;} color: #191970;} h1 { background-color: #191970; color: #E6E6FA;} color: #E6E6FA;} h2 { background-color: #AEAED4; color: #191970;} color: #191970;} The body selector sets the global style rules for the entire page. These global rules are overridden for and elements by the h1 and h2 style rules.

17 CSS and text 17

18 Configuring Text with CSS CSS properties for configuring text: CSS properties for configuring text: font-weightfont-weight Configures the boldness of text Configures the boldness of text font-stylefont-style Configures text to an italic style Configures text to an italic style font-sizefont-size Configures the size of the text Configures the size of the text font-familyfont-family Configures the font typeface of the text Configures the font typeface of the text

19 The font-size Property Accessibility Recommendation: Accessibility Recommendation: ◦Use em or percentage font sizes – these can be easily enlarged in all browsers by users

20 The font-family Property Not everyone has the same fonts installed in their computer Not everyone has the same fonts installed in their computer Configure a list of fonts and include a generic family name Configure a list of fonts and include a generic family name p {font-family: Arial,Verdana, sans-serif;}

21 Embedded Styles Example body { background-color: #E6E6FA; color: #191970; font-family: Arial, Verdana, sans-serif; } h1 { background-color: #191970; color: #E6E6FA; line-height: 200%; font-family: Georgia, "Times New Roman", serif; } h2 { background-color: #AEAED4; color: #191970; font-family: Georgia, "Times New Roman", serif; } p {font-size:.90em; } ul {font-weight: bold; }

22 CSS Selectors CSS Selectors CSS style rules can be configured for an: HTML element selector (i.e. as we saw in previous slide to the element itself)HTML element selector (i.e. as we saw in previous slide to the element itself) class selectorclass selector id selectorid selector

23 Using CSS with “class” class Selector class Selector Apply a CSS rule to a certain "class" of elements on a Web pageApply a CSS rule to a certain "class" of elements on a Web page Does not associate the style to a particular XHTML elementDoes not associate the style to a particular XHTML element Configure with.classname Configure with.classname The sample creates a class called “new” with red italic text. The sample creates a class called “new” with red italic text. To use the class, code the following XHTML: To use the class, code the following XHTML: This is text is red and in italics This is text is red and in italics 23.new { color: #FF0000; font-style: italic; }

24 Using CSS with “id” id Selector id Selector Apply a CSS rule to ONE element on a Web page.Apply a CSS rule to ONE element on a Web page. Configure with #idname Configure with #idname The sample creates an id called “new” with red, large, italic text. The sample creates an id called “new” with red, large, italic text. To use the id, code the following XHTML: To use the id, code the following XHTML: This is text is red, large, and in italics This is text is red, large, and in italics 24 #new { color: #FF0000; font-size:2em; font-style: italic; }

25 CSS and styling “areas” 25

26 XHTML element A block-level element A block-level element Purpose: configure a specially formatted division or area of a Web page Purpose: configure a specially formatted division or area of a Web page There is a line break before and after the division.There is a line break before and after the division. Can contain other block-level and inline elementsCan contain other block-level and inline elements Useful to define an area that will contain other block-level tags (such as paragraphs or spans) within it. Useful to define an area that will contain other block-level tags (such as paragraphs or spans) within it. 26

27 XHTML Element Example Configure a page footer area Configure a page footer area Embedded CSS: Embedded CSS:.footer { font-size: small; text-align: center; } text-align: center; } XHTML: XHTML: Copyright © 2009 Copyright ©

28 XHTML element An inline-level element An inline-level element Purpose: Purpose: configure a specially formatted area displayed in-line with other elements, such as within a paragraph.configure a specially formatted area displayed in-line with other elements, such as within a paragraph. There is no line break before and after the span. There is no line break before and after the span. 28

29 XHTML Element Example Embedded CSS: Embedded CSS:.companyname { font-weight: bold; font-family: Georgia, "Times New Roman", serif; font-size: 1.25em; } XHTML: XHTML: Your needs are important to us at Acme Web Design. We will work with you to build your Web site. Your needs are important to us at Acme Web Design. We will work with you to build your Web site. 29

30 External Style Sheets Storing in separate file – decoupling design from content!!! Storing in separate file – decoupling design from content!!! 30

31 External Style Sheets CSS style rules are contained in a text file separate from the XHTML documents. CSS style rules are contained in a text file separate from the XHTML documents. The External Style Sheet text file: The External Style Sheet text file: extension ".css"extension ".css" contains only style rulescontains only style rules does not contain any XHTML tagsdoes not contain any XHTML tags 31

32 body {background- color:#E6E6FA; color:#000000; font-family:Arial, sans-serif; font-size:90%; } h2 { color: #003366; }.nav { font-size: 16px; font-weight: bold; } body {background- color:#E6E6FA; color:#000000; font-family:Arial, sans-serif; font-size:90%; } h2 { color: #003366; }.nav { font-size: 16px; font-weight: bold; } External Style Sheets Multiple web pages can associate with the same external style sheet file.Multiple web pages can associate with the same external style sheet file. 32 site.css index.htm clients.htm about.htm Etc…

33 The Element A self-contained tag A self-contained tag Placed in the header section Placed in the header section Purpose: associates the external style sheet file with the web page. Purpose: associates the external style sheet file with the web page. Example: Example: 33

34 Using an External Style Sheet To link to the external style sheet called color.css, the XHTML code placed in the header section is: body { background-color: #0000FF; color: #FFFFFF; } External Style Sheet color.css

35 Centering Page Content with CSS #container { margin-left: auto; margin-right: auto; margin-right: auto; width:80%; } width:80%; }

36 W3C CSS Validation

37 CSS Guidelines – Getting Started Review the design of the page Review the design of the page Configure global font and color properties for the body selectorConfigure global font and color properties for the body selector Identify typical elements (such as,, and so on) and declare style rules for these if needed. Identify typical elements (such as,, and so on) and declare style rules for these if needed. Identify page areas such as logo, navigation, footer, and so on – configure an appropriate class or id for each. Identify page areas such as logo, navigation, footer, and so on – configure an appropriate class or id for each. Create one prototype page that contains most of the elements you plan to use and test. Create one prototype page that contains most of the elements you plan to use and test. Revise your CSS as needed.Revise your CSS as needed. Once your design is set – move styles to an external.css fileOnce your design is set – move styles to an external.css file Planning and testing are important activities when designing a Web site Planning and testing are important activities when designing a Web site

38 CSS Troubleshooting Tips Verify you are using the : and ; symbols in the right spots—they are easy to confuse. Verify you are using the : and ; symbols in the right spots—they are easy to confuse. Check that you are not using = signs instead of : between each property and its value. Check that you are not using = signs instead of : between each property and its value. Verify that the { and } symbols are properly placed Verify that the { and } symbols are properly placed Check the syntax of your selectors, their properties, and property values for correct usage. Check the syntax of your selectors, their properties, and property values for correct usage. If part of your CSS works, and part doesn’t: If part of your CSS works, and part doesn’t: ◦Review your CSS ◦Determine the first rule that is not applied. Often the error is in the rule above the rule that is not applied. Validate your CSS at Validate your CSS at

39 This “cascade” applies the styles in order from outermost (External Styles) to innermost (actual XHTML coded on the page). This “cascade” applies the styles in order from outermost (External Styles) to innermost (actual XHTML coded on the page). This way site-wide styles can be configured but overridden when needed by more granular (or page specific) styles. This way site-wide styles can be configured but overridden when needed by more granular (or page specific) styles. The Cascade

40 CSS and anchor tag Style attributes differently Style attributes differently Use to make buttons Use to make buttons 40

41 41 CSS Pseudo-classes Pseudo-classes and the anchor tag Pseudo-classes and the anchor tag Link – default state for a link (anchor tag)Link – default state for a link (anchor tag) Visited – state of a link that has been visitedVisited – state of a link that has been visited Hover – state of a link that the mouse is currently overHover – state of a link that the mouse is currently over Active – state of a link that is being clickedActive – state of a link that is being clicked a:link {color:#FF0000 } a:hover {text-decoration:none; color:# }

42 42 CSS “buttons”.button { border: 2px inset #cccccc; width: 100px; width: 100px; padding: 3px 15px; padding: 3px 15px; color: #ffffff; color: #ffffff; background-color: #006600; background-color: #006600; font-family: Arial, Helvetica, sans-serif; font-family: Arial, Helvetica, sans-serif; font-size: 16px; font-size: 16px; font-weight: bold; font-weight: bold; text-align: center; text-align: center; text-decoration:none; text-decoration:none;} a.button:link { color : #FFFFFF; } a.button:visited { color : #cccccc; } a.button:hover { color : #66cc33; border:2px outset #cccccc; border:2px outset #cccccc;} CSS Buttons! Home Products Services Contact About

43 CSS – more to do Effecting borders Effecting borders 43

44 The CSS border Property Configures a border on the top, right, bottom, and left sides of an element Configures a border on the top, right, bottom, and left sides of an element Consists of Consists of border-widthborder-width border-styleborder-style border-colorborder-color h2 { border: 2px solid #ff0000 } h2 { border: 2px solid #ff0000 }

45 CSS Borders: Block / Inline Elements Block element Block element ◦ default width of element content extends to browser margin (or specified width) Inline element Inline element ◦Border closely outlines the element content h2 { border: 2px solid #ff0000; } a { border: 2px solid #ff0000; } a { border: 2px solid #ff0000; }

46 Browser Display Can Vary

47 Configuring Specific Sides of a Border Use CSS to configure a line on one or more sides of an element Use CSS to configure a line on one or more sides of an element border-bottomborder-bottom border-leftborder-left border-rightborder-right border-topborder-top h2 { border-bottom: 2px solid #ff0000 } h2 { border-bottom: 2px solid #ff0000 }

48 The CSS padding Property Configures empty space between the content of the XHTML element and the border Configures empty space between the content of the XHTML element and the border Set to 0px by default Set to 0px by default h2 { border: 2px solid #ff0000; h2 { border: 2px solid #ff0000; padding: 5px; } padding: 5px; } No padding configured:

49 Configuring Padding on Specific Sides of an Element Use CSS to configure padding on one or more sides of an element Use CSS to configure padding on one or more sides of an element padding-bottompadding-bottom padding-leftpadding-left padding-rightpadding-right padding-toppadding-top h2 { border: 2px solid #ff0000; h2 { border: 2px solid #ff0000; background-color: #cccccc; background-color: #cccccc; padding-left: 5px; padding-left: 5px; padding-bottom: 10px; padding-bottom: 10px; padding-top: 10px;} padding-top: 10px;}

50 CSS padding Property Shorthand: two values Two numeric values or percentages Two numeric values or percentages first value configures top and bottom paddingfirst value configures top and bottom padding the second value configures left and right paddingthe second value configures left and right padding h2 { border: 2px solid #ff0000; background-color: #cccccc; background-color: #cccccc; padding: 20px 10px; padding: 20px 10px;}

51 Four numeric values or percentages Four numeric values or percentages ◦Configure top, right, bottom, and left padding h2 { border: 2px solid #ff0000; width: 250px; width: 250px; background-color: #cccccc; background-color: #cccccc; padding: 30px 10px 5px 20px; padding: 30px 10px 5px 20px;} CSS padding Property Shorthand: four values

52 Hands-On Practice h1 { background-color:#191970; color:#E6E6FA; color:#E6E6FA; padding: 15px; padding: 15px; font-family: Georgia, "Times New Roman", serif; } font-family: Georgia, "Times New Roman", serif; } h2 { background-color:#AEAED4; color:#191970; color:#191970; font-family: Georgia, "Times New Roman", serif; font-family: Georgia, "Times New Roman", serif; border-bottom: 2px dashed #191970; } border-bottom: 2px dashed #191970; } Try this on your own

53 CSS – more to do Background images Background images 53

54 CSS background-image Property Configures a background-image Configures a background-image By default, background images tile (repeat) By default, background images tile (repeat) body { background-image: url(background1.gif); }

55 CSS background-repeat Property

56 Using background-repeat h2 { background-color: #d5edb3; color: #5c743d; font-family: Georgia, "Times New Roman", serif; padding-left: 30px; background-image: url(trilliumbullet.gif); background-image: url(trilliumbullet.gif); background-repeat: no-repeat; background-repeat: no-repeat;} trilliumbullet.gif:

57 57 CSS Strategies(1) Always include end tags (even though browsers usually display the page, anyway) for all XHTML container tags. Always include end tags (even though browsers usually display the page, anyway) for all XHTML container tags. Design and code the page to look "OK" or "Acceptable" -- then use style sheets for extra-special effects and formatting. Design and code the page to look "OK" or "Acceptable" -- then use style sheets for extra-special effects and formatting. Use style sheet components that will degrade gracefully. Check the compatibility charts and test, test, test, test, test.... Use style sheet components that will degrade gracefully. Check the compatibility charts and test, test, test, test, test....

58 58 CSS Strategies(2) Use and tags to create logical page sections. Be aware that Netscape 4.x handles the tag better than the tag. Use and tags to create logical page sections. Be aware that Netscape 4.x handles the tag better than the tag. Use style sheets in Intranet environments -- you know exactly what browsers your visitors will be using. Use style sheets in Intranet environments -- you know exactly what browsers your visitors will be using. Consider using a browser detection script (discussed in Chapter 12) to test for a specific browser and link to the style sheet coded specifically for that browser. Consider using a browser detection script (discussed in Chapter 12) to test for a specific browser and link to the style sheet coded specifically for that browser.


Download ppt "CSS Cascading Style Sheets Cascading Style Sheets 1."

Similar presentations


Ads by Google