Frames Tutorial: Creating Frame-based websites
Frames are a feature of HTML that allow browsers to put more than one page in the browser window at one time. This is done by dividing the main browser screen into smaller sections. These smaller sections are called frames, hence the name 'Frames'. Each of these smaller sections can contain a different page.
You can compare frames page with a picture frame on your desk, showing two or more images of your family members. Each image is a separate "page" or "frame", and all of them are held together by the "pictureframe" or "frameset".
A link on one page of the frames can change the page in the same frame section or in other ones. Because of this frames can sometimes yield unexpected results, particularly when using the BACK button.
If you have been navigating within a frame and then press BACK, you will go back within the frameset instead of going back to the previous page / other URL.
Frames are used for many things. The most common of these is for navigation around a web site. Frames provide a very neat solution to getting from place to place within a site. Frames also provide an additional artistic element for creating pages.
Frames can add an extra dimension to your page. As with many things, it is possible to get carried away with frames. Putting them onto your site just to be using them can distract from your page, but used judiciously, frames can make your page stand out.
Step 1: Decide how many rows or columns you want
Remember, frames are a way of dividing the browser's screen into different independent portions. In the "simple" case, you will be dividing the screen into columns or rows. For this example, we'll create a page with two columns.
Step 2: Create the pages that make up the rows or columns.
The most important thing to remember about frames is that a frameset consists of more than one HTML page.
If you have a page with TWO frames in it, THREE HTML files are needed:
- one for each of the frames
- and one that describes how the frameset will be laid out.
Remember that multipart frame with family pictures which I have already talked about? You need several images AND the picture frame to display them on your desk.
In this case, we'll say that you've already created two HTML pages, column1.htm and column2.htm. The former will be shown in the left frame, and the latter will be shown in the right frame of the browser window.
If you need to know how to set up such HTML documents please refer to our Basic HTML tutorial at: www.pcrobin.com/hpu/basic1.html
Step 3: Create the frameset page.
The next step is to create the frameset page that describes how the page is laid out.
Throughout this tutorial we'll be showing you HTML code. We'll add a period between the opening < and the tag. This is done to show you the code rather than the result of the code. When you write your HTML, please omit the period and use and not <.tag>
Here's the HTML for our frameset page:
<.HTML>
<.HEAD>
<.TITLE>Frameset Page<./TITLE>
<./HEAD>
<.FRAMESET COLS="20%, *">
<.FRAME SRC="column1.htm">
<.FRAME SRC="column2.htm">
<./FRAMESET>
<./HTML>
When viewed in a browser, this page will be divided into two columns, showing
the two files that you created: column1.htm and column2.htm.
Notice that this page doesn't use any BODY tag - frameset pages can't be
rendered with a BODY tag as they don't present any information.
The FRAMESET tag defines the "frame" for the columns or rows you want to use
to split your browser screen. It's like the frame which holds your different
family pictures on your desk.
The FRAME tag defines a single window in the frameset - a single family image
within the multipart frame on your desk.
To create a page with two rows instead of two columns, simply use ROWS
instead of COLS in the FRAMESET tag:
<.FRAMESET ROWS="20%, *">
Step 4: Set the relative sizes of the rows or columns
Observant readers will notice the following line in the above example:
<.FRAMESET COLS="20%, *">
This line determines the relative sizes of the two columns. In this case, the left column is 20% of the height of the window, and the right column takes up the remaining 80%. There are four ways to tell the browser how tall or wide to make a column:
1. Percentage of screen height or width, as above.
2. Number of pixels. To make the first column 200 pixels wide, use
COLS="200,*".
3. Use "*", meaning "whatever is left over." The above example also uses this method.
4. Use "n*", where n is a number. This means "n parts of what's left over."
Thus ROWS="*, 2*" makes two rows where the first one is 1/3 the height of the screen and the second is 2/3 the height of the screen.
And ROWS="100,2*,*" creates three rows: one 100 pixels tall, one that takes up 2/3 of the remaining space, and one that takes up the last 1/3.
Step 5: Decide where the links should go
Because each frame is a separate browser window, clicking a link in one of the frames will go to a new page in that frame/window. In some cases that may not be what you want. It's possible to create a link that, when clicked, makes a new page show up in another frame.
To do this, you need two steps:
1. Name the frames:
<.HTML>
<.HEAD><.TITLE>Frameset Page<./TITLE>
<./HEAD>
<.FRAMESET COLS="20%, *">
<.FRAME NAME="firstcolumn" SRC="column1.htm">
<.FRAME NAME="secondcolumn" SRC="column2.htm">
<./FRAMESET>
<./HTML>
Notice the change in the FRAME tag: Each of them has a new attribute called NAME. This attribute assigns a name to the frame window which can be used to refer to it when calling links or executing scripts.
2. Tell your links where to go:
Let's say you want to be able to click a link in the left frame and have a new page come up in the right frame. To do this, place the following link in column1.htm:
<.A HREF="newpage.htm" TARGET="secondcolumn"> Click here to see "newpage.htm" in the right frame<./A>
Notice that the link tag gets a new attribute TARGET. This attribute causes the link to be opened in the frame with the name it's calling up, i. e. the right frame in this case.
There are a few default TARGET attributes which you should know about:
1. TARGET="_TOP"
If you want to be able to click a link and have a new page fill the complete browser window (replacing the complete frameset or "breaking out of frames"), use TARGET="_TOP" as follows:
<.A HREF="newpage.htm" TARGET="_TOP"> Click here to see "newpage.htm" take up the whole window<./A>
2. TARGET="_BLANK"
And if you want the page to come up in an entirely new browser window, useTARGET="_BLANK" as follows:
<.A HREF="newpage.htm" TARGET="_BLANK"> Click here to see "newpage.htm" come up in a new window<./A>
Note: This works even on pages that do not have frames. To avoid cluttering the user's desktop with windows, this feature should be used sparingly.
3. TARGET="_parent"
If you have pages in nested framesets and only want to update the frameset of the page with the link please use the _parent target like this:
<.A HREF="newpage.htm" TARGET="_PARENT"> Click here to see "newpage.htm" come up in the parent frameset window of this page<./A>
NOTE: If you have registered your frames page with Hometown AOL please use TARGET="_PARENT" instead of TARGET="_TOP" so that the link correctly replaces the frameset of your page.
4. TARGET="_SELF"
If you want to make sure that the link replaces the page with the link please use this target attribute:
<.A HREF="newpage.htm" TARGET="_SELF"> Click here to see "newpage.htm" replace the page with the link<./A>
Review:
Let's quickly summarize what we have just learned:
If you want to use a page with frames and one of them is used as a
navigational frame you will have to go through two steps to make sure that all the links don't replace the navigation bar:
1. Name your frame windows.
2. Target your links to appear in the correct frame window by calling up that name in the TARGET attribute with the link.
Step 6: Do you want users to be able to resize and scroll?
The default setting for the scrolling in frame windows is "AUTO" - it
instructs the browser to decide whether scrollbars are needed, and place them where necessary.
<.FRAME SRC="column1.htm"> is the same as using
<.FRAME SRC="column1.htm" SCROLLING=AUTO>
If you want to have those scrollbars always show up, add a SCROLLING
attribute to the FRAME tag and set it to "YES" like this:
<.FRAME SRC="column1.htm" SCROLLING=YES>
If you do not want a frame to have scrollbars, add a SCROLLING attribute to the FRAME tag and set it to "NO" to disable the scrollbar:
<.FRAME SRC="column1.htm" SCROLLING=NO>
By default all frames are resizable - if you do not want users to be able to resize a frame, add a NORESIZE attribute to the FRAME tag:
<.FRAME SRC="column1.htm" NORESIZE>
Step 7: Do you want 3-D borders around your frames?
By default, frameset pages have 3-D borders between the frames. For a completely seamless look, change the FRAMESET tag to the following: