Simple Frames


HTML frames give you a way to display two or more HTML documents at once. Each frame in the browser window displays its own HTML document. Those documents can link to each other or remain completely separate entities.

Frame

A complete HTML document that appears inside of, or alongside, one or more other HTML documents within the same browser window.


Most often, as in Figure 10.1, you'll see frames used as a navigation bar on a Web site. The navigation frame can appear on any side of the document, but you'll find it most often on the top or left margins because English (the language of the majority of Web pages) is oriented from top to bottom and left to right.

Figure 10.1. A simple two-frame document as it appears in the browser. The left frame contains the site's navigation bar and the right frame contains the pages to which the navigation buttons link.


To create frames, you'll need to create a new type of HTML document, called a frameset. A frameset is a special type of HTML document that defines how many frames will be displayed and which HTML documents will appear in each frame. The frameset document for the page is displayed in Figure 10.1.

<!DOCTYPE html      PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"        xml:lang="en" lang="en"> <head> <title>First Frameset</title> <style type="text/css"> </style> </head> <frameset cols="20%,*"> <frame src="/books/1/174/1/html/2/toc.htm" name="left"  /> <frame src="/books/1/174/1/html/2/latin.htm" name="main"  />   <noframes>     <body>     <p><a href="toc.htm">Table of Contents</a></p>     </body>   </noframes> </frameset> </html>


Tip

Did you notice that the <!DOCTYPE> tag changed? In Lesson 2, "Creating Your First Page," you learned that XHTML has three variations. The frameset variation is used whenever you create framed pages.


If you compare this document with a regular HTML document, you should notice right away that the <body> tag is missing. A frameset document uses a new tag, <frameset>, to replace the <body> tag. Within the <frameset> tag, you'll see the <frame /> tag, which is used to describe the contents of each frame, and the <noframes> tag, which is used to instruct the browser what to display in the event the viewer's browser does not support frames, including adding the <body> tag again. Confused? Let's take a closer look at each of these tags.

<frameset>

Within the <frameset> tag, you will need to define the orientation of the framesin vertical columns, cols, or in horizontal rows, rows. This orientation attribute also requires you to define the size of each of your frames. For example, if you have three vertical frames in your frameset, you will need to specify three size attributes. Look again at the <frameset> tag in the preceding HTML sample.

<frameset cols="20%,*">


This tag defines two vertical columns. The first column is 20% of the screen width; the second column fills the remainder of the screen80%. The asterisk (*) tells the browser to fill the remainder of the screen. You can use the same trick if you are defining more than two frames. Although it shows only two values, the following <frameset> tag will actually be used to define three horizontal rows. The first row has been set to 20% of the length of the screen; the asterisk forces the browser to equally divide the remainder of the screen between the other two rows.

<frameset rows="20%,*">


You don't have to let the browser figure out the size of your frames. If you are a perfectionist, you can do your own math and specify the size yourself. Just remember that the total value of the sizes can't be more than 100% of the screen. Now that makes sense, doesn't it?

Tip

You can specify the size of your frames in pixels or as a percentage of the browser window by using the % sign as in the following tag:

<frameset cols="50%,50%">.


You don't have to use the % sign, however. You can use a forward slash (/) as an abbreviation of the % sign, as in the following tag:

<frameset cols="50/,50/">.



<frame />

Like the <img /> tag you learned about in Lesson 8, "Using Graphics," the <frame /> tag uses the src (source) attribute to tell the browser where to find the document to display. The important thing to remember when you are setting up your frameset document is that you are defining the start page for your Web site, or the first framed page in your site. You don't have to figure out every possible combination of pages that might appear, you only have to specify the first one.

The <frame /> tag also requires the name and id attributes. Most people name their frames by their location on the browser window. The <frame /> tags in the following example, for instance, call the frame that appears on the left of the screen, left, and the other frame main because it will hold the main pages of the Web site.

<frame src="/books/1/174/1/html/2/toc.htm" name="left"  /> <frame src="/books/1/174/1/html/2/latin.htm" name="main"  />


Tip

You could name the frames anything (Dog, Cat, Red, or Blue), but you'll find them easier to remember if you stick to something simple.


Following are a few more attributes of the <frame /> tag that might come in handy:

  • frameborder With this attribute, you can remove the small border line that separates the frames. In Figure 10.2, the border has been removed from the sample frameset.

    Figure 10.2. Figure 10.1 with the frameborder attribute set to "0".

  • marginwidth or marginheight These attributes specify (in pixels) the space between the border and the text in the frame.

  • scrolling Using the values of yes, no, or auto, you can tell the browser whether or not to add a scrollbar next to the frame. Don't worry, however; even if you've specified scrolling="no", the browser will display a scrollbar if the content of the frame exceeds the size of the frame.

  • noresize Just like any other window, you can resize frames manually by dragging the frame's border (even when the frameborder="0" attribute has been specified). You can prevent that capability by specifying the noresize attribute in your frameset.

<noframes>

The <noframes> tag that appears in the preceding example tells the browser what to do if it doesn't know how to display frames, or if your visitor has adjusted his browser's settings to refuse frames.

<noframes> <body> <p><a href="toc.htm">Table of Contents</a></p> </body> </noframes>


The <noframes> tag is not required and many Web page authors choose to ignore it, but it takes very little effort to add it and it makes good sense if you want to be certain that everyone will be able to view your Web site.

While it's true that many authors ignore the <noframes> tag, you'll find that just as many authors choose to create an entire non-framed version of their Web site. I happen to think that's overkill.

If you are using frames as a navigation bar, you could make a couple simple changes to your main HTML pages to help people who can't see the frames navigate your site. In Figure 10.3, I've made the same frameset document from Figure 10.1 work for people who can't see frames. By adding a simple one-row table to hold a duplicate set of navigation elements, someone who stumbles upon your page, but can't see the frame on the left, can still navigate the site.

Figure 10.3. Adding a navigation bar to the top of each of the main pages will make this site work for those people who can't view framed pages.




Sams Teach Yourself HTML in 10 Minutes
Sams Teach Yourself HTML in 10 Minutes (4th Edition)
ISBN: 067232878X
EAN: 2147483647
Year: 2004
Pages: 151
Authors: Deidre Hayes

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net