CSS n Cascading Style Sheets 1 Style Sheets

  • Slides: 59
Download presentation
CSS n Cascading Style Sheets 1

CSS n Cascading Style Sheets 1

Style Sheets • Describe the evolution of style sheets from print media to the

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 2

Overview of Cascading Style Sheets (CSS) n See what is possible with CSS: •

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

CSS Advantages n n n Greater typography and page layout control Style is separate

CSS Advantages n n n 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 4

Types of Cascading Style Sheets n Inline Styles • Inline styles are coded in

Types of Cascading Style Sheets n 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 n Embedded Styles • Embedded styles are defined in the header of a web page. These style instructions apply to the entire web page document. n 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. n Imported Styles • Imported Styles are similar to External Styles in that they are 5 coded in a separate text file. We’ll concentrate on the other types of styles in this text.

CSS Syntax n n Style sheets are composed of "Rules" that describe the styling

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

CSS Syntax Sample body { color: blue; background-color: yellow; } This could also be

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

Common Formatting CSS Properties n See Table 9. 1 Common CSS Properties, including: •

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

Using Inline Styles are coded as attributes on XHTML tags. n The following code

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

Hexadecimal Color Values # is used to indicate a hexadecimal value Hex value pairs

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 #000000 #FF 0000 #0000 FF black red blue #FFFFFF white #00 FF 00 green #CCCCCC grey 10

Configuring Color with Inline CSS (1) n Inline CSS • Configured in the body

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

Configuring Color with Inline CSS (2) Example 2: configure the red text in the

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 ; <h 1 style="color: #FF 0000; background-color: #cccccc">This is displayed as a red heading with gray background</h 1> 12

Questions 1. 2. 3. List three reasons to use CSS on a web page.

Questions 1. 2. 3. 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. 13

CSS Embedded Styles n n n Configured in the header section of a Web

CSS Embedded Styles n n n 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> 14

<style type="text/css"> body { background-color: #000000; color: #FFFFFF; font-family: Arial, sans-serif; } </style> n

<style type="text/css"> body { background-color: #000000; color: #FFFFFF; font-family: Arial, sans-serif; } </style> n n n 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 15 that should have the value of "text/css".

CSS Embedded Styles • The body selector sets the global style rules for the

CSS Embedded Styles • The body selector sets the global style rules for the entire page. • These global rules are overridden for <h 1> and <h 2> elements by the h 1 and h 2 style rules. <style type="text/css"> body { background-color: #E 6 E 6 FA; color: #191970; } h 1 { background-color: #191970; color: #E 6 E 6 FA; } h 2 { background-color: #AEAED 4; color: #191970; } </style>

CSS and text 17

CSS and text 17

Configuring Text with CSS n CSS properties for configuring text: • font-weight n Configures

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

The font-size Property Accessibility Recommendation: ◦ Use em or percentage font sizes – these

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

The font-family Property n n Not everyone has the same fonts installed in their

The font-family Property n n 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; }

Embedded Styles Example <style type="text/css"> body { background-color: #E 6 E 6 FA; color:

Embedded Styles Example <style type="text/css"> body { background-color: #E 6 E 6 FA; color: #191970; font-family: Arial, Verdana, sans-serif; } h 1 { background-color: #191970; color: #E 6 E 6 FA; line-height: 200%; font-family: Georgia, "Times New Roman", serif; } h 2 { background-color: #AEAED 4; color: #191970; font-family: Georgia, "Times New Roman", serif; } p {font-size: . 90 em; } ul {font-weight: bold; } </style>

CSS Selectors CSS style rules can be configured for an: • HTML element selector

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

Using CSS with “class” n class Selector • Apply a CSS rule to a

Using CSS with “class” n class Selector • Apply a CSS rule to a certain "class" of elements on a Web page • Does not associate the style to a particular XHTML element n n n <style type="text/css">. new { color: #FF 0000; font-style: italic; } </style> 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> 23

Using CSS with “id” n id Selector • Apply a CSS rule to ONE

Using CSS with “id” n id Selector • Apply a CSS rule to ONE element on a Web page. n n n Configure with #idname <style type="text/css"> #new { color: #FF 0000; font-size: 2 em; font-style: italic; } </style> 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> 24

Why ID and class n n SPECIAL question ---why both id and classname used

Why ID and class n n 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 n Classes are NOT unique • You can use the same class on multiple elements. • You can use multiple classes on the same element. 25

CSS and styling “areas” 26

CSS and styling “areas” 26

XHTML <div> element n n A block-level element Purpose: configure a specially formatted division

XHTML <div> element n n 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 n Useful to define an area that will contain other block-level tags (such as paragraphs or spans) within it. 27

XHTML <div> Element Example n n Configure a page footer area Embedded CSS: <style

XHTML <div> Element Example n n 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> n 28

XHTML <span> element n n An inline-level element Purpose: • configure a specially formatted

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

XHTML <span> Element Example n Embedded CSS: <style type="text/css">. companyname { font-weight: bold; font-family:

XHTML <span> Element Example n Embedded CSS: <style type="text/css">. companyname { font-weight: bold; font-family: Georgia, "Times New Roman", serif; font-size: 1. 25 em; } </style> n 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> 30

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

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

External Style Sheets n n CSS style rules are contained in a text file

External Style Sheets n n 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 32

External Style Sheets • Multiple web pages can associate with the same external style

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

The <link /> Element n n A self-contained tag Placed in the header section

The <link /> Element n n 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" /> 34

Using an External Style Sheet color. css body { background-color: #0000 FF; color: #FFFFFF;

Using an External Style Sheet color. css body { background-color: #0000 FF; 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" />

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

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

W 3 C CSS Validation n http: //jigsaw. w 3. org/css-validator/

W 3 C CSS Validation n http: //jigsaw. w 3. org/css-validator/

CSS Guidelines – Getting Started n Review the design of the page • Configure

CSS Guidelines – Getting Started n Review the design of the page • Configure global font and color properties for the body selector • Identify typical elements (such as <h 1>, <h 3>, 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. n 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 n Planning and testing are important activities when designing a Web site

CSS Troubleshooting Tips Verify you are using the : and ; symbols in the

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 http: //jigsaw. w 3. org/css-validator

The Cascade n n This “cascade” applies the styles in order from outermost (External

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

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

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

<style type=”text/css> a: link {color: #FF 0000 } a: hover {text-decoration: none; color: #000066

<style type=”text/css> a: link {color: #FF 0000 } a: hover {text-decoration: none; color: #000066 } </style> n CSS 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 42

<style type="text/css">. button { border: 2 px inset #cccccc; width: 100 px; padding: 3

<style type="text/css">. button { border: 2 px inset #cccccc; width: 100 px; padding: 3 px 15 px; color: #ffffff; background-color: #006600; font-family: Arial, Helvetica, sans-serif; font-size: 16 px; font-weight: bold; text-align: center; text-decoration: none; } a. button: link { color : #FFFFFF; } a. button: visited { color : #cccccc; } a. button: hover { color : #66 cc 33; border: 2 px outset #cccccc; <div align="center"> } <h 2>CSS Buttons!</h 2> </style> <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> CSS “buttons” 43

CSS – more to do n Effecting borders 44

CSS – more to do n Effecting borders 44

The CSS border Property n n Configures a border on the top, right, bottom,

The CSS border Property n n Configures a border on the top, right, bottom, and left sides of an element Consists of • • • border-width border-style border-color h 2 { border: 2 px solid #ff 0000 }

CSS Borders: Block / Inline Elements Block element ◦ default width of element content

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 h 2 { border: 2 px solid #ff 0000; } a { border: 2 px solid #ff 0000; }

Browser Display Can Vary

Browser Display Can Vary

Configuring Specific Sides of a Border n Use CSS to configure a line on

Configuring Specific Sides of a Border n Use CSS to configure a line on one or more sides of an element • • border-bottom border-left border-right border-top h 2 { border-bottom: 2 px solid #ff 0000 }

The CSS padding Property n n Configures empty space between the content of the

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

Configuring Padding on Specific Sides of an Element n Use CSS to configure padding

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

CSS padding Property Shorthand: two values n Two numeric values or percentages • first

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

CSS padding Property Shorthand: four values Four numeric values or percentages ◦ Configure top,

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

Hands-On Practice Try this on your own h 1 { background-color: #191970; color: #E

Hands-On Practice Try this on your own h 1 { background-color: #191970; color: #E 6 E 6 FA; padding: 15 px; font-family: Georgia, "Times New Roman", serif; } h 2 { background-color: #AEAED 4; color: #191970; font-family: Georgia, "Times New Roman", serif; border-bottom: 2 px dashed #191970; }

CSS – more to do n Background images 54

CSS – more to do n Background images 54

CSS background-image Property n n Configures a background-image By default, background images tile (repeat)

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

CSS background-repeat Property

CSS background-repeat Property

Using background-repeat trilliumbullet. gif: h 2 { background-color: #d 5 edb 3; color: #5

Using background-repeat trilliumbullet. gif: h 2 { background-color: #d 5 edb 3; color: #5 c 743 d; font-family: Georgia, "Times New Roman", serif; padding-left: 30 px; background-image: url(trilliumbullet. gif); background-repeat: no-repeat; }

CSS Strategies(1) n n n Always include end tags (even though browsers usually display

CSS Strategies(1) n n n 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. . 58

CSS Strategies(2) n n n Use <div> and <span> tags to create logical page

CSS Strategies(2) n n n 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. 59