CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is...

20
CSS Cascading Style Sheets

Transcript of CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is...

Page 1: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

CSSCascading Style Sheets

Page 2: 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 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

• Easier site maintenance

2

Page 3: 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 Syntax SampleConfigure a Web page to display blue text and yellow

background.

body { color: blue; background-color: yellow; }

This could also be written using hexadecimal color values as shown below.

body { color: #0000FF; background-color: #FFFF00; }

3

Page 4: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Web Color Palette

A collection of 216 colors

Display the most similar on the Mac and PCplatforms

Hex values: 00, 33, 66, 99, CC, FF

Color Chart http://webdevfoundations.net/color

4

Page 5: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

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

Page 6: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

External Style Sheets - 1

• 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

6

Page 7: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

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

– Multiple web pages can associate with the same external style sheet file.

7

site.css

index.htmindex.htm

clients.htmclients.htm

about.htmabout.htm

Etc…

Page 8: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

The <link /> Element

• A self-contained tag

• Placed in the header section

• Purpose: associates the external style sheet file with the web page.

• Example:

8

<link rel="stylesheet" href="color.css" type="text/css" />

Page 9: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

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; color: #191970; font-family: Georgia, "Times New Roman", serif; }p {font-size: .90em; }ul {font-weight: bold; }</style>

Page 10: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Using CSS with “class”• class Selector

– Apply a CSSrule to a certain"class" of elementson 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>

10

<style type="text/css">.new { color: #FF0000; font-style: italic; }</style>

Page 11: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Using CSS with “id”• id Selector

– Apply a CSSrule 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>

11

<style type="text/css">#new { color: #FF0000;

font-size:2em; font-style: italic;

}</style>

Page 12: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Using CSS with “id”• id Selector

– Apply a CSSrule 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>

12

<style type="text/css">#new { color: #FF0000;

font-size:2em; font-style: italic;

}</style>

Page 13: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Centering Page Content with CSS#container { margin-left: auto;

margin-right: auto;

width:80%; }

Page 14: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

<body> <div id="container"> <h1>&nbsp;Trillium Media

Design</h1>…<p id="footer">Copyright &copy; 2011 Your

Name Here</p> </div> </body> </html>

Page 15: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

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:

Page 16: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

XHTML Image ElementConfigures graphics on a Web page

src Attribute◦ File name of the graphic

alt Attribute◦ Configures alternate text content (description)

height Attribute◦ Height of the graphic in pixels

width Attribute◦ Width of the graphic in pixels

16

<img src=“cake.gif” alt=“birthday cake” height=“100” width=“100” />

Page 17: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Image LinksTo create an image link use an anchor element to

contain an image element

Browsers automatically add a border to image links.

Configure CSS to eliminate the border img {border:0 }

17

Home

<a href="index.html"><img src="home.gif" height="19" width="85" alt="Home" /></a>

Page 18: 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 background-image Property• Configures a background-image • By default, background images tile (repeat)

body { background-image: url(background1.gif); }

Page 19: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Thumbnail Image

• A small image configured to link to a larger version of that image.

19

Page 20: CSS Cascading Style Sheets. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate.

Favorites Icon - favicon• A square image

associated with a Web page

• Usually named: favicon.ico

• May display in the browser address bar, tab, or favorites/bookmarks list

• Configure with a link tag:

<link rel="icon" href="favicon.ico" type="image/x-icon" />