HTML Iframe
An HTML <iframe>
(inline frame) is used to embed another document within the current HTML document. It allows you to include external content, such as another webpage, within your webpage.
Basic Syntax
Here's the basic syntax of an <iframe>
element:
<iframe src="URL" width="width" height="height" frameborder="border" allowfullscreen></iframe>
src
: Specifies the URL of the page to display.width
: Sets the width of the iframe.height
: Sets the height of the iframe.frameborder
: Specifies whether or not to display a border around the iframe (deprecated in HTML5, use CSS instead).allowfullscreen
: Allows the iframe to be displayed in full-screen mode.
Examples
1. Simple Iframe
This example embeds the page from "https://www.example.com" with a width of 600 pixels and a height of 400 pixels:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Iframe</title>
</head>
<body>
<h1>Embedding Example</h1>
<iframe src="https://www.example.com" width="600" height="400"></iframe>
</body>
</html>
2. Iframe with Border and No Scrollbars
This example uses CSS to remove the border and scrollbars from the iframe:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iframe with CSS</title>
<style>
iframe {
border: none;
overflow: hidden;
}
</style>
</head>
<body>
<h1>Iframe Without Border</h1>
<iframe src="https://www.example.com" width="600" height="400"></iframe>
</body>
</html>
3. Iframe with Fullscreen Capability
This example adds the allowfullscreen
attribute to enable fullscreen mode:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fullscreen Iframe</title>
</head>
<body>
<h1>Iframe with Fullscreen</h1>
<iframe src="https://www.example.com" width="600" height="400" allowfullscreen></iframe>
</body>
</html>
Key Considerations
-
Security: Be cautious when embedding content from external sources, as it could potentially expose your site to security risks. Use attributes like
sandbox
to limit the capabilities of the embedded content. -
Responsiveness: If you're working on a responsive design, make sure the iframe adjusts properly across different screen sizes. You can use CSS to make the iframe responsive.
-
Cross-Origin Restrictions: Some websites may have restrictions on being embedded in iframes due to security policies (e.g., X-Frame-Options).
Feel free to ask if you have any more questions about iframes or need further examples!
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.