Cascading Style Sheets
E N D
Presentation Transcript
Cascading Style Sheets Class 1, Lecture 1 Rachel A Ober rober@ccs.neu.edu
What is “CSS”? • CSS stands for “Cascading Style Sheet” • It is used to define how HTML is displayed. • Styles can be stored in external style sheet documents and in the document itself. • Style Sheets were implemented in HTML 4.0 to fix the problems caused by Netscape and Internet Explorer who were coming up with different tags to display content. CSS is a standard developed by the W3C to separate content from display.
Why is CSS Important in Design? • HTML was originally developed so that different sections of the document were divided up with different tags. • The two major browsers, IE and Netscape, constantly added new tags that they supported, such as <font>. • This caused a problem where design in the document could not be separated from the content. • What if I have over 100 pages, and now I need to change one design element that’s hard-coded into each document?
Let’s Compare… • We can accomplish the same design by using HTML tags, OR we can separate the content from design by setting up a style sheet. • What if I have 50 documents and want to change the color of the word “different”?
The HTML… uh, eww? • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> • <html> • <head> • <title>Untitled Document</title> • <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> • </head> • <body bgcolor="#999999" text="#000000" link="#663333" vlink="#FF00FF" alink="#999933" leftmargin="3" topmargin="3" marginwidth="3" marginheight="3"> • <h1><i><font color="#9933CC">This is a Header</font></i></h1> • <p align="center">This is a paragraph with some text.</p> • <p>Hey, I have a <a href="http://www.cnn.com/">link</a>!</p> • <p>What about some <b><font color="#FF0000">bold</font></b> and some <font color="#990099">different</font> • <font color="#0000CC" size="+7">colors</font>.</p> • <table width="75%" border="1" cellspacing="10" cellpadding="10"> • <tr> • <td><p align="center">OOh, <font size="3">what</font> about a table?</p></td> • </tr> • </table> • </body> • </html>
New HTML… • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> • <html> • <head> • <title>Untitled Document</title> • <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> • <link href="example2.css" rel="stylesheet" type="text/css"> • </head> • <body> • <h1>This is a Header</h1> • <p class="center">This is a paragraph with some text.</p> • <p>Hey, I have a <a href="http://www.cnn.com/">link</a>!</p> • <p>What about some <b>bold</b> and some <span class="purple">different</span> • <span class="bigandblue">colors</span>.</p> • <table> • <tr> • <td><p class="center">OOh, <span class="big">what</span> about a table?</p></td> • </tr> • </table> • </body> • </html>
a { color: #663333; } a:visited{ color: #FF00FF; } a:active{ color: #999933; } body { color: #000000; background-color: #999999; margin: 3px; } b { font-weight: bold; color: #FF0000; } h1 { font-style: italic; color: #9933CC; } table { padding: 10px; margin: 10px; width: 75%; border: 10pt solid; } .center { text-align: center; } .purple { color: #990099; } .bigandblue { font-size: 40px; color: #0000CC; } Our Style Sheet All we need to change!
How to Include CSS… • In a separate, linked file included in the header • <link href="example2.css" rel="stylesheet" type="text/css"> • Within the file header • <style type="text/css"> • <!-- • body {font-family: "Times New Roman", Times, serif;} • --> • </style> • Inline • <p style="color: #000000; text-align: center">This is a paragraph with some text.</p>
What Can I Manipulate? • Pre-Defined Tags • <p>, <b>, <i>, etc… • Pseudo-Classes • a:visited{color: #FF00FF;} • Pseudo-Elements • p:first-letter {color: #0000CC;} • Your Own Classes and IDs The “:visited” part is considered a “pseudo-class” The “:first-letter” part defines the first letter of the paragraph. All paragraphs in the doc will have the first letter blue.
Classes Vs. IDs • There are two ways to identify your own created style in a document. • Classes are used for a site-wide style. Perhaps I want a class called “header” that I use on all of my pages. • IDs are used for very specific content. Perhaps I want the second paragraph in “index.html” to have a certain style.
Classes Denoted with a “.” p.center {text-align: center;} <p class=“center”>…</p> IDs Denoted with a “#” p#blue {color: #0000CC;} <p id=“blue”>…<p> How to Use Them Note: Do not start Classes or IDs with a number! It won’t work in FireFox
Lab #1: Recreating Styles • http://cpu.rachelober.com/lab1 • Objective: • Get familiar with styles and how to use them. • Look at the HTML and try to find a way to convert it into a style sheet. • The customer wants a style, how do you go about designing it? • Stock Images for use: • http://cpu.rachelober.com/stock