HTML Frames
HTML Frames
Have you ever seen a web page with content that scrolls while the navigation
In this chapter
toolbar or an ad stays in the same place? Pages like these are created using a
web design feature called frames. Frames divide the browser window into How frameset documents are
mini-windows, each displaying a different HTML document (Figure 1). different from other web
documents
The primary advantage of frames is the ability to have one portion of the
window always visible while others scroll through longer content. Frames Setting up a frameset document
open up navigational possibilities, and they can be used to unify information Controlling frame appearance
from several sites onto one page. and functionality
However, frames have been controversial since their introduction in Netscape Targeting frames
Navigator 2.0. They cause as many navigational problems as they solve,
Providing content for users
because some users find it difficult to click through them. It is also difficult without frames
for content in frames to be bookmarked or found by search engines. And
because each framed page is comprised of several HTML documents, this
means more work for developers and a heavier load for the server.
Figure 1. Frames divide the browser into separate windows, each displaying its own web page. The windows can
scroll independently.
1
How Frames Work
faq.html
doclist.html
index.html
Figure 2. This framed document is actually displaying two separate external documents at the same time.
2
How Frames Work
<html>
<head>
<title>Littlechair Index</title>
</head>
1 <frameset cols="175,*">
2 <frame src="doc_list.html">
<frame src="faq.html">
</frameset>
3 <noframes>
You need a frames-enabled browser to view this page.
</noframes>
</html>
Figure 3. This is the HTML source for the frameset document index.html in our example.
1 <frameset>
NOTE (2007)
We’ll talk about creating frameset documents in more detail in the next
section, but for now I want to highlight a few points of interest. First, There is actually a separate Frameset DTD
for the frameset document that should be
notice that the frameset document uses the <head> structural element,
listed in the DOCYTPE declaration. See
but it does not have a <body>. It uses the <frameset> structural element
Chapter 10 of Learning Web Design, 3e for
instead. This sets framesets apart from all other web pages. more information on DTDs and DOCTYPE
The cols attribute in the frameset tag divides the window into two declarations.
frame columns, one 175 pixels wide and the second filling whatever
space is remaining (indicated by the asterisk).
2 <frame>
Within the <frameset> container element, we see a <frame> element for
each frame on the page. The primary job of the <frame> element is to
specify which HTML document to display; however, you can control
other features of a frame, as we’ll see later in this chapter.
3 <noframes>
Finally, there’s some minimal content within the <noframes> element.
This is what will display if the frames do not work (for instance, if the
user is using an ancient browser). It is similar to the alternative text pro-
vided in image elements. We’ll talk more about “noframes” content at
the end of this chapter.
When the browser sees that this is a frameset document, it draws out the
frames as instructed in the document and then pulls the separate HTML
documents into the page.
3
Setting Up a Frameset Document
<html>
<head>
The Finished Product <title>From Jen's Cookbook</title>
The frame demonstration in this </head>
section walks you through the Frameset documents
<frameset>
creation of this three-framed “Jen’s use a <frameset>
Kitchen” page, shown here. tag instead of <body>.
</frameset>
</html>
Figure 4. I begin by adding basic structural tags to a new document using the <frameset> element.
Now we can decide how many rows and/or columns we want the page to
have and what size each should be. These settings are all made within the
<frameset> element.
TRY IT
4
Setting Up a Frameset Document
If you want to divide the page into vertical frames (columns), use the cols
attribute and specify the width measurement for each column, separated by
commas (Figure 5, left). The number of measurements you provide specifies
the number of vertical frames you’ll create. Similarly, if you want to create The number of measurements
horizontal frames (rows), use the rows attribute, followed by the height mea- you provide specifies the
surement for each row (Figure 5, right). number of frames you’ll
When it comes to specifying the measurements, you have some options, as create.
the next section explains.
The cols attribute creates vertical frames. The rows attribute creates horizontal
There are three frames because I provided frames. Two measurement values after the
three measurements after the attribute. attribute create two rows.
HTML TIP
Gridlock
You can combine rows and columns to
make a grid of frames. The frames will
be filled from left to right, top to
bottom. This method gives you less
flexibility than others described in this
chapter.
<frameset rows="*,*"
cols="25%,50%,25%">
<frame src="1.html">
<frame src="2.html">
<frame src="3.html">
<frame src="4.html">
<frame src="5.html">
<frame src="6.html">
</frameset>
5
Setting Up a Frameset Document
Frame measurements
There are three ways to specify sizes for frames:
Absolute pixel values. To make a frame a specific pixel size, enter the num-
Frame measurements
ber of pixels after the rows or cols attribute. The frameset <frameset
can be specified in pixels,
rows="100,400"> creates two horizontal frames, one exactly 100 pixels
percentages, or relative
high, the other exactly 400 pixels high. If the browser window is larger
values. than the combined 500 pixels high, it will enlarge each frame propor-
tionally to fill the window.
Percentages. You can also specify sizes as percentages of the browser win-
dow. The frameset <frameset cols="25%,50%,25%"> creates three col-
umns: the left and right columns always take up 25% of the browser
width, and the middle column takes up 50%, regardless of how the win-
dow is resized.
Relative values. There’s another system that uses asterisks to specify relative
values. The best way to explain this is with an example. The frameset
<frameset cols="100,*"> creates two columns: the left column is exact-
ly 100 pixels wide, and the right column fills whatever portion of the
window is left. This combination of fixed width and flexible width is
one of my personal favorites.
You can also specify relative values in multiples, as in <frameset
cols="100,2*,*">, which creates a 100-pixel-wide column on the left of
the page, then the remainder of the page is divided into two frames; the
middle column is always twice the width of the right column.
That said, let’s start designing the frames for our new page. I’m going to start
Figure 6. I’m designing my cookbook page to with two frames, a narrow one at the top for a banner and the remainder of
have two rows, a narrow one at the top (50 pixels the page for my content (Figure 6).
high) for top-level navigation, and a frame that
fills the remainder of the window for content.
<html>
<head>
<title>From Jen's Cookbook</title>
</head>
<frameset rows="50,*">
50 pixels
</frameset>
</html> The top
frame stays
50 pixels high
regardless of
how the
window is
resized.
6
Setting Up a Frameset Document
<html>
<head>
Exercise 1 (continued)
<title>From Jen's Cookbook</title> STEP 2: Add two rows to
</head> the frameset as shown in
<frameset rows="50,*"> Figure 7. Save the file and
<frame src="header.html"> take a look in a browser. If
<frame src="tapenade.html"> pages aren’t showing up,
</frameset> make sure that the frameset
</html> document is saved in the same
directory as the other files.
Figure 7. For each frame, I’ve added a <frame> tag that tells the browser which HTML document to display in
that frame.
Wait, I just had an idea. I’d like to take that large bottom frame and divide it
into two vertical frames. I can do this by nesting a second frameset inside my
current frameset. Nesting is done by replacing a <frame> element with a
complete frameset (a <frameset> element with its <frame> elements).
In Figure 8, I’ve swapped out my bottom <frame> for a <frameset> containing
two vertical frames.
TRY IT
<html>
<head>
<title>From Jen's Cookbook</title>
Exercise 1 (continued)
</head> STEP 3: Replace the bottom
<frameset rows="50,*"> frame with a two-column
<frame src="header.html"> frameset as shown in Figure 8.
<frameset cols="150,*"> Save and view the frameset in
Frameset Nested <frame src="links.html"> a browser.
Frameset <frame src="tapenade.html">
</frameset>
</frameset>
</html>
Figure 8. To nest frames (fill one frame with another frameset), simply replace a <frame> with a complete
<frameset>. You can do this as many levels deep as you like. Just make sure you close your <frameset> tags
properly.
7
Setting Up a Frameset Document
jenscookbook.html
header.html
links.html
tapenade.html
Figure 9. My framed cookbook page as it looks in the browser. Below you can see the separate HTML documents
that are held together by the frameset.
This is a good start, but I can make some adjustments to make the page less
clunky.
Borders
The last decision I can make at the frameset level (i.e, within the <frameset>
element) is whether I want borders to appear around my frames. If you don’t
specify otherwise, frames will be divided by thick 3-D bars (as shown in You can choose whether to
Figure 9). To control borders for the entire frameset, use the frameborder and have 3-D borders display
border attributes in the <frameset> element.* around your frames.
To turn off the borders completely, making a smooth transition between
frames, simply set the border attribute to 0. This works for Netscape
Navigator and Microsoft Internet Explorer, Versions 4 and higher.
You might want to control just the thickness of the borders. If this is the case,
set frameborder to 1 (“on”) and use the border attribute to specify a pixel
thickness.
I definitely don’t want borders around my frames, so I’m turning them off at
the frameset level (Figure 10).
We’ve done everything we can do with the whole frameset. Now let’s see the
kinds of things we can tweak within each frame.
<frameset rows="50,*">
<frame src="header.html">
<frameset cols="150,*">
<frame src="links.html">
<frame src="tapenade.html">
</frameset> TRY IT
</frameset>
* The border attribute is not part of the standard HTML specification, but it works fine in the
current versions of the major browsers.
Creating a Frameset
Here’s how you create a new frameset in three of the more popular authoring programs.
Dreamweaver MX 1
One way to create a
frameset document
is to add a frame 2 Alternatively, you can click on a predefined frame icon from
to the current the Frames panel of the Object window.
document with the
Modify Frameset
3 The Frames window (accessed from the Windows menu) is
menu.
used to manage the frameset and its settings.
4 When you select the whole frameset in the Frames window, you
can make frame-level settings in the Properties window.
GoLive 6
Frames tab
1
First, select the
Frame Editor tab at
the top of the
document window
to switch to the 2 From the Frames tab of the Palette,
Frames Editor view. drag a frame configuration into
This view is used to Frames Editor tab your document window.
set up and organize
the framed 3 Select the frameset by clicking on any
document. border (you can drag the border to resize
the frame) and make your frameset
settings in the Inspector palette.
FrontPage 2002
1
Create a new document and
select the Frames Pages tab.
There are a variety of frame
templates, each with a short
description of suggested use.
Choose the frameset closest to
the layout you want.
2
Pages can then be set or
created for each frame. The
frames can be modified by
moving the frame borders.
10
Frame Appearance and Function
Exercise 1 (continued)
STEP 5: Turn off scrolling in
the top frame as shown in
Figure 11. Save and view.
Figure 11. I’ve removed the scrollbar from my top frame by setting the scrolling attribute to no in the
<frame> element.
11
Frame Appearance and Function
Setting margins
Browsers automatically add a little space between the edge of the frame and
its contents, just as they do for a web page in the browser. You can control the
BUG ALERT margin amount inside each frame by adding extra space or setting the con-
tents flush to the frame’s edge.
Margins Bug in The marginheight attribute controls the pixel width of the margin at the top
Navigator and bottom edges of the frame. The marginwidth attribute controls the space
There’s a weird bug in on the left and right edges. Figure 12 shows examples of these attributes.
Netscape Navigator Versions
4.0 and earlier that leaves a
1-pixel margin even if the <frameset cols="*,*">
marginheight and marginwidth <frame src="sweetpea.html" marginheight="24" marginwidth="12">
attributes are set to 0. There’s <frame src="sweetpea.html" marginheight="0" marginwidth="0">
not much you can do except </frameset>
camouflage it with a matching
background color or image.
Netscape 6.0 seems to have
corrected the problem. 0px
24px
12px 0px
Figure 12. The marginheight attribute controls the amount of space between the top and bottom edges of the
frame and its contents. marginwidth controls the space on the left and right edges.
Exercise 1 (continued)
STEP 6: Set the margins to 0 in
the top frame as shown in
Figure 13. Save and view.
Figure 13. With marginwidth and marginheight set to 0, my graphic is positioned flush to the top-left
corner of the frame, with no extra space.
12
Frame Appearance and Function
Frame resizing
By default, users can resize your frames, overriding your careful size settings,
simply by clicking on and dragging the border between frames. You can pre-
vent them from doing this by plopping the noresize attribute in the <frame>
element. I’d like to make that top frame stay put, so I will add the noresize
attribute there (Figure 14). (I had to temporarily turn on my frame borders to
demonstrate the noresize trick; I’ll turn them off again for the final prod-
uct.)
TRY IT
Figure 14. Frames are resizable by default. The noresize attribute prevents this behavior.
Before you go setting all your frames to noresize, consider whether there
might be a good reason to allow resizing (such as to view more text in the
screen). In my example, users aren’t gaining anything by resizing that top
frame, so I restricted the ability to change it.
13
TOOL TIP
Formatting Frames
Here’s how you format individual frames in three of the more popular authoring programs.
Dreamweaver MX
GoLive 6
1
While in Frames Editor view,
place the cursor in a frame.
2
You can then control all the frame-level
settings in the Frame Inspector. You can
also preview your HTML document
within the frame by clicking on the
Preview button.
14
Targeting Frames
Targeting Frames
Now that you’ve gotten the hang of setting up framed documents, it’s time to
tackle one last aspect of frames: making sure linked documents load into the
correct frame.
When you click on a link in an ordinary browser window, the new page
replaces the current one in the browser window. The same thing happens by
default within a frame. When you click on a link in a frame, the linked
document will load in that same frame (after all, a frame is just a mini–
browser window).
In many cases, however, you want the linked document to load in a different
frame, such as when you have a list of links in one frame and your content
Setting the Target
in another. In these instances, you need to tell the link which frame to use— for a Whole Document
in other words, you need to target a specific frame. If you want all the links on a page to
point to the same window, you can
specify the target in the header of
Naming the frame the document using the <base>
element, as follows:
Before you can target a frame, you need to give it a name using the name
<head>
attribute in the <frame> element 1 (Figure 15, following page). I’d like to load <base target="main">
my content documents into the main frame on the page, so I’ve given that </head>
frame the name “main”. With this specification in the head of
<frame src="salmon.html" name="main"> the document, all the links on that
page will automatically load in the
“main” frame (unless specified
Targeting the frame otherwise in the link). This technique
saves extra typing and keeps the file
Now I can point to that frame from any link 2. My left frame contains a
size down.
document (links.html) with a list of links. Within links.html, I add the
target attribute to each of my links and set the value to “main”. When some-
one clicks on that link, the browser will load the new document in the frame
called “main”:
<a href="risotto.html" target="main">wild mushroom risotto</a><br>
15
Targeting Frames
jenscookbook.html
<p>
<img src="graphics/maindishes.gif" alt="Main Dishes"><br>
<a href="salmon.html" target="main">garlic salmon</a><br>
<a href="risotto.html" target="main">wild mushroom risotto</a><br>
<a href="blackbean.html" target="main">fish in black bean sauce</a><br>
<a href="clamsauce.html" target="main">linguini with clam sauce</a><br>
</p>
links.html
salmon.html
16
Targeting Frames
_parent
This target name causes the linked document to load into the parent
frame (the frameset that is one step up in the nested frame hierarchy). TIP
This causes some breaking out as well, but only to the next frame Linking between frames can be
level. tricky. Pay attention to what
you’re doing and test all of your
_self links in a browser to make sure
This causes the document to load in the same frame. Since this action is they’re behaving the way you
the default for all frames, you generally don’t need to specify it. However, intend.
it might be useful if you need to override a target set with the <base>
element introduced earlier.
_blank
A link with target="_blank" opens a new browser window to display the TRY IT
linked document. This is not necessarily a frame-related value—you can
use it from any web page. Bear in mind, however, that each time a link Exercise 1 (continued)
that targets _blank is clicked, the browser launches a new window, poten- STEP 8: Name each of
tially leaving the user with a mess of open browser windows. By contrast, the frames as shown in
Figure 15.
when a target window is given a specific name (such as sample or new), it
will be reused by all links that specify that name. STEP 9: Set the appropriate
targets for the links so the
I’m going to need to take advantage of the _top value in my documents. The frameset functions properly:
top frame contains a graphic link to the home page. If I leave it as it is, the • In header.html, make sure
home page will load in that little sliver of a frame. To break out of the frames that the home graphic
and get back to a normal browser window, I’ll target the top level in that link links to the top level of
(Figure 16). the browser window
(Figure 16).
• In links.html, use the
<a href="index.html" target="_top"><img src="graphics/jk-home.gif"
border="0" alt="HOME"></a> <base> element in the
<head> of the document to
make all of the links on the
header.html page link to the main
window.
Save and view the frameset in
the browser. Test the links to
Lo a
l in
“_t o testing). Be sure the links back
p”
to the home page on each
recipe page load in the top
frame.
Note that this is only an
exercise and not a fully
designed and functioning site.
In the files provided there is
no way to get back to the
frameset from the sample
home page document.
Figure 16. The _top target value makes the home page break out of the frames and load in the top level of the
browser window.
17
Content for Users Without Frames
<html>
<head>
<title>From Jen's Cookbook</title>
</head>
<frameset rows="50,*" border="0" frameborder="0" frameborder="no">
<frame src="header.html" marginwidth="0" marginheight="0" scrolling="no">
<frameset cols="150,*">
<frame src="links.html">
<frame src="tapenade.html" name="main">
</frameset>
</frameset>
<noframes>
<body background="graphics/bkgd-grid.gif">
<center>
<img src="graphics/jensbook.gif">
<p>[NOTE: This page is best viewed with a frames-enabled browser.]</p>
<p>
<img src="graphics/appetizers.gif" alt="Appetizers"><br>
<a href="tapenade.html" target="main">tapenade (olive spread)</a><br>
<a href="purses.html" target="main">black bean phyllo purses</a><br>
<a href="mangosalsa.html" target="main">mango chipotle salsa</a><br>
<a href="springroll.html" target="main">fresh spring rolls</a>
</p>
18
Test Yourself
Test Yourself
Answers to these frames questions can be found in the Appendix.
1. What makes frameset documents different from all other HTML
documents?
2. What are the disadvantages of frames that have made them unpopular
in the the industry?
3. In the space on the right, sketch the frameset that results from the
following code:
a. <frameset cols="*,*,*">
b. <frameset rows="*,3*">
4. How do you make a linked document appear in the top level of the
browser, instead of within the frame?
5. Write the code for the frameset shown in Figure 18. Place a document called
empty.html in each frame. Your column and row measurements can be
approximate.
19
HTML Review: Frame Tags
20