 |
Linked Style Sheets
|
|
|
 |
This lesson: Overview | Syntax
| Location | Example
| Exericses
Other lessons: Style Syntax | Order of
Precedence | Style Properties |
Other Style Information
Intro. to Styles | Inline Styles | Embedded Style Sheets |
Linked Style Sheets
- The style sheet information is located in a separate file from the web
page. A link is established between the web page and the file containing
the style information.
- Use a linked (external) style sheet if you want to apply formatting styles
to elements in more than one web page.
- A web page can have a link to multiple style sheets.
The syntax for linked and embedded style sheets are the same. These two
style types:
- use curly braces, i.e. { and },
to enclose the style definition (or declaration).
This is the syntax for linked (and embedded) style sheets:
Tag { property: value; property: value }
Here are a few variations of the above syntax:
Tag, Tag, Tag { property: value; property: value }
Tag.class { property: value; property: value }
H1, H2, H3 { font-size: 20pt; color: red }
H4 { font-size: 16pt; color: navy }
H5.Indent { font-size: 12pt; margin-left: 50pt }
The style information for a linked style sheet is placed in a separate
file. Further,
- The file containing the style information must have an extension of .css.
- The .css file must be saved as a plain
text file.
- The .css file must be transferred to the
web server. If a web page uses a linked style sheet and the
.css file is not on the web server, some
browsers may not display the web page.
- Example of style information in a .css
file:
H1, H2, H3 { font-size: 20pt; color: red }
H4 { font-size: 16pt; color: navy }
H5.Indent { font-size: 12pt; margin-left: 50pt }
- The link between the web page and the .css
file is established by placing the following code into the HEAD section of
the web page:
<LINK rel="stylesheet" type="text/css"
href="filename.css">
- The rel attribute specifies the
relationship to the .css file.
- The type attribute specifies the type of
style sheet (i.e. cascading style sheet).
- The href attribute allows you may specify
the relative path or the complete web address of the .css
file.
- Examples of the code which is used to create a link from the web page to
the .css file:
<HEAD>
<LINK rel="stylesheet" type="text/css" href="mystyles.css">
</HEAD>
OR
<HEAD>
<LINK rel="stylesheet" type="text/css" href="http://www.mysite.edu/styles/mystyles.css">
</HEAD>
- Click here to view an example of a web
page that uses a linked style sheet. Use your browser to view the HTML.
- Below is the code for the linked style sheet for that web page.
- Note: JunkFont1 is a fictitious font name. As a result,
since the web page visitor doesn't have this specific font, the
browser will display the text using one of the alternative fonts,
either Arial or some other sans-serif font.
body { font-family: JunkFont1, Arial, sans-serif }
h1 { font-size: 20pt; font-weight: bold;
color: #FF0000; text-align: center }
h2 { background-color: #CCCCFF; text-align: center }
blockquote { color: green }
address { font-size: 10pt; text-align: center }
Method 1 - Using a Text Editor
Create the Style Sheet
- Use a text editor to create a new file and type the code below.
body { font-size: 18pt }
h1 { font-family: Impact, Cooper Black, sans-serif;
text-align: center;
background-color: yellow }
- Save the file with the name of:
linkedstyles.css .
- Don't forget to save the file with an extension of
.css.
- Save the file in the same location as your web page (for this exercise).
Create the Web Page (with a Link to the Style Sheet)
- Use a text editor to create a new file and type the code below.
- You may use a web page editor. If you use FrontPage, use the HTML view
to type the code that appears boldfaced (and in red).
- Or, if you wish, open the file,
exercise-linked-text.txt.
Copy the needed text and paste it into the appropriate areas. To paste
as Normal, select Edit/Paste Special from the menu.
At the Convert Text dialog window, click on the option for Normal Paragraphs.
<html>
<head>
<title>Exercise: Using Linked Style Sheets</title>
<link rel="stylesheet" type="text/css" href="linkedstyles.css">
</head>
<body>
<h1>Exercise: Using Linked Style Sheets</h1>
<p>The linked style sheet formats the body text as 18 points. It also formats a heading 1 paragraph so that it uses
the Impact or Cooper Black Font. If those fonts are not available, then a
sans-serif font is used. Heading 1 is also formatted with a center text
alignment and a yellow background.</p>
</body>
</html>
- Save the file with the name of: exercise-linked.htm
.
- View the web page in a browser.
Method 2 - Using FrontPage
- Only certain formatting features in FrontPage can be applied as styles
when using the FrontPage Style menu.
Create the Style Sheet
- From the FrontPage menu, select: File/New/Page.
- At the New dialog window, click on the tab for Style Sheets.
- Click on the desired style (in this case, Normal Style Sheet) and click
the OK button.
- Save the file with the name of:
linkedstyles.css.
- From the menu, select Format/Style.
Format Body Text
- At the Styles dialog window, click on the body
tag which is listed in the Styles box. Then click the Modify button.
- At the Modify Style dialog window, click the Format button.
Then click on Font from the pop-up menu.
- At the Font dialog window, select 18pt
from the Size box. Then click OK until you return to the Style dialog window.
Format Heading 1
- Click the drop-down arrow next to the List box and select All
HTML Tags.
- In the Styles box, scroll down and click on the h1
tag. Then click the Modify button.
- At the Modify Style dialog window, click the Format button.
Then click on Font from the pop-up menu.
- At the Font dialog window, select Impact
from the Font box. Then click OK until you return to the Modify Style dialog
window.
- At the Modify Style dialog window, click the Format button.
Then click on Paragraph from the pop-up menu.
- At the Paragraph dialog window, select Center from the Alignment
drop-down menu. Then click OK until you return to the Modify Style dialog
window.
- At the Modify Style dialog window, click the Format button.
Then click on Border from the pop-up menu.
- At the Borders dialog window, click on the Shading tab. From the
Background Color drop-down menu, click on the yellow color. Then click OK until you
exit from the Style dialog window.
- Save the file.
- FrontPage doesn't give you the capability to specify multiple fonts for
the font family. You will need to type the style information for the alternative fonts.
Create the Web Page (with a Link to the Style Sheet)
- Open a blank page in FrontPage.
- Type the text below.
OR
Open the file,
exercise-linked-text.txt.
Copy the text and paste it into the blank page (in Normal view).
Exercise: Using Linked Style Sheets
The linked style sheet formats the body text as 18 points. It also formats a
heading 1 paragraph so that it uses the Impact or Cooper Black font. If those
fonts are not available, then a sans-serif font is used. Heading 1 is also
formatted with a center alignment and a yellow background.
- Change the title that appears in the browser's title bar to:
Exercise: Using Linked Style Sheets.
- Click in the first paragraph. From the Style drop-down menu, select
Heading 1.
- Save the file with the name of:
exercise-linked-fp.htm
.
- From the FrontPage menu, select Format/Style Sheet Links.
- At the Link Style Sheet dialog window, click on the Add button.
- Navigate to the location which contains the style sheet that you wish to
link to.
- Click on the style sheet file name. Click OK.
- Click OK to exit from the dialog window.
- Save the file.
- View the HTML tags by clicking on the HTML tab. You should notice
that a link statement has been added to the Head section of the web page.

Penn State Behrend Home
| Computer Center Home
This page is maintained by Carolyn Dudas
Updated May 21, 2002