Introduction Document Structure Cascading Style Sheets Text Formatting Lists Links Tables Images Forms Frames Appendix - XHTML Elements Site Home

XHTML Reference Guide

Frames

What is a Frame?

Frames are a way of arranging and presenting several web pages at once. A frame is a web page within its own pane in the browser window. They can be organised horizontally, vertically or in a grid style.

A common use for frames is to have one frame at the top or side of the browser window displaying a menu or table of contents. When an item is clicked on, the remaining pane is refreshed with the selected page.

Note that if using frames, the Frameset DTD must be specified.

Top

Creating a Frameset Document

To create a Web page which contains frames, an initial document known as a frameset document must be created. This instructs the browser how to split the window up into multiple frames.

A frameset document will look like the following:

<html>
<head>
<title>Frame Example</title>
</head>
<frameset rows="80,*">
   <frame src="menu.html" name="top">
   <frame src="InitialBlurb.html"  name="main">
</frameset>
</html>

The first thing to note is that the <body> element has been replaced by the <frameset> element. The <frameset> element contains the rows attribute. This means that the frames will be arranged in horizontal rows. To have them arranged vertically, the cols attribute could have been used instead.

The sizes of the rows or columns (i.e. height for a horizontal row and width for a vertical row) must be specified as a specific pixel value, or as a percentage of the total size of the browser window, or as a relative length. See the table below for some examples of the rows and cols values.

rows="20%,80%"The first row is set to 20% of the height of the browser window and the second row to 80%.
cols="200,400"The first column is set to a width of 200 pixels and the second column 400 pixels.
rows="3*,*"The first row is set to 3 times the height of the second row.
rows="80,*"The first row is set to 80 pixels in height and the '*' means that the second row will take the remaining available space.
rows="50, 2*, *, 80"This example defines a four-row frame. The first is 50 pixels high and the fourth is 80 pixels high. The second occupies two-thirds of the remaining height, while the third occupies the final one-third of the remaining height.

In the example above, menu.html would open in the top frame which has been set to 80 pixels high and InitialBlurb.html would appear in the lower frame taking the remainder of the available space.

In the case where an old browser is being used which doesn't support frames, it is possible to display some alternative text to that effect. This is done by using a normal <body> element within the <noframes> </noframes> tags as in the following example.

<html>
<head>
<title>Frame Example</title>
</head>
<frameset rows="80,*">
   <frame src="menu.html" name="top">
   <frame src="InitialBlurb.html"name="main">
</frameset>
<noframes>
<body>
   <p>Oh dear! Can't see what you're expecting to.  Tough.
   Why don't you do yourself a favour and get a browser that supports frames.></p>
</body>
</noframes>
</html>
Top

Linking Between Frames

Apart from the frameset document, the web pages with which the frames will be loaded do not require any special attention other than the controlling one. using the example above, menu.html would look something like this.

<html>
<head>
<title>Frame Menu</title>
</head>
<body>
   <ul>
      <li><a href="whatframe.html" target="main">What is a Frame?</a></li>
      <li><a href="cre8frameset.html" target="main">Creating a Frameset Document</a></li>
      <li><a href="linkframe.html" target="main">Linking between Frames</a></li>
      <li><a href="nestframe.html" target="main">Nested Frames</a></li>
   </ul>
</body>
</html>

When the frameset document is opened, it will display the documents specified by the <frame> elements; in our example, 'menu.html' in the top row and 'Initial Blurb.html' in the bottom. When one of the links is clicked, the frame displaying 'Initial Blurb.html' will be refreshed with the document specified in the link. The target attribute references the ids of the frames defined in the frameset.

If the target attribute is missing from an anchor tag, it will refresh the current frame (i.e. the frame displaying the menu). There are some special target values provided which can control where the required document is displayed.

_blank
Loads the requested document into a new browser window.
_top
Replaces the frames in the current window with the requested document only.
_parent
Same action as _top but if the frames are nested it will refresh the parent frame.
_self
Loads the document into the current frame. This is the same action as when the target attribute is missing.

Note that target="_blank" is also often used in anchor tags to force a web page to open in another instance of the browser. This attribute has been unfortunately deprecated in XHTML Strict, but is available in Transitional and Frameset.

Top

Nested Frames

A grid may be set up by nesting framesets to define a combination or rows and columns.

<frameset rows="70%,30%">
   <frameset cols="33%,33%,34%">
      <frame id="Photo1" src="Row1_Column1.html">
      <frame id="Photo2" src="Row1_Column2.html">
      <frame id="Photo3" src="Row1_Column3.html">
   </frameset>
   <frameset cols="33%,33%,34%">
      <frame id="Caption1" src="Row2_Column1.html">
      <frame id="Caption2" src="Row2_Column2.html">
      <frame id="Caption3" src="Row2_Column3.html">
   </frameset>
</frameset>

This has the effect of producing a window which is split into frames as below:

Photo1Photo2Photo3
Caption1Caption2Caption3

The same may also be written like this:

<frameset rows="70%,30%" cols="33%,33%,34%">
   <frame id="Photo1" src="Row1_Column1.html">
   <frame id="Photo2" src="Row1_Column2.html">
   <frame id="Photo3" src="Row1_Column3.html">
   <frame id="Caption1" src="Row2_Column1.html">
   <frame id="Caption2" src="Row2_Column2.html">
   <frame id="Caption3" src="Row2_Column3.html">
</frameset>

If a frame is not large enough to display its contents, vertical and/or horizontal scroll bars are enabled.

Top

Formatting Frames

By default a border is used to separate the frames. This may be removed by setting the frameborder attribute to 0 in the frame tag.

<frame name="menu.html" frameborder="0">

Note that this just removes the border of the specified frame. To completely remove all borders, a frameborder value of 0 must be specified on any adjacent frames also.

Other attributes available for use in the <frame> tag are:

marginwidth
Sets the left and right margins of the frame (in pixels)
marginheight
Sets the top and bottom margins of the frame (in pixels)
scrolling
Controls the display of the scrollbar. Values are: "auto" (only provides scrollbars when required), "yes" (always provides scrollbars) or "no"(suppresses scrollbars.)
noresize
Prevents the resizing of the frame.
Top

Inline Frames

Instead of committing the complete page to frames, you can designate a region of the page to be a frame by using the inline frame element <iframe>. The src attribute is used to specify the URL of another HTML document that you want displayed in this frame. All the attributes that can be used with frames can be used with iframe.

In the following example, the intoduction page is being displayed in the iframe.

<iframe src="XHTMLFrames.html" width="700" frameborder="no" ></iframe>
Top

Valid XHTML 1.0! Valid CSS2