25
Cascading Style Sheets

Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Embed Size (px)

Citation preview

Page 1: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Cascading Style Sheets

Page 2: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Cascading Style Sheets

• Defines the presentation of one or more web pages

• Similar to a template

• Can control the appearance of an entire web site giving a consistent look

• Allow mass changes to entire site by changing the style

Page 3: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Cascading Style Sheets

• Multiple sheets can influence a single page

• Sheets can cascade effects to other sheets, each adding new features

• Complete specification can be downloaded at:WWW.W3.ORG

Page 4: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Types of Styles

• Inline Styles– Styles are added directly to the HTML tag

• Global Styles– Style is applied to the entire HTML document

• External Style Sheet– Styles are kept in a separate document with

the .CSS extension– Multiple pages can share the style sheet

Page 5: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Inline Styles

• A style can be specified directly on an HTML tag without referring to a separate style sheet

<P style="color: red"> this approach allows use of the expanded parameters of a style not usually associated with an

HTML tag </P>

Page 6: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Global Style Definition

• To hide from older browsers that don’t support, enclose in a comment tag:<style type=“text/css”><! - -

style sheet rules here

- - ></style>

Page 7: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Style Rules

• Style is a collection of rules• Rules have the syntax:

selector { styles }

• Selector is the tag to be modified– example <P>

• Styles are the properties that are being specified – example COLOR

Page 8: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Example Change all paragraphs to be blue and be italicized

<HTML><style type=“text/css”>P { color: blue; font-style: italic }</style>

This is a demo of a style.<P> All paragraphs will now have blue,

italics</P><P> It affects the entire document </P></HTML>

Page 9: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Selectors

• Type Selectors– Identify the entire class of HTML tags to be

modified

• Id Selectors– Identify a name that can be used to specify

selectively which HTML tags are modified

Page 10: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Selectors

• Context Selectors– Identifies HTML tags to be modified within the

context of other HTML tags

• Class Selectors– Creates a class that various HTML tags can

belong to based on the CLASS parameter

Page 11: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Type Selector

• Example: Change Ordered and Unordered List Items to use italics and bold

<style type=“text/css”>UL, OL { font-style: italic; font-weight:

bold}</style>

Page 12: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

ID Selector

<style type=“text/css”>#MyParaA { font-style: italic; font-weight:

bold}#MyParaB { color: red}</style><P id= “MyParaA” > </P><P id= “MyParaB” > </P>

Page 13: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Context Selector

<style type=“text/css”>UL LI { color: red}OL LI { color: blue }</style>

• Line items in an unordered list will be red

• Line items in an ordered list will be blue

Page 14: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Class Selector

<style type=“text/css”>.MyClass { font-style: italic; font-weight:

bold}.MyRedClass { color: red}</style><P CLASS= MyRedClass > </P><P CLASS= MyClass > </P><TABLE CLASS=MyRedClass><TD CLASS=MyClass> </TD> ...

Page 15: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Combination Selector

<style type=“text/css”>LI.MyRedClass { color: red}</style><OL ><LI CLASS= MyRedClass > this will only

affect the LI tags that use this class<LI> this won’t be part of the class</OL>• Other type tags using the class won’t be affected

Page 16: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

<div> and <span>

• Create an area on the HTML page that can cover multiple tags

• A <div> is separated from adjacent areas by a line break

• A <span> can be in the middle of text

• Both can be modified by styles

Page 17: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

<SPAN>

<style type=“text/css”>span { color: red}</style>this is before the span <span> this is

inside the span <P> other tags can be inside a span</P>

this is still inside the span</span> this is after the span

Page 18: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

<DIV>

<style type=“text/css”>div { color: red}</style>this is before the div <div> this is inside

the div <P> other tags can be inside a div</P>

this is still in the div</div> this is after the span and will be

separated by a line break

Page 19: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Parent Styles are Inherited by Children

<BODY>

<P><TABLE>

<TR>

<TD>

<OL>

<LI>

•Children’s styles will override their parents

Page 20: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Styles In Separate Documents

• Allows multiple HTML pages to share

<LINK REL=“STYLESHEET” HREF=styleSheetURL>

• Place inside <HEAD> </HEAD> tags

• URL should refer to a file with an extension of .CSS

Page 21: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Styles In Separate Documents

• CSS document contains only the definitions of the style rules ( i.e. no <STYLE> tags)

P { color:red }

Page 22: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Linking with @import

• Alternative method to reference external style sheet

• Microsoft Extension• CSS can include styles from another CSS

<STYLE>

@import url(stylesheet.css)

additional styles

</STYLE>

Page 23: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Style Precedence

• Inline Style Overrides Global Style

• Global Style Overrides External Style Sheet

• Style Sheets that Reference Other Style Sheets Override the Referenced Sheets

• Anything Not Specified Defaults to the Browser Settings

Page 24: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Absolute Positioning

• Styles can be used to positioning an element on a page in an exact position, instead of the normal “flow” of HTML

• POSITION: absolute• TOP: top of element• LEFT: left edge of element• Z-INDEX: Layer effect -- higher indexes

appear in front lower indexes

Page 25: Cascading Style Sheets. Defines the presentation of one or more web pages Similar to a template Can control the appearance of an entire web site giving

Absolute Positioning

style="POSITION: absolute; LEFT: 127px; TOP: 253px; Z-INDEX: 100”

• This style would place the element’s upper left corner 252pixels from the top, and 127 pixels from the left edge of the window.

• It would appear on top of any other element in the same location with an index < 100