Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS Cascading Style Sheets.

Similar presentations


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

1 CSS Cascading Style Sheets

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

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

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

5 Types of Cascading Style Sheets
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 attribute Embedded Styles 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 are coded in a separate text file. This text file is linked to the web page by using a <link> tag in the header section. 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.

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

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

8 Common Formatting CSS Properties
See Table 9.1 Common CSS Properties, including: background-color color font-family font-size font-weight font-style text-decoration line-height text-align background-image

9 Using Inline Styles Inline Styles are coded as attributes on XHTML tags. The following code will set the text color of a <h1> tag to a shade of red: <h1 style="color:#CC0000">This is displayed as a red heading</h1> The following code sets the text in the heading to red and italic. <h1 style="color:#CC0000;font-style:italic">This is displayed as a red heading in italic style</h1>

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

11 Configuring Color with Inline CSS (1)
Configured in the body of the Web page Use the style attribute of an XHTML tag Apply only to the specific element The Style Attribute Value: one or more style declaration property and value pairs Example: configure red color text in an <h1> element: <h1 style="color:#ff0000">Heading text is red</h1>

12 Configuring Color with Inline CSS (2)
Example 2: configure the red text in the heading configure a gray backgroundin the heading Separate style rule declarations with ; <h1 style="color:#FF0000;background-color:#cccccc">This is displayed as a red heading with gray background</h1>

13 Questions List three reasons to use CSS on a web page.
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. Write the code to configure an inline style attached to a <div> 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.
Use the XHTML <style> element Apply to the entire Web page document Style declarations are contained between the opening and closing <style> tags The type attribute indicates the MIME type of text/css Example: Configure a Web page with white text on a black background <style type ="text/css"> body { background-color: #000000; color: #FFFFFF; } </style>

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

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

17 CSS and text

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

19 The font-size Property
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 Configure a list of fonts and include a generic family name p {font-family: Arial,Verdana, sans-serif;}

21 Embedded Styles Example
<style type="text/css"> 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; font-family: Georgia, "Times New Roman", serif; } p {font-size: .90em; } ul {font-weight: bold; } </style>

22 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) class selector id selector

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

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

25 Why ID and class SPECIAL question ---why both id and classname used to define CSS embeded styles: ID's are unique Each element can have only one ID Each page can have only one element with that ID Classes are NOT unique You can use the same class on multiple elements. You can use multiple classes on the same element.

26 CSS and styling “areas”

27 XHTML <div> element
A block-level element Purpose: configure a specially formatted division or area of a Web page There is a line break before and after the division. Can 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.

28 XHTML <div> Element Example
Configure a page footer area Embedded CSS: <style type="text/css"> .footer { font-size: small; text-align: center; } </style> XHTML: <div class=“footer">Copyright © 2009</div>

29 XHTML <span> element
An inline-level element Purpose: 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.

30 XHTML <span> Element Example
Embedded CSS: <style type="text/css"> .companyname { font-weight: bold; font-family: Georgia, "Times New Roman", serif; font-size: 1.25em; } </style> XHTML: <p>Your needs are important to us at <span class=“companyname">Acme Web Design</span>. We will work with you to build your Web site.</p>

31 External Style Sheets Storing in separate file – decoupling design from content!!! NOTE: IDEs like Dreamweaver often do this by default

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

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

34 The <link /> Element
A self-contained tag Placed in the header section Purpose: associates the external style sheet file with the web page. Example: <link rel="stylesheet" href="color.css" type="text/css" />

35 Using an External Style Sheet
External Style Sheet color.css body { background-color: #0000FF; color: #FFFFFF; } To link to the external style sheet called color.css, the XHTML code placed in the header section is: <link rel="stylesheet" href="color.css" type="text/css" />

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

37 W3C CSS Validation

38 CSS Guidelines – Getting Started
Review the design of the page Configure global font and color properties for the body selector Identify typical elements (such as <h1>, <h3>, 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. Create one prototype page that contains most of the elements you plan to use and test. Revise your CSS as needed. Once your design is set – move styles to an external .css file Planning and testing are important activities when designing a Web site

39 CSS Troubleshooting Tips
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. Verify that the { and } symbols are properly placed Check the syntax of your selectors, their properties, and property values for correct usage. 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

40 The Cascade 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.

41 CSS and anchor <a> tag
Style attributes differently Use to make buttons

42 CSS Pseudo-classes Pseudo-classes and the anchor tag
<style type=”text/css> a:link {color:#FF0000 } a:hover {text-decoration:none; color:# } </style> CSS Pseudo-classes Pseudo-classes and the anchor tag Link – default state for a link (anchor tag) Visited – state of a link that has been visited Hover – state of a link that the mouse is currently over Active – state of a link that is being clicked

43 CSS “buttons” <style type="text/css">
.button { border: 2px inset #cccccc; width: 100px; padding: 3px 15px; color: #ffffff; background-color: #006600; font-family: Arial, Helvetica, sans-serif; font-size: 16px; font-weight: bold; text-align: center; text-decoration:none; } a.button:link { color : #FFFFFF; } a.button:visited { color : #cccccc; } a.button:hover { color : #66cc33; border:2px outset #cccccc; </style> CSS “buttons” <div align="center"> <h2>CSS Buttons!</h2> <a href="index.htm" class="button">Home</a> <a href="products.htm" class="button">Products</a> <a href="sevices.htm" class="button">Services</a> <a href="contact.htm" class="button">Contact</a> <a href="about.htm" class="button">About</a> <div>

44 CSS – more to do Effecting borders

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

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

47 Browser Display Can Vary

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

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

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

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

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

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

54 CSS – more to do Background images

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

56 CSS background-repeat Property

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

58 CSS Strategies(1) 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. Use style sheet components that will degrade gracefully. Check the compatibility charts and test, test, test, test, test....

59 CSS Strategies(2) Use <div> and <span> tags to create logical page sections. Be aware that Netscape 4.x handles the <div> tag better than the <span> tag. 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.


Download ppt "CSS Cascading Style Sheets."

Similar presentations


Ads by Google