How to Use Frames in HTML with Example Tutorial

In the realm of web development, HTML is a tool that enables us to create and structure content on the World Wide Web. One of its many features is the ability to implement frames, which allow us to divide a webpage into smaller, distinct areas. These frames can then display different content or navigate independently. In this guide, we will delve into how to use frames in HTML, accompanied by practical examples.

Inserting and styling images in HTML 5.
Inserting and styling images in HTML 5.

Before we proceed, understanding that the use of frames in web design has largely been outdated due to their negative impact on accessibility, SEO, and responsive design. However, for historical or specific use cases, knowing how to use them can still be beneficial. Always consider modern web design practices when deciding to use frames.

an info sheet with the text basic html structure in blue and white, on top of it
an info sheet with the text basic html structure in blue and white, on top of it

The Basic Structure of an HTML Frame

An HTML frame consists of two main elements: the <frameset> tag and the <frame> tag. The <frameset> tag defines the number of frames, their size, and their respective positions within the webpage. The <frame> tag, on the other hand, specifies the content material of each frame.

Embedding Images in HTML using <img>
Embedding Images in HTML using <img>

Here's a simple example of an HTML page that uses two frames, side by side. The left frame is 30% of the webpage width, while the right frame takes up the remaining 70%.

Using the <frameset> Tag

Learn HTML Basics: Introduction to HTML Structure & Elements
Learn HTML Basics: Introduction to HTML Structure & Elements

The <frameset> tag is a container for the <frame> tags. You can specify the rows and columns properties to divide the webpage into frames. Here's how to use it:

<frameset cols="30%, 70%">

Using the <frame> Tag

an image of a web page with the text'day 17 iframe '
an image of a web page with the text'day 17 iframe '

The <frame> tag is used within the <frameset> tag to define each individual frame. You can specify the source of the content (src attribute), the name of the frame (name attribute), and more. Below is an example of two <frame> tags within a <frameset> tag:

<frame src="left_frame.html" name="leftFrame">

<frame src="right_frame.html" name="rightFrame">

a handwritten diagram with the words html and an image of a computer screen on it
a handwritten diagram with the words html and an image of a computer screen on it

Navigating Between Frames

You can navigate between frames using JavaScript. The following example shows how to switch from one frame to another:

HTML Tutorial For Beginners
HTML Tutorial For Beginners
🧾 How to Create Tables in HTML
🧾 How to Create Tables in HTML
an image of a computer screen with the words inline frame in it's bottom corner
an image of a computer screen with the words inline frame in it's bottom corner
the ultimate guide to creating and formating tags
the ultimate guide to creating and formating tags
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Cycle Completed! Prime Python, Java, JS Services: Loop Closed – Order for Infinite Innovations!
Basic HTML Structure
Basic HTML Structure
Very Simple HTML CSS Image Gallery
Very Simple HTML CSS Image Gallery
an image of a web page with the words'body'and'reader '
an image of a web page with the words'body'and'reader '
About HTML tags
About HTML tags
an info sheet with the words, tags and key points on it's page
an info sheet with the words, tags and key points on it's page
the page structure in html5
the page structure in html5
an image of a computer screen with the names of different types of web pages on it
an image of a computer screen with the names of different types of web pages on it
How To Create Triangles With HTML & CSS
How To Create Triangles With HTML & CSS
an image with the text how to place text over image in html css
an image with the text how to place text over image in html css
Complete Beginner HTML Master Notes
Complete Beginner HTML Master Notes
the three ways to apply css in html and wordpress, including an info sheet
the three ways to apply css in html and wordpress, including an info sheet
an image of a computer screen with the words canvass in html and csss
an image of a computer screen with the words canvass in html and csss
Design Elements in HTML & CSS
Design Elements in HTML & CSS

Let's assume you have two frames named "rightFrame" and "leftFrame". To switch content from "rightFrame" to "leftFrame", you wouldn't update the content of "rightFrame" directly:

document.rightFrame.location.href = "new_right_frame.html";

Instead, you update the content of "leftFrame" to reload "new_right_frame.html". This way, you're essentially making the navigation back to the "leftFrame" from the "rightFrame", leading to a more seamless transition. Here's how to accomplish this:

Creating a Navigation Function

First, create a navigation function in JavaScript that will handle switching frames. This function should take the frame name and the new source as parameters:

function navigate(framename, newsrc) {

document.getElementById(framename).src = newsrc;

}

Calling the Navigation Function

Then, in your HTML, you can create a button that calls this function:

<button onclick="navigate('leftFrame', 'new_right_frame.html')">Switch frames</button>

This way, when the button is clicked, the frames will switch without refreshing the entire webpage.

Targeting Frames in Links

Another common use of frames is displaying external content within an iframe. An iframe is an inline frame that displays another webpage within a webpage. Here's how you can make a link open in a frame:

To make a link open in a frame named "leftFrame", you need to specify this in the <a> tag's target attribute:

<a href="https://www.example.com" target="leftFrame">Visit Example</a>

This will open the linked webpage in the existing "leftFrame", overriding its current content.

Remember, using frames can lead to accessibility issues and search engine optimization challenges. Use them judiciously and only when absolutely necessary. Modern web design frameworks offer more accessible and SEO-friendly solutions for multi-part webpages. Always consider these factors when deciding to use frames.

Related Articles

Www Tutorial Pasoapaso Com What Is Hyperlink In Html In Simple Words Explain Form Tag In Html With Registration Page As Example How To Remove Hyperlink In Html Code What Is The Tag To Add Forms In Html Css How To Start Html In Visual Studio Code What Is Form In Html Html Form Tag W3 Que Paso Gif How To Write Html In Vscode