Cascading Style Sheets HTML
Cascading Style Sheets HTML
Contents
Page 1 How to use this booklet
Page 2 Introduction
Page 8 Selectors, Properties and Values
Page 9 Practical Exercise 1 - Editing a CSS File
Page 11 Practical Exercise 2 - Creating and Linking a CSS File
Page 13 IDs and Classes
Page 14 Practical Exercise 3 - Adding Classes to a Style Sheet
Page 15 Extension Work - Page Layout Using Positioning Properties
Page 19 Practical Extension Exercise - Creating a Page Layout Using Positioning
Page 20 Appendix A - CSS Properties and Values
Page 22 Appendix B - HTML Tag Reference
Higher
Structures and Links stylesheets, CSS (cascading style sheets)
(Websites) page structure: head, title, body
metadata
dynamic web pages
interactive web page
1
Cascading Style Sheets & HTML
Introduction
As you have already learned, the source code in web pages (HTML) is used by browsers to display
the pages according to the format detailed in the HTML.
In addition to the web page text itself, the code will contain style details such as:
● what fonts, spacing, justification to use for text
● what colours to use for objects or backgrounds
● where to position objects
● what size objects should be
● which objects are in front or behind other objects etc, etc, etc
There are three ways that styles may be added to HTML code: in-line, internally or externally.
Each of the three examples that follow will use the HTML code shown below. Before styles are
added the code produces a white web page with black text. The page contains no formatting.
Source Code
Web Page
2
Cascading Style Sheets & HTML
In-line Styles
Method one involves embedding styles directly, “in-line”, into HTML tags as shown below.
Styles have been added to the <body>, <h1>, <h2>, <table>, <tr>, <td> and <p> tags to control margins,
colours, fonts, text size, spacing and borders.
When in-line styles have been inserted into a tag they only apply to that tag. You will notice that the
above document has a lot of repetition of styles throughout.
3
Cascading Style Sheets & HTML
Internal Styles
Method two is highlighted below and involves the use of the <style> tag to list all the styles that will
be used throughout the source code at the top of the document.
Note that the HTML tags in the body of the document no longer contain additional style instructions.
When a style is used to define a tag internally in this way, the styles apply to every time the defined tag
is used in the source code. Although this significantly reduces the repetition of code in the document it
creates a problem with larger websites as the same styles must be included in each web page in the site.
4
Cascading Style Sheets & HTML
External Styles
Method three removes the styles from the HTML source code and places them in a separate document
called a cascading style sheet.
The two files are linked by adding the highlighted line below to the HTML source code. The browser
interprets this by loading the named style sheet (in this example - “External StyleSheet.css”) and
applying the style definitions it lists when displaying the web page.
5
Cascading Style Sheets & HTML
Once a CSS has been created it can be linked to multiple web pages. This ensures that headings, text
format, layouts, tables, paragraphs are formatted the same way throughout a website.
6
Cascading Style Sheets & HTML
For large websites, cascading style sheets are essential as they:
● reduce workload - styles are only
created once and are then applied to
every page Edited
● ensure consistency - if styles were CSS
applied internally every web page File
would have to be checked to ensure it
used exactly the same styles as the
others
● aid maintenance - a change can be
applied to an entire website by simply
changing one line of code in the CSS.
7
Cascading Style Sheets & HTML
Selector Properties
Values
Note:
● each property is separated from the value with a colon
● each property is separated from the next property with a semi-colon
Selectors with a large number of properties become harder to read if the properties are all squashed
together in a single line. For this reason it is common to split the properties over several lines as
shown below.
Once you know how a CSS file is structured, the next step is to learn how the different properties can
be applied to each selector. Unless you are professional web developer it’s unlikely that you will
memorise the extensive list of properties and values that are available.
At the back of this booklet you will find an Appendix of commonly used properties and values. Use
Appendix A when completing each practical exercise.
If you wish to use additional properties and values the Internet has many, many websites devoted to
the use of CSS files in website development.
8
Cascading Style Sheets & HTML
Step 3 Open the HTML file in the CSS Task 1 folder in a browser of your choice.
Step 4 Edit the CSS file so that the <h1> tag will display:
● white text
● centered text
● size 24px text
Save the CSS file to update the changes you have made and then refresh the web page
in your browser. The web page should change to reflect the newly edited CSS file.
After
10
Cascading Style Sheets & HTML
Step 2 Using a suitable editor, create a new blank document and save it as a .css file to your
own copy of the CSS Task 2 folder. Choose a suitable name for the file.
Step 3 Using a suitable editor open the HTML file called “Standard Algorithms.html”.
Step 4 Also open the “Standard Algorithms” file in a browser and look carefully at the web
page you are about to format.
You can see from the above web page that there is a main heading, several sub-
headings, a bullet-point list, paragraphs and several tables containing pseudocode.
These were created using the following HTML tags:
● <h1>, <h2> & <h3> three levels of headings
● <div>, <p> division sections and paragraphs
● <ul>, <li> bullet point list containing list items
● <table>, <tr>, <td> rows and data (cells) within a table
11
Cascading Style Sheets & HTML
Step 5 First you must link the HTML file to your CSS file.
Inside the <head> tag, at the top of the HTML file, add the line of code shown below.
Remember to put your own .css file name into the link and save the changes.
Step 6 Now you are ready to begin creating and testing your cascading style sheet.
Step 7 Using Appendix A (along with CSS reference pages on the Internet) add properties
and values to each of the selectors.
Unlike other tasks, where you may complete a large section of a project before testing
it, you should continually save the CSS file and then refresh your browser to check
your CSS code is formatting the HTML file correctly.
Step 8 When you have finished, print the HTML file in the browser, write your name on it
and submit it to your teacher.
12
Cascading Style Sheets & HTML
IDs and Classes are used to define additional style information that is not tied to a specific tag. These
two additional selectors behave in a similar way and should be used as follows:
● ID selectors are created to style a single, unique element in your web page. For example
a paragraph that is formatted differently for any other tag in the HTML. IDs are added
to the CSS file with a preceding # symbol and then referenced in the HTML by adding
the ID within a tag, as highlighted below.
CSS file HTML file
● Classes are created when you wish to apply the same style to multiple tags.
Classes in CSS files are preceded with a full stop and are added to the HTML tags as
highlighted below.
CSS file HTML file
13
Cascading Style Sheets & HTML
The html file contains 5 tables. An example of the table for the linear search
algorithm is shown below. Note that there are no borders set in the table.
Step 2 Your task is to create classes in the CSS file and use them in multiple <td> tags to
indent the loops and if statements in the pseudocode. You will have to work out how
to increase the blank space before some of the text.
A finished example is shown below.
14
Cascading Style Sheets & HTML
The boxes below can be mapped to the different areas of the Amazon web page shown above.
To create this page, each of the above boxes would be created using division <div> </div> tags. Style
sheet rules are then created to resize and position each of the boxes.
15
Cascading Style Sheets & HTML
The diagram below shows a common structure for a simple web page.
Header
Navigation Content
Footer
To create the structure for the above web page start by creating a division for the whole page.
16
Cascading Style Sheets & HTML
Complete the structure by adding a division for each of the four boxes.
Header
Navigation Content
Footer
You should now have 5 divisions with one of them enclosing the others.
Whole page
Header
Navigation
Content
Footer
To resize and position the boxes we will create ID selectors. (Remember IDs are unique to one
object.)
17
Cascading Style Sheets & HTML
Header Style
This allows us to set:
● a background colour of
● a padding of 20 pixels inside the header
No positioning is required as the header <div>
appears first out of the four boxes in the HTML.
This means it will automatically appear at the
top of the page.
Navigation Style
This allows us to:
● ensure that the navigation box is positioned
at the left of the web page
● set the width of the navigation
● set a padding of 20 pixels inside the box,
pushing the text from the edges.
● set the background colour to
Content Style
This allows us to:
● ensure that the content box is positioned at
the right of the web page
● set the width of the content box
● set a padding to push the text in from the top
and left of the content box edges.
Footer Style
This allows us to:
● clear the left and right positioning of the
previous two boxes (ignore them)
● set a background colour of
● centre all the text
● set a padding of 20 pixels inside the footer
Note that a height of 1% will ensure the footer
is never larger than its contents require.
18
Cascading Style Sheets & HTML
Your task is create the layout shown below using <div> </div> tags, IDs, properties and values
to position objects on the page.
Header
Menu Options
Advertising
Navigation Content
Space
Footer
19
Cascading Style Sheets & HTML
Appendix A - CSS Properties & Values
Text
Property Example Values Explanation
font-family ariel Sets the selectors font.
helvetica Note that if the name of the font is longer than one word it
“times new roman” should be enclosed in inverted commas.
font-size medium Text may be set to a small, medium, large size.
14px Set text to a fixed size in pixels (px) or points (pt).
18pt The percentage value indicates the size of text relative to a
75% parent element.
font-weight bold The thickness of each character can be adjusted using either
normal text values (bold etc) or more accurately using numerical
bolder values (500).
lighter For numerical values, 400 = normal and 700 = bold.
400
font-style normal Sets styles of text as seen in Word Processing applications.
underline
italics
text-decoration underline This property works in a similar way to font-style and is
overline commonly used to style hyperlinks.
line-through
text-transform capitalize Changes the first letter of every word into uppercase.
uppercase Turns all the text to uppercase.
lowercase Turns all the text to lowercase.
letter-spacing 0.5em Sets the size of the gap between each letter.
word-spacing 2em Sets the size of the gap between each word.
Word-wrap Normal Sets text to word wrap words at the end of a line (normal) or
Break-word split words if they are too long (break-word)
text-align left Text justification as found in Word Processing applications.
right Note the spelling of ‘center’ uses American English.
center
justify
text-indent 20px This indents the first word of a paragraph by a distance given
in px.
line-height 2 This specifies the distance between the lines in relation to the
font-size. So a value of 2 would be double spacing.
Colours
Property Example Values Explanation
color blue Colors (note the American spelling in HTML and CSS) can be
rgb(255,67,0) set on the majority of selectors using:
#ff349f ● a word - red, navy, yellow, black
● an r/g/b value - expressed as numbers between 0 and 255
background-color (237,129,255) or a percentage (60%,34%,0%)
● a hexadecimal rgb code - using either one digit (0-f) for
each color (f6b), or two digits (ff6c00)
20
Cascading Style Sheets & HTML
21
Cascading Style Sheets & HTML
Appendix B - HTML Tag Reference
Document
Tag Explanation of Use
<html> These tags appear at the top and bottom of every web page. They tell the browser that
</html> this is an HTML document.
<head> The head tag acts as a container for head elements. The head tag must include a <title>
</head> and may include <style>, <base>, <link>, <meta> and <script> tags.
<title> The title tag is used to enclose a title of your choice that will appear at the top of the
</title> browser when the page is viewed.
<title>My Hockey Home Page</title>
<body> The body tag encloses the all the content of the web page that will be displayed by the
</body> browser.
Example <html>
<head>
<title>My Hockey Home Page</title>
</head>
<body>
I have been playing hockey since the age of 6.
</body>
</html>
Layout
Tag Explanation of Use
<div> The division tag can be used several times throughout a document to spilt the HTML code
</div> into sections. They are often used with CSS files to layout a web page.
<p> The paragraph tag is used to define and enclose paragraphs of text.
</p>
<br> The break tag tells the browser to move the next object onto the next line.
Links
Tag Explanation of Use
<a> The a tag encloses an object to turn it into a hyperlink.
</a> It is used like this:
<a href=”http://www.rslearning.co.uk>Visit RS Learning Ltd</a>
<link> The link tag is used to load in external sources like style sheets and has no end tag.
<link rel="stylesheet" href=”Home Page Styles.css">
Graphics
Tag Explanation of Use
<img> The image tag includes a link to a graphic file and tells the browser how to display the
file.
<img src="smiley.gif" alt="Smiley face" height="42" width="42">
22
Cascading Style Sheets & HTML
Tables
Tag Explanation of Use
<table> Used to enclose the content of a table.
</table>
<tr> The table row tags enclose the data for each row of the table. If the table has three
</tr> columns each table row tag will enclose three sets of data tags <td> </td>.
<td> The table data tags each represent a cell in the table. All the information in a table,
</td> displayed by the browser, is enclosed by <td> </td> tags.
<thead> The table head tag usually encloses the top row of the table. It can be used to style the
</thead> headings or top row of the table differently from the rest.
<tbody> The table body tag encloses all the rows that make up the body of the table. It can be
</tbody> used to style the body content of the table differently from the headings.
<colgroup> The column group tag allows groups of columns to be declared and then styled.
</colgroup> Additional <col> tags contain the information about how the columns are grouped.
Example <table>
<colgroup>
<col style="background-color:red">
<col span="2" style="background-color:yellow">
</colgroup>
<thead style="background-color:red">
<tr>
<td>Name</td>
<td>Wins</td>
<td>Draws</td>
</tr> Name Wins Draw
</thead>
Pars United 29 5
<tbody>
<tr> Saints Academicals 18 16
<td>Pars United</td>
<td>29</td>
<td>5</td>
</tr>
<tr>
<td>Saints Academicals</td>
<td>18</td>
<td>16</td>
/tr>
</tbody>
</table>
Lists
Tag Explanation of Use
<ul> The unordered list tag creates a bullet point list. Each bulleted item should be contained
</ul> between list item tags.
<ol> </ol> The ordered list tag creates a numbered list. Each numbered item should be contained
between list item tags.
<li></li> The list item tags are used to enclose each item in a list.
Example <ul>
● Bread
<li>Bread</li>
<li>Butter</li> ● Butter
<li>Jam</li> ● Jam
</ul>
23