To Cascading Style Sheets: A Workbook & Guide
To Cascading Style Sheets: A Workbook & Guide
to Cascading
Style Sheets
A Workbook &
Guide
LANCASTER
UNIVERSITY
1st Edition Information Systems
August 2002 Services
Introduction to CSS - A Workbook & Guide
Status Record
First Edition September 2002
Acknowledgements:
Screen shots reprinted by permission from Microsoft Corporation.
‘Introduction to CSS’ Copyright © 2002 Lancaster University.
Author: Olga Falko
TheGuideWorks Series
Other guides available in ‘TheGuideWorks’ series include:
Introduction to Windows NT4
Introduction to Windows Explorer
Word 97 for Converters
Intermediate Word 97/2000
Advanced Word 97
Introduction to Excel 97/2000 and Spreadsheet Concepts
Intermediate Excel 97/2000
Introduction to PowerPoint 97
Using Outlook 97 for Email
Getting More out of Outlook 97
Image Processing with Paint Shop Pro
GIF Animation with Animation Shop
For more information about these guides ask at ISS reception in the Library or visit the
website at: www.lancs.ac.uk/TheGuideWorks
Administration:
Document code: PC70B
File location: F:\CSS\Guide\css2.doc
Date printed: 30.01.2003
Table of Contents
INTRODUCTION TO THIS GUIDE .................................................................................................. 1
PRE-REQUISITES ................................................................................................................................... 1
SUMMARY ............................................................................................................................................ 1
SECTION 1 – CREATING A CSS ....................................................................................................... 3
EXERCISE 1.1 – CREATING A CSS........................................................................................................ 4
EXERCISE 1.2 – LINKING STYLE SHEET TO AN HTML DOCUMENT ...................................................... 6
EXERCISE 1.3 – CSS SYNTAX.............................................................................................................. 8
EXERCISE 1.4 – CSS EDITING ............................................................................................................ 10
SECTION 2 – TEXT FORMATTING ............................................................................................... 13
EXERCISE 2.1 – CHANGING FONT ...................................................................................................... 14
EXERCISE 2.2 – CHANGING FONT (CONTINUED)................................................................................ 16
EXERCISE 2.3 – SETTING FONT PROPERTIES IN ONE DECLARATION .................................................... 18
EXERCISE 2.4 – CHANGING TEXT APPEARANCE ................................................................................ 20
EXERCISE 2.5 – HYPERLINKS ............................................................................................................. 22
SECTION 3 – BACKGROUNDS AND COLOURS ......................................................................... 25
EXERCISE 3.1 – SETTING A BACKGROUND COLOUR ........................................................................... 26
EXERCISE 3.2 – SETTING A BACKGROUND IMAGE .............................................................................. 28
SECTION 4 – BOX PROPERTIES.................................................................................................... 31
EXERCISE 4.1 – BOX PROPERTIES ...................................................................................................... 32
EXERCISE 4.2 – SETTING MARGINS ................................................................................................... 34
EXERCISE 4.3 – BORDERS .................................................................................................................. 36
SECTION 5 – CSS CLASSES............................................................................................................. 39
EXERCISE 5.1 – CLASSES ................................................................................................................... 40
EXERCISE 5.2 – BLOCK AND INLINE ELEMENTS ................................................................................. 42
EXERCISE 5.3 – PROJECT ................................................................................................................... 44
SECTION 6 - APPENDICES.............................................................................................................. 45
APPENDIX A – MEASUREMENT UNITS ............................................................................................... 46
APPENDIX B – A TABLE OF CSS PROPERTIES AND VALUES ............................................................. 47
APPENDIX C – CSS WEB RESOURCES ................................................................................................ 51
.
Introduction to this Guide
This workbook introduces the use of Cascading Style Sheets in HTML and Web
Authoring. The course covers the following topics:
• creating external Style Sheets and linking them to your html document
• applying styles to html elements
• applying layout to web pages.
Pre-requisites
In order to get the most out of this course you should be familiar with Windows 2000
and World Wide Web, have basic knowledge of HTML and basic understanding of
building a web site.
Summary
This guide is divided up into sections:
Creating a CSS – creating a style sheet file and linking it to an html file
Text and Font Properties – styling text and headings.
Backgrounds, colours and Images – adding background images and colours.
Box Properties – setting up margins, padding and borders for various elements
Classes – applying styles to selected elements.
Exercise 1 Example
1. copy the example.
2. save the file.
3. close application…
2 Introduction to this Guide
Keyboard Explanation
press Enter press the large ‘Enter’ key to the right of the
main letter keys.
This key is sometimes known as ‘return’.
move cursor to.... use the cursor direction keys Ï to move the
cursor box along, up or down ÍÐÎ the screen.
You can also use the mouse.
press: Delete key use the key marked ‘Delete’ in the group of six
keys, which are just to the right of the ‘Enter’
key.
press: Backspace key tap the Í Backspace key to delete text to the
left of the flashing cursor bar. The ‘Backspace’
key is above the large ‘Enter’ key.
press: Tab key tap the ↹ (Tab) key to demote bullet points.
Hold down the Shift key & press ↹ key to
promote bullet points.
press Ctrl & (letter) hold down the Ctrl key and press the letter.
The Ctrl is at the bottom left of the keyboard and
is also known as ‘Control’.
Mouse Explanation
click on ‘File’ menu move the mouse over the desk so that the pointer
(or other feature) moves to the specified location (‘File’ menu in
our example) then press the left mouse button
down briefly.
double click on filename move the mouse so that the pointer points to the
filename (in this case) then press down the left
mouse button twice in quick succession.
right click on (feature) move the mouse pointer over the feature, then
click once with the right mouse button.
drag to (location) press and hold down the left button, move the
mouse to the new location then release the left
button.
Section 1 – Creating a CSS
H1 {
font-family: arial, sans-serif;
color: sienna;
}
HR {color: sienna;}
3. save your style sheet to A drive and give it ‘style1.css’ name – see Example B.
Case Convention
Although browsers are not case sensitive and can interpret a style sheet written in any
case, there is a convention to type the selectors (HTML tags in the example above) in
upper case and everything else in lower case.
Naming Convention.
Save your style sheet as a ‘plain text’ file. Give your style sheet a .css file extension.
Your Style Sheet filename may have letters (a-z) and numbers (0-9) and should start
with a letter.
Never use white spaces and special characters in CSS file names.
Examples 5
P {margin-left:0.4in;}
Notice that there is no white space between the number 0.4 and the
measurement unit in (inch).
3. save changes. Refresh index.html and page1.html in the Internet Explorer
window to view changes.
Examples 7
Add linking
instruction here
Add a new
instruction here
8 Creating a CSS
Rule
Declaration
H1{color:red;}
H1{color:red;}
selector property value
10 Creating a CSS
H1{
H1,H2,H3 {
Group the
rules here
12 Creating a CSS
Section 2 – Text Formatting
Add
declarations
here
Unit Description
% a percentage of something
1em a default size of the viewer’s font for the current element
1px pixel (a dot on a PC monitor)
pt points, 1 pt = 1/72 in
pc picas, 1 pc=12 pt=1/6 in
1in inch
1mm millimetre
1cm centimetre
16 Text and Font Properties
STRONG {
font-weight: bold;
font-style: italic;
}
5. save the changes to style1.css and view changes to the appearance of a third
paragraph of index.html in the browser window.
6. experiment with the styles for the <strong> element – see more font
properties and values in a table on page 19.
Examples 17
H1 {
font-weight: bold;
font-size: 12pt;
font-family: Arial,Helvetica;
font-style: italic;
}
Using shorthand
1. in the index.html at the bottom of the document (just above the closing
</body> tag) type:
Avoiding Problems
When using numeric values, length units and percentages in shorthand you rely on
the viewer’s browser to interpret which value belongs to what property.
In most cases the values should be listed in a specific order. Unfortunately it might
not help as you are allowed miss out any properties with the default values.
The advice is not to use shorthand with numeric values – spend few extra minutes
typing the rules with properties and values and get a predictable result .
Examples 19
A table of Font Properties and Values
Property* Values Description
font-family family-name A prioritised list of font family names
generic-family and a generic family name for an
element
font-size smaller Sets the size of a font
larger
length
%
font-style normal Sets the style of the font
italic
oblique
font-weight normal Sets the weight of a font
bold
bolder
lighter (where 400 is equal to normal and 700 is
100 - 900 equal to bold)
font-variant normal Displays text in a small-caps font or a
small-caps normal font
font font-style A shorthand property for setting all of
font-variant the properties for a font in one
font-weight declaration
font-size
font-family
*
The browsers might ignore some of the properties!
20 Text and Font Properties
color: red;
STRONG {
font-weight: bold;
font-style: italic;
color: red;}
3. save changes to your style sheet and view index.html in Internet Explorer.
H1 {
text-align: center;
letter-spacing: 0.1em;
}
and see all text (in <h1>) aligned to the centre and letter spacing increased by
0.1em – see example A.
In the above rule we have used em measurement unit. Here 1em is equal to the
default font size for the <h1> element. Find more in Appendix A, page 46.
5. Try increasing letter-spacing to 0.2em. Save and view the changes. Change it
back to 0.1em. Save changes.
text-decoration: overline;
7. save your style sheet and view the changes to index.html in Internet Explorer.
8. delete the text-decoration declaration and save the style sheet.
Examples 21
You can apply whatever style you'd like to each of these selectors, just like you would
do with normal text.
Note: there are no white spaces in the hyperlink selectors.
*
Note In order to be effective A:hover MUST come after A:link and A:visited in the CSS
definition and A:active MUST come after A:hover in the CSS definition.
Examples 23
Add hyperlink
style rules
H2 {background-color: yellow;}
H1 {background-color: transparent;}
TH {background-color: seashell;}
TD {background-color: white;}
Adding Comments
You may add comments to your style sheets. It will make it easier to do the editing of
style rules and declarations in the future.
1. put your cursor at the end of the TH rule after the closing curly bracket } and
press a spacebar few times.
2. type on the same line – see example B:
Add comments
28 Backgrounds and Colours
background-image: url(canvas.gif);
BODY {
background-color: seashell;
background-image: url(canvas.gif);
}
background-attachment: fixed;
BODY {
background-color: seashell;
background-image: url(canvas.gif);
background-attachment: fixed;
}
5. save changes to style1.css
6. in a browser window refresh the page, then use a side scrollbar to scroll up and
down to view the effect.
7. return to a style1.css.
Examples 29
Add a background -
image declaration
Add a background -
attachment declaration
30 Backgrounds and Colours
H2 {background-color: yellow;}
2. Add the following declaration to the h2 rule – see example B:
padding: 2%;
H2 {
background-color: yellow;
padding: 2%;
}
3. Save changes and view index.html and page1.html in browser.
4. Return to your style sheet.
border
element
padding
margin
Add a padding
declaration for H2
margin: 5%;
IMG {
float: right;
margin: 0.5em;
}
2. save changes to style1.css.
3. refresh page1.html in browser.
Examples 35
Add a margin
declaration here
Add a an IMG
rule here
36 Margins and Borders
H1 {
text-align: center;
letter-spacing: 0.1em;
}
2. add to it the following new declarations
border-color: red;
border-style: dashed;
border-width: 0.1em;
H1 {
text-align: center;
letter-spacing: 0.1em;
border-color: red;
border-style: dashed;
border-width: 0.1em;
}
3. Save changes to your style sheet and view the result in browser.
4. Try other border styles and width from the Example B.
5. Use your skills to create borders around H2 element.
Examples 37
Add a border
declaration here
Creating Classes
Let’s create a class to justify the second paragraph and another one to centre <h3> on
page1.
1. Type at the end of the style1.css
Note: text marked as h3 (without class attribute) will not be affected by any class style
rules.
Remember: there is a restriction on classes - they cannot be nested. A nested class
selector (e.g. H1.cent.red) is not allowed in CSS.
Examples 41
Add class
attribute
here
Add another
h3 here
42 Classes and IDs
DIV.frame {
background-color: seashell;
margin: 2%;
border: 2px sienna outset;
}
The shorthand border declaration above will produce the 2 pixel thick border
in sienna colour with 3D outset effect (top and left border in lighter sienna
colour and right and bottom border in darker sienna colour).
3. Open page1.html in Notepad and just below the opening <body> tag type
<div class=frame>
</div>
Add a new
rule here
Type a new
instruction
here
44 Classes and IDs
Unit Description
% a percentage of something
1em a default size of the viewer’s font
1px pixel (a dot on a PC monitor)
pt points, 1 pt = 1/72 in
pc picas, 1 pc=12 pt=1/6 in
1in inch
1mm millimetre
1cm centimetre
*
There are exeptions: e.g. vertical alignment and margins.
Appendices 47
*
The browsers might ignore some of the properties!
48 Appendices
Text
Property Possible Values Description
color color Sets the colour of a text
text-align left Aligns the text in an element
right
center
justify
text-decoration none Adds decoration to text
underline
overline
line-through
blink
text-indent length Indents the first line of text in an
% element
text-transform none Controls the letters in an element
capitalize
uppercase
lowercase
white-space normal Sets how white space inside an
pre element is handled
nowrap
word-spacing normal Increase or decrease the space
length between words
direction ltr Sets the text direction
rtl
letter-spacing normal Increase or decrease the space
length between characters
Hyperlinks
Selector Description
a:link Defines the style for normal unvisited links.
a:visited Defines the style for visited links.
a:active Defines the style for active links. A link becomes active
once you click on it.
a:hover Defines the style for hovered links. A link is hovered when
the mouse moves over it.
Note: Not supported by Netscape browsers prior to version 6.
Appendices 49
Background
Property Possible Values Description
background-color color-rgb Sets the background color of
color-hex an element
color-name
transparent
background-image url Sets an image as the
none background
background- scroll Sets whether a background
attachment fixed image is fixed or scrolls with
the rest of the page
background-position top left Sets the starting position of a
top center background image
top right
center left
center center
center right
bottom left
bottom center
bottom right
x-% y-%
x-pos y-pos
background-repeat repeat Sets if/how a background
repeat-x image will be repeated
repeat-y
no-repeat
background background-color A shorthand property for
background-image setting all background
background-repeat properties in one declaration
background-attachment
background-position Note the order of the properties
50 Appendices
Margin
Property Possible Values Description
margin-top length Sets the top margin of an element
%
margin-right length Sets the right margin of an element
%
margin-bottom length Sets the bottom margin of an element
%
margin-left length Sets the left margin of an element
%
margin margin-top A shorthand property for setting the
margin-right margin properties in one declaration
margin-bottom
margin-left
Padding
Property Possible Values Description
padding-top length Sets the top padding of an element
%
padding-right length Sets the right padding of an element
%
padding-bottom length Sets the bottom padding of an
% element
padding-left length Sets the left margin of an element
%
padding padding-top A shorthand property for setting all
padding-right of the padding properties in one
padding-bottom declaration
padding-left
Note the order!
Appendices 51
Border
Property Possible Values Description
border-color color Sets the colour of the borders
border-style Sets the style of the four borders:
none - no border is drawn
hidden -
dotted - the border is a dotted line
dashed - the border is a dashed line
solid - the border is a solid line
double - the border is a double line
groove - a 3D groove is drawn
ridge - a 3D ridge
inset - a 3D inset
outset - a 3D outset
Note: Some browsers will interpret
all border-styles (except for none and
hidden) as ‘solid’.
border-width thin Sets the width of four borders
medium
thick
length
border border-width Sets the borders
border-style
border-color
Index
B H
background hyperlink ...................................................... 22
background-attachment ............................28 colours ..................................................... 22
background-image....................................28 properties ................................................. 48
colour .......................................................26 roll-over effect ......................................... 22
properties............................................30, 49
I
borders ....................................................36, 51
image
C
as a background ....................................... 28
classes floating..................................................... 34
applying....................................................40
M
creating.....................................................40
comments ......................................................26 margins......................................................... 34
css ...................................................................4 properties and values ............................... 50
css syntax measurement units.................................. 15, 46
declaration..................................................8 absolute and relative length units ............ 46
property ......................................................8
selector .......................................................8 P
padding......................................................... 32
D
properties ................................................. 50
DIV ...............................................................42
creating a frame effect..............................42 S
SPAN............................................................ 42
F
style sheet
font................................................................14 creating ...................................................... 4
font-family ...............................................14 external ...................................................... 4
font-size....................................................14 linking to an html document...................... 6
font-style ..................................................16 naming ....................................................... 4
font-weight ...............................................16 saving......................................................... 5
generic-family ..........................................14
properties and values..........................19, 47 T
shorthand..................................................18 text................................................................ 20
properties and values ......................... 21, 48
text-decoration......................................... 20