CSS Cascading Style Sheets.

Slides:



Advertisements
Similar presentations
CSS Cascading Style Sheets. Objectives Using Inline Styles Working with Selectors Using Embedded Styles Using an External Style Sheet Applying a Style.
Advertisements

Web Development & Design Foundations with XHTML Chapter 3 Key Concepts.
Cascading Style Sheets
Cascading Style Sheets
CSS Cascading Style Sheets Cascading Style Sheets 1.
Today CSS HTML A project.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 6 Key Concepts 1 Copyright © Terry Felke-Morris.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 4 Key Concepts 1 Copyright © Terry Felke-Morris.
Cascading Style Sheets. CSS stands for Cascading Style Sheets and is a simple styling language which allows attaching style to HTML elements. CSS is a.
Web Development & Design Foundations with XHTML Chapter 7 Key Concepts.
Cascading Style Sheets By: Valerie Kuna. What are Cascading Style Sheets? Cascading Style Sheets (CSS) are a standard for specifying the presentation.
Web Development & Design Foundations with XHTML Chapter 7 Key Concepts.
Web Development & Design Foundations with XHTML Chapter 7 Key Concepts.
1 Web Developer & Design Foundations with XHTML Chapter 9 Key Concepts.
CSS BASICS. CSS Rules Components of a CSS Rule  Selector: Part of the rule that targets an element to be styled  Declaration: Two or more parts: a.
Recognizing the Benefits of Using CSS 1. The Evolution of CSS CSS was developed to standardize display information CSS was slow to be supported by browsers.
CSS(Cascading Style Sheets )
Web Pages and Style Sheets Bert Wachsmuth. HTML versus XHTML XHTML is a stricter version of HTML: HTML + stricter rules = XHTML. XHTML Rule violations:
กระบวนวิชา CSS. What is CSS? CSS stands for Cascading Style Sheets Styles define how to display HTML elements Styles were added to HTML 4.0 to.
CSS normally control the html elements. Three Ways to Insert CSS There are three ways of inserting a style sheet: External style sheet Internal style.
Web Development & Design Foundations with XHTML Chapter 3 Key Concepts.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 7 TH EDITION Chapter 3 Key Concepts 1 Copyright © Terry Felke-Morris.
1 Web Developer Foundations: Using XHTML Chapter 9 Cascading Style Sheet Concepts.
Chapter 4 Cascading Style Sheets Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D 1.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 3 Key Concepts 1 Copyright © Terry Felke-Morris.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 3 Key Concepts 1 Copyright © Terry Felke-Morris.
Cascading Style Sheets
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.
CSS Introductions. Objectives To take control of the appearance of a Web site by creating style sheets. To use a style sheet to give all the pages of.
CONFIGURING COLOR AND TEXT WITH CSS Chapter 3. Cascading Style Sheets (CSS) Used to configure text, color, and page layout. Launched in 1996 Developed.
WebD Introduction to CSS By Manik Rastogi.
Cascading Style Sheets
Web Development & Design Foundations with XHTML
Cascading style sheets basics
4.01 Cascading Style Sheets
Web Development & Design Foundations with HTML5 8th Edition
Basics of Web Design Chapter 4 Cascading Style Sheets Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D.
( Cascading style sheet )
Web Development & Design Foundations with HTML5
Introduction to the Internet
Chapter 6 Cascading Style Sheets™ (CSS)
Cascading Style Sheets
Web Developer & Design Foundations with XHTML
Cascading Style Sheets
Basics of Web Design Chapter 4 Cascading Style Sheets Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D.
Copyright (c) 2007 Prentice-Hall. All rights reserved.
Web Development & Design Foundations with HTML5
Web Development & Design Foundations with HTML5 8th Edition
Cascading Style Sheets
IS333: MULTI-TIER APPLICATION DEVELOPMENT
Introduction to CSS.
Web Development & Design Foundations with HTML5 7th Edition
The Internet 10/13/11 The Box Model
Basics of Web Design Chapter 4 Cascading Style Sheets Basics Key Concepts Copyright © 2013 Terry Ann Morris, Ed.D.
CSS Borders and Margins.
Web Programming– UFCFB Lecture 11
What are Cascading Stylesheets (CSS)?
Cascading Style Sheets
Web Development & Design Foundations with H T M L 5
Web Development & Design Foundations with H T M L 5
Tutorial 3 Working with Cascading Style Sheets
Cascading Style Sheets (CSS)
Basics of Web Design Chapter 4 Cascading Style Sheets Basics Key Concepts Copyright © 2016 Terry Ann Morris, Ed.D.
Cascading Style Sheets
4.01 Cascading Style Sheets
Stylin’ with CSS.
Introduction to Cascading Style Sheets (CSS)
Introduction to Styling
Presentation transcript:

CSS Cascading Style Sheets

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

Overview of Cascading Style Sheets (CSS) See what is possible with CSS: Visit http://www.csszengarden.com 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.

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

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.

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

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; }

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

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>

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

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>

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>

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.

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>

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".

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>

CSS and text

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

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 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: #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>

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” 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>

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>

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.

CSS and styling “areas”

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.

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>

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.

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>

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

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

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…

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" />

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" />

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

W3C CSS Validation http://jigsaw.w3.org/css-validator/

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

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.w3.org/css-validator

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.

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

CSS Pseudo-classes Pseudo-classes and the anchor tag <style type=”text/css> a:link {color:#FF0000 } a:hover {text-decoration:none; color:#000066 } </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

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>

CSS – more to do Effecting borders

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 }

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; }

Browser Display Can Vary

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 }

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:

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;}

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; }

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; }

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; }

CSS – more to do Background images

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

CSS background-repeat Property

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; }

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....

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.