HTML iFrame
HTML Iframes - Codes With Pankaj
Welcome back to codes with pankaj ! In this tutorial, we’ll explore how to use HTML iframes to embed other webpages within your document, using HTML attributes alone.
What is an HTML iframe?
An iframe (short for inline frame) is an HTML element that allows you to embed another webpage within your HTML page. This is especially useful for displaying external content such as videos, maps, and interactive web pages.
Step 1: Creating a Basic iframe
To start, let's create a basic iframe to embed an external website.
Example
In this example:
The
src
attribute specifies the URL of the webpage you want to display inside the iframe.The
width
andheight
attributes set the iframe dimensions.
Step 2: Removing Borders from an iframe
To create a clean, borderless look, you can set frameborder
to 0
.
Example
In this example:
frameborder="0"
removes the default border around the iframe.
Step 3: Disabling Scrollbars in an iframe
If the embedded page is larger than the iframe’s dimensions, scrollbars appear by default. To disable them, use the scrolling
attribute.
Example
In this example:
scrolling="no"
disables the scrollbars, which can create a cleaner look if you’re sure all content fits within the iframe.
Step 4: Adding Security with the sandbox
Attribute
sandbox
AttributeThe sandbox
attribute restricts the functionality of the iframe, improving security by controlling what the iframe can do.
Example
In this example:
sandbox
limits the iframe's abilities.allow-same-origin
permits access to content from the same domain.allow-scripts
allows JavaScript to run within the iframe (use cautiously).
Step 5: Embedding YouTube Videos
To embed a YouTube video, you can use the video URL directly as the src
of the iframe.
Example
In this example:
allowfullscreen
enables full-screen mode on the video.Additional settings like
accelerometer
,autoplay
,encrypted-media
,gyroscope
, andpicture-in-picture
specify allowed interactions.
Conclusion
You now have the basics of HTML iframes! You’ve learned how to embed external content, adjust dimensions, remove borders, disable scrollbars, enhance security, and even embed YouTube videos—all using just HTML.
Experiment with these attributes and enjoy enhancing your webpages with interactive iframe content.
Happy coding!
Last updated