CSS Coding PDF
CSS Coding PDF
66
CSS
CSS is an acronym which stands for Cascading Style Sheets.
A style may be applied to an existing HTML tag or can be used to create the equivalent of one.
This is called a style.
ASSIGNMENT
Create a subfolder called "css" in your "labs" folder on your personal (g:) drive and
copy & paste the following into notepad (you can access this from the course site in
ATutor). Save as practice.htm. Create a sub-folder in the "css" folder called images.
Scroll down past the HTML code below and right click on the image and save it in the
graphics folder (you can access this from the course site in ATutor).
---------------------------------------------------------------------------------
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<p>Elementary School</p>
<ul>
<li><a href="index.htm">Home</a></li>
<li><a href="schoolinfo/schoolinfo.htm">School Information</a></li>
67
<li><a href="schoolinfo/calendar.htm">Calendar</a></li>
<li><a href="academics/academics.htm">Academics</a></li>
<li><a href="extracurricular/extracurricularactivities.htm">Extracurricular
Activities</a></li>
<li><a href="students/students.htm">For Students</a></li>
<li><a href="parents/parents.htm">For Parents</a></li>
<li><a href="staff/staff.htm">For Staff</a></li>
<li><a href="staff/schooldevelopment.htm">School Development</a></li>
<li><a href="search/demosearch.htm">Search</a></li>
</ul>
<h1>Upcoming Events</h1>
<ul>
<li>Dec 8 ~ Christmast Concert</li>
<li>Dec 19th ~ last day of classes for 2008</li>
<li>Jan 5th ~ first day of classes for 2009</li>
</ul>
</body>
68
</html>
69
Applying CSS
There are three ways in which to apply css code to an HTML doc:
1. Inline
2. Internal
3. External
Inline
The style is used with a single instance of an HTML tag. In reality, it would make much more
sense to use HTML to change only one specific tag.
<html>
<head>
<title>Inline Style Page Example</title>
</head>
<body style="background-color:#99FFCC;">
<p>This is an example where the background colour of this particular webpage would
be light blue</p>
</body>
</html>
Internal
Styles are defined in the head section of an HTML document. These styles only affect the
instances included in the particular page. It is more common for people to use External style
sheets.
<html>
<head>
<title>Internal Style Page Example</title>
70
<style type="text/css">
body{background-color:#99FFCC;}
</style>
</head>
<body>
<p>This is an example where the background colour of this particular webpage would
be light blue</p>
</body>
</html>
External
Styles are defined in a separate document called a "style sheet". This sheet may be referenced
from many HTML documents. As such, each time the styles are changed within the sheet the
"look" is reflected in HTML pages referencing the sheet.
<html>
<head>
<title>External Style Page Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<p>This is an example where the background colour of this particular webpage would
be light blue</p>
</body>
</html>
71
The style.css sheet reads as follows:
body {background-color:#99FFCC;}
72
Cascading
Different style sheets will overwrite (cascade) similar styles:
ASSIGNMENT
Open the practice.htm document in notepad. Although it hasn't yet been created, you
need to create a link to an external style sheet called styles.css in the same directory
as the practice.htm document.
73
Coding Syntax
Examples:
p {text-align:center; color=green;}
- you can apply the same property and value to vaious selectors as in the example above
74
Selectors
There are three types of selectors:
tag selectors
ID selectors
Class selectors
Tag Selectors
Tag selectors are those which change already in existence HTML tags.
p{
background-color: red;
}
Class Selectors
Class selectors are essentially a way in which to style an element which you name. For
example, you might wish to create a type of text box for news stories on a webpage to be
reused many times, or a way in which to wrap text around an image. The point is that a class
selector can be used many times.
#leftcontainer{
background:url(images/template/shadow_left.gif) repeat-y right top;
width:419px;
float:left;
color:#585858;
font:11px Tahoma, serif;
height: 466px;
overflow: auto;
}
ID Selectors
An ID selector is exactly like a class selector except that it can only be used ONCE in a page.
75
Using Selectors
To use selectors which are different from the HTML Tags, you will need to add these in in a
similar way to HTML tags.
To do this you use SPAN and the term class or id depending on what kind of selector it is.
If your selector is already an HTML tag, you would simply use it as you already have.
<p>Ipsum Lorem</p>
76
Properties
There are many different properties and values that can be applied to selectors. This section
will examine them in some detail. As always, this is not a final list but a good cross reference of
the variety that are available.
77
Text
There are many ways in which to change the value of properties that relate to text.
line-height:
30px;
normal;
text-decoration:
underline;
overline;
line-through;
blink;
none;
font-weight: bold; You may also choose 100, 200, 300, 400, 500,
bolder; 600, 700, 800, or 900
lighter;
normal;
text-variant: small-caps;
normal;
text-transform: capitalize;
uppercase;
lowercase;
none;
color: #99CCFF;
red;
78
ASSIGNMENT
Open notepad. Save the empty file as styles.css (in your css folder).
Notes
1. If you type in /**A Description**/ it will help you as you develop sections. In the
end, your styles.css file will have several sections and it makes sense to name them
what they are. In this case, we are starting off with changing a normal HTML code
(body), so you are going to call this section "HTML Styles".
2. Do not attempt to change the colour of the text (eg body is pink) this is just to help
you see the difference between the style, selector and properties.
Explanation
What you are doing is changing the HTML code body to result in the following: All text
(unless over-ridden by another style) will be 12px Arial (or Helvetica or sans-serif if the
computer does not have that font) and it will be a darker grey.
Save the styles.css file. Because you are referring to this file in your practice.htm
page, you do not have to do anything to that page. Refresh it in the browser and see
what happens to the text!
79
In this case, there is a PROPERTY which is redundant in the above example and
really not required. Remove it and only add the 3 properties required to change the
look of a header.
Save the .css file and refresh your practice.htm file to see what has happened to any
text which has the <h1></h1> tags around it.
80
Background
In the HTML section, backgrounds were applied to two items...the entire page or to a cell in a
table. The Background of any css selector can be changed in css.
ASSIGNMENT
Open a web browser and navigate to and open the practice.htm file.
81
In the last variation of the styles.css file, the HTML code for <h1> had a style applied
to it where the font was a little bigger than the default font, bolded and a turned a
green colour.
Change the background of the h1 tag by using the "background-color" property. Make
the colour be: #bce27f.
Save the .css file and preview the practice.htm file in your web brower. Welcome to
our school should now be in darker green with a lime green background which
reaches across the page from left to right.
82
Block
property: value; Notes:
word-spacing 3px; - increases or decreases the whitespace
between words
letter-spacing 5px; - increases or decreases the whitespace
between characters
vertical-align - sets the vertical alignment of an element
baseline;
bottom;
inherit;
middle;
sub;
super;
text-bottom;
text-top;
top;
83
table-row-group;
table-header-group;
table-footer-group;
table-row;
table-column-group;
table-column;
table-cell;
table-caption;
ASSIGNMENT
Open notepad and the styles.css file. Change the letter spacing of the h1 tag to -3px.
Save and preview the change to practice.htm in your web browser. Note that word
spacing is not observed...change the word spacing to 3px.
Next, create a new section header. Remember this is not code, but a note to
yourself. You can use /** ending with **/ to leave notes.
You are going to create your own selector called .newspara (short in this case for a
"news paragraph" style). This will indent the text 50px for any text you add this new
selector to.
Save style.css.
----------
Because this is a new style (a class selector) and not a tag (HTML) selector it will not
84
affect any item within your HTML file unless directly coded in. Note how it is coded in
below. Add this class to the paragraph following "Welcome to our school" and also
"School News".
Rather than using HTML code to align these paragraphs to the left...this information
can be incorporated into the stylesheet as well. That way, if in the future you wish to
differently align all news paragraphs you can quite easily.
85
Box
Box properties are extremely useful in css. This area is what allows you to specifically decide
how to parcel each item on the page. Later on, when we look at combining this with positioning
you will see how placing boxes precisely on a page will allow you to accomplish not only what
you were able to in HTML with tables, but you can go further with this in that you can layer
boxes amongst other things.
86
ASSIGNMENT
Open your styles.css file and insert the following in the /**Class Selectors**/ section:
Open your practice.htm file. Find the text where updates are listed and add the class
as highlighted below. Notice that you place the opening tag (in this case span class)
at the beginning and end of the section you wish to affect just like with HTML.
----- let's change it up a little bit more...add the background-colour to the .updates
style.
87
Div vs Span
ASSIGNMENT
Normally, creating the layout of a page should be done first; however, because you
are learning different aspects of .css which are related, this is where we will
accomplish the layout of the html page.
So...as we are working on creating a page which will look something like the below
page, the following "boxes" have been drawn to show you what we are working on.
Each box we are going to create is named and outlined as well as one big container
for the entire page is indicated by arrows.
88
So...with keeping organization in mind in your styles.css page create a new header for
the Class Selectors you are going to create to accomplish the layout. Creating a
container to hold everything will accomplish what you did in the HTML section with the
main table.
The .header style (which we will do more with in the future) basically replaces the top
cell of the table.
The following 2 classes -- sidebarup and sidebardown create the equivalent of two
cells which we will ensure make up the left hand isde of the page.
89
The .mainbox class creates a container which can mimic the main cell of the table
created in HTML. There are some interesting features here which we will further
exploit in a few lessons. The footer replaces the bottom cell of the table. Again...we
will be adding to this soon.
Obviously, these styles will not yet affect any content on your webpage as they have not been
coded in to the HTML document.
So...because you are inserting totally NEW codes this is an appropriate time to talk about the
"div" and "span" code. In order to insert these codes you will either use:
<div class="nameofstyle"></div>
OR
<span class="nameofstyle"></span>
90
So when do you use div and when do you use span?
In short, div is used for block elements and span is used for inline elements. Block elements
are items which stand on their own on a page and take up the horizontal space on either side of
it, where inline elements are included in the line or flow of the page. Think of a block element as
being similar to the paragraph in HTML (except it can contain more than just text) and the inline
element would be similar to a font or link element where simply some text is changed.
ASSIGNMENT
We have already used span to affect the .updates information; in the case of all of the
above, you should use div as they are blocks that you are delineating around the
page.
Open your practice.htm file and insert the styles around the appropriate content. It will
not yet look "perfect", but items will begin to fall into place.
91
Placement in Code
Obviously, it still matters which order you place your styles in...especially when using block
elements. The following code is actually placed in your html document AFTER the "main"
section of code.
ASSIGNMENT
Copy and past the "sidebardown" section of the practice.htm document to above the
"main" section. Save and preview.
92
Next add the following to your HTML section. This will make the image move to the
right hand side of the element it is contained within. See how this can replace tables!
93
Border
property: value; Notes:
border: thin #000000;
none - combine the three values into one, for
border-right: medium
border-left: dotted thick example:
border-top: 5px
border-bottom: dashed border-right: thin dashed #FF0000;
solid
double
groove
ridge
inset
outset
ASSIGNMENT
Open your styles.css sheet. Go to your .footer style and give it a 1px top border which
is solid and the colour #668e39.
94
List
property: value; Notes:
list-style-type: without specifying the type -- the default is
disc;
circles
circle;
square;
decimal;
lower-roman;
upper-roman;
lower-alpha;
upper-alpha;
none;
list-style- inside;
- outside creates a bullet which resides on
position: outside;
the left hand side of the list separately
- inside creates an indented bullet
list-style-image: url(direct or indirect link); - you can create your own graphical bullet
and link to it
In our practice page, there are two lists. One makes up the menu in sidebarup and the other
lists the upcoming events in the sidebardown. We are actually going to replace the list in
sidebarup in the near future; however, see how it affects BOTH lists.
ASSIGNMENT
In the appropriate section in your styles.css sheet, create a new definition for <ul>
which makes use of squares inside the list.
Save styles.css and preview your practice.htm page. You'll note that both the menu
and Upcoming Events lists now have squares which are indented. We are actually
going to treat the menu in a totally separate way next so that this "look" does not apply
to that list.
95
96
Positioning
The position property defines where an element will be placed on a page, either in relation to
the upper left hand corner of the screen or through it's placement within the code.
width: auto;
750px; (a value)
height:
auto;
97
500px; (a value)
clip: rect
- eg. rect (1px,33px,1px,5px)
(top,right,bottom,left);
- this property is fairly useless and
reminds one of the discussion about
images -- resize or crop them to their
actual display size so that that is all the
user has to download
- clipping a layer allows you to determine
what portion of a layer is displayed
98
Cleaning Up
Now that you have a fairly good overview of what .css can do. Here are a couple more things
where you may wish to use non-HTML elements to create or layout content on your webpage.
ASSIGNMENT
Replace the menu list by removing the HTML list tags in your practice.htm page.
Next go to the styles.css page and create a new section called /**Menu**/
First download and save this green rectangle ( ) in your graphics folder and then
create the following:
What this is, is a style called menu links that you can apply to the entire menu. The
99
a:links, a:visited: and a:hover are the ways in which you wish all links specifically
found within the menulinks style to be treated. This makes a much more interesting
navigation effect than a simply list.
FURTHERMORE, imagine inserting background images to make the links look like
buttons...
ASSIGNMENT
Save the following image into your graphics folder and incorporate it as a background
in your header style. This can be attained from the Atutor Course site.
ASSIGNMENT
AND
100
and the end result should be very similar to...
101
Major Assignment
Recreate your HTML project in a new folder using .css to replace/tweak HTML tags, to create
your own new styles, update your navigation menu, as well as to lay out the page.
102
Assignment Listing
In the css folder you should have the following files:
practice.htm
styles.css
css major project
103
css Marking Scheme
Code Changes to Practice.htm
104
o As is
o Add 1 px solid border colour 668e39
- Create section /**Menu**/
- #menulinks
o As is
- #menulinks a:link, a:visited
o As is
- #menulinks a:hover
o As is
105
WYSIWYG
What You See Is What You Get
This section can only be found on the Atutor Course site
106