NEW

6/recent/ticker-posts

Header Ads Widget

HTML Iframes

 

HTML Iframes

HTML Iframe is used to display a nested webpage (a webpage within a webpage). The HTML <iframe> tag defines an inline frame, hence it is also called as an Inline frame.

An HTML iframe embeds another document within the current HTML document in the rectangular region.

The webpage content and iframe contents can interact with each other using JavaScript.

Iframe Syntax

An HTML iframe is defined with the <iframe> tag:

  1. <iframe src="URL"></iframe>  

Here, "src" attribute specifies the web address (URL) of the inline frame page.

Set Width and Height of iframe

You can set the width and height of iframe by using "width" and "height" attributes. By default, the attributes values are specified in pixels but you can also set them in percent. i.e. 50%, 60% etc.

Example: (Pixels)

  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <h2>HTML Iframes example</h2>    
  5. <p>Use the height and width attributes to specify the size of the iframe:</p>    
  6. <iframe src="https://www.slogantechblog.com/" height="300" width="400"></iframe>    
  7. </body>    
  8. </html>  


Example: (Percentage)

  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <h2>HTML Iframes</h2>    
  5. <p>You can use the height and width attributes to specify the size of the iframe:</p>    
  6. <iframe src="https://www.slogantechblog.com/" height="50%" width="70%"></iframe>    
  7. </body>    
  8. </html>    


You can also use CSS to set the height and width of the iframe.

Example:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <h2>HTML Iframes</h2>    
  5. <p>Use the CSS height and width properties to specify the size of the iframe:</p>    
  6. <iframe src="https://www.slogantechblog.com/" style="height:300px;width:400px"></iframe>    
  7. </body>    
  8. </html>    



Remove the border of iframe

By default, an iframe contains a border around it. You can remove the border by using <style> attribute and CSS border property.

Example:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <h2>Remove the Iframe Border</h2>    
  5. <p>This iframe example doesn't have any border</p>     
  6. <iframe src="https://www.slogantechblog.com/" style="border:none;"></iframe>    
  7. </body>    
  8. </html>    


You can also change the size, color, style of the iframe's border.

Example:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <body>    
  4. <h2>Custom Iframe Border</h2>    
  5. <iframe src="https://www.slogantechblog.com/" style="border:2px solid tomato;"></iframe>    
  6. </body>    
  7. </html>    



Iframe Target for a link

You can set a target frame for a link by using iframe. Your specified target attribute of the link must refer to the name attribute of the iframe.

Example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4.   
  5. <h2>Iframe - Target for a Link</h2>  
  6. <iframe height="300px" width="100%" src="new.html" name="iframe_a"></iframe>  
  7. <p><a href="https://www.slogantechblog.com" target="iframe_a">JavaTpoint.com</a></p>  
  8. <p>The name of iframe and link target must have same value else link will not open as a frame. </p>  
  9.   
  10. </body>  
  11. </html>  


Output

HTML iframes

new.hmtl output code:

<!DOCTYPE html>
<html>
<head>
	<style>

	p{ font-size: 50px;
         color: red;}
</style>
</head>
<body style="background-color: #c7f15e;">
  <p>This is a link  below the ifarme click on link to open new iframe. </p>
</body>
</html>

Embed YouTube video using iframe

You can also add a YouTube video on your webpage using the <iframe> tag. The attached video will be played at your webpage and you can also set height, width, autoplay, and many more properties for the video.

Following are some steps to add YouTube video on your webpage:

  • Goto YouTube video which you want to embed.
  • Click on SHARE ➦ under the video.
  • Click on Embed <> option.
  • Copy HTML code.
  • Paste the code in your HTML file
  • Change height, width, and other properties (as per requirement).

Example:

  1. <iframe width="550" height="315" src="https://www.youtube.com/embed/JHq3pL4cdy4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen style="padding:20px;"></iframe>  
  2.         <iframe width="550" height="315" src="https://www.youtube.com/embed/O5hShUO6wxs" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" style="padding:20px;">></iframe>  


Output:

HTML iframes

Attributes of <iframe>

Attribute nameValueDescription
allowfullscreenIf true then that frame can be opened in full screen.
heightPixelsIt defines the height of the embedded iframe, and the default height is 150 px.
nametextIt gives the name to the iframe. The name attribute is important if you want to create a link in one frame.
frameborder1 or 0It defines whether iframe should have a border or not. (Not supported in HTML5).
WidthPixelsIt defines the width of embedded frame, and default width is 300 px.
srcURLThe src attribute is used to give the path name or file name which content to be loaded into iframe.
sandbox
This attribute is used to apply extra restrictions for the content of the frame
allow-formsIt allows submission of the form if this keyword is not used then form submission is blocked.
allow-popupsIt will enable popups, and if not applied then no popup will open.
allow-scriptsIt will enable the script to run.
allow-same-originIf this keyword is used then the embedded resource will be treated as downloaded from the same source.
srcdocThe srcdoc attribute is used to show the HTML content in the inline iframe. It overrides the src attribute (if a browser supports).
scrolling
It indicates that browser should provide a scroll bar for the iframe or not. (Not supported in HTML5)
autoScrollbar only shows if the content of iframe is larger than its dimensions.
yesAlways shows scroll bar for the iframe.
noNever shows scrollbar for the iframe.

Post a Comment

0 Comments

Recents Post

Exploring Paidwork - Your Gateway to Earning Online