What is CSS?
Cascading Style Sheets | |
specify the style of HTML elements (e.g. spacing, margins) separately from the structure of the document (e.g. Headers, body text, links) | |
control the appearance of a Web site | |
all pages of a Web site provides the same look and feel | |
External Style Sheets saves a lot of work and are stored in CSS files | |
Multiple style definitions will cascade into one | |
World Wide Web Consortium (W3C) created STYLES in addition to HTML 4.0 | |
Both Netscape 4.0 and
Internet Explorer 4.0 support CSS
|
Cascading Order
Multiple styles will cascade into one | |||||||||
Style Sheets allow
style information to be specified:
| |||||||||
Cascading Order (number (1) has the highest priority): |
(1) inline style
(2) internal style sheet
(3) external style sheet
(4) browser default
CSS syntax
General format |
selector {property:value; property:value;...}
3 types of
selector:
| |||||||
property - characteristic to be changed, e.g. color | |||||||
value - value to assign to the property, e.g. blue |
Examples: |
body {color:green} |
p {font-family: "Courier New"} |
p {text-align: center; color: blue; font-family: "sans serif"} |
or
p { text-align: center; color: blue; font-family: "sans serif" } |
CSS
GroupingGroup selectors to have the same format | |
Examples: |
/*
heading 1, 2 and 4 are set to red */
h1,h2,h4 |
comment begins with "/*", and ends with "*/ |
CSS class attribute
to define different styles for the same element, e.g. |
p.right
{text-align: right} p.center {text-align: center} |
<p
class="right"> Right-aligned this paragraph. </p> <p class="center"> Center-aligned this paragraph. </p> |
omit tag name in selector, so style defined can be used by many elements: |
.left {text-align: left} |
<h4 class="left"> Left-aligned this paragraph</h4> <p class="left"> Left-aligned this paragraph, too</p> |
CSS - id Attribute
The id attribute has to be unique on the page | |
There can only be one element with a given id in a document | |
They are marked in the HTML document with id instead of class: |
<p id="intro"> This paragraph will be right-aligned. </p> |
The id attribute can be defined to match all elements with a particular id, or to match only one element | |
Examples to match all elements with id="intro": |
#intro { font-size:110%; font-weight:bold; color:#0000ff; background-color:transparent } |
Insert a Style sheet
External style
sheet
|
<head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> |
Sample program 1 :
sample1.htm |
<html> <h2>This header is red</h2> <p>This paragraph has a left margin of 60 pixels</p> </body></html> |
sample1.css |
body {background-color: FFCC99} |
result |
Sample program 2 :
sample2.htm |
<html><head> |
sample2.css |
body {background-color: tan} |
result |
Internal style
sheet
|
<head> <style type="text/css"> hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} </style> </head> |
Sample program 3 :
sample3.htm |
<html><head> |
results |
Inline style
|
Format a background
Sample programs |
sample4.htm |
<html><head> |
results |
sample5.htm |
<html><head> |
results |
More about background properties... click |
Format text
Sample programs |
sample6.htm |
<html><head> |
results |
More about text properties... click |
Font Properties
Sample programs |
sample7.htm |
<html><head> |
results |
More about font properties... click |
Border Properties
Sample programs |
sample8.htm |
<html><head> |
results |
More about border properties... click |
Margin Properties
Sample programs |
sample9.htm |
<html><head> |
results |
More about margin properties... click |
List Properties
Sample programs |
sample10.htm |
<html><head> |
results |
sample11.htm |
<html> |
results |
More about List properties... click |
SPAN tag
to mark-up an element to apply a style without affecting by other predefined formatting feature | |
generic grouping element | |
apply styles or ID attributes to a block of text | |
e.g. |
text here is not affected <SPAN class="dark"> dark text </SPAN> |
CSS-P
for position and visibility of elements | |||||||
<DIV> tag
| |||||||
define using <STYLE> and <DIV> |
<STYLE> #book {css-p rules} </STYLE> |
<DIV ID =
"book" STYLE= css-p rules> This is a good book </DIV> |
properties that can be handled by DIV ... click | |
Samples program |
sample12.htm |
<HTML><HEAD> |
results |
Important notes
MSIE and Netscape does not support CSS to the same extent | |
check the type of browsers before using some of the styles |