Wednesday, September 23, 2020

HTML iFrame | Video and Audio

Introduction to iframe

An iframe or inline frame is used to display external objects including other web pages within a web page. An iframe pretty much acts like a mini web browser within a web browser. Also, the content inside an iframe exists entirely independent of the surrounding elements. The HTML <iframe> tag defines an inline frame, hence it is also called as an Inline Frame.

The basic syntax for adding an iframe to a web page can be given with:

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

The URL specified in the src attribute points to the location of an external object or a web page.


Setting Width and Height of an iFrame

The height and width attributes are used to specify the height and width of the iframe.

Example

<iframe src="hello.html" width="400" height="200"></iframe>

You can also use CSS to set the width and height of an iframe, as shown here:

<iframe src="hello.html" style="width: 400px; height: 200px;"></iframe>

Note: The width and height attribute values are specified in pixels by default, but you can also set these values in percentage, such as 50%, 100% and so on. The default width of an iframe is 300 pixels, whereas the default height is 150 pixels.

 

Removing Default Frameborder

The iframe has a border around it by default. However, if you want to modify or remove the iframe borders, the best way is to use the CSS border property.

The following example will simply render the iframe without any borders.

Example

 <iframe src="hello.html" style="border: none;"></iframe>

Similarly, you can use the border property to add the borders of your choice to an iframe. The following example will render the iframe with 2 pixels blue border.

Example

 <iframe src="hello.html" style="border: 2px solid blue;"></iframe>

 

Using an iFrame as Link Target

An iframe can also be used as a target for the hyperlinks. An iframe can be named using the name attribute. This implies that when a link with a target attribute with that name as value is clicked, the linked resource will open in that iframe. Let's try out an example to understand how it basically works:

Example

 <iframe src="demo-page.html" name="myFrame"></iframe>

<p><a href="https://www.beta-labs.in" target="myFrame">Open Beta-Labs.in</a></p> 


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). 
iframe Important attribute
  • src: The src attribute is used to give the pathname or file name which content to be loaded into iframe.
  • name: It gives the name to the iframe. The name attribute is important if you want to create a link in one frame.
  • height: It defines the height of the embedded iframe, and the default height is 150 px.
  • width: It defines the width of the embedded frame, and the default width is 300 px.
  • frameborder: It defines whether iframe should have a border or not. (Not supported in HTML5).
  • allowfullscreen: If true then that frame can be opened in full screen.
  • 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-popups:  It will enable popups, and if not applied then no popup will open.
    • allow-scripts:   It will enable the script to run.
    • allow-same-origin:  If this keyword is used then the embedded resource will be treated as downloaded from the same source.
  • srcdoc: The 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) 


HTML5 - Audio & Video

HTML5 features include native audio and video support without the need for Flash. The HTML5 <audio> and <video> tags make it simple to add media to a website. You need to set src attribute to identify the media source and include a controls attribute so the user can play and pause the media.

Embedding Video

HTML5 Video tag or <video> is used to add videos on a webpage. Video tag supports mp4oggmov and H.264 files. To embed a video, create a video tag. src is compulsory attribute for video tag. controls attribute can add play/pause button, video timeline, mute button, volume controller, full screen, subtitles options on player. Here is the simplest form of embedding a video file in your webpage –

Example

 <video src = "intro.mp4"  width = "425" height = "240" controls>

        Your browser does not support the <video> element.   
 </video>

You can use <source> tag to specify media along with media type and many other attributes. A video element allows multiple source elements and browser will use the first recognized format.

Video Attribute Specification

The HTML5 video tag can have a number of attributes to control the look and feel and various functionalities of the control −

  • src: The URL of the video to embed. This is optional; you may instead use the <source> element within the video block to specify the video to embed.
  • width: This attribute specifies the width of the video's display area, in CSS pixels.
  • height: This attribute specifies the height of the video's display area, in CSS pixels.
  • controls: If this attribute is present, it will allow the user to control video playback, including volume, seeking, and pause/resume playback.
  • autoplay: This Boolean attribute if specified, the video will automatically begin to playback as soon as it can do so without stopping to finish loading the data.
  • loop: This Boolean attribute if specified, will allow video automatically to seek back to the start after reaching at the end.
  • autobuffer: This Boolean attribute if specified, the video will automatically begin buffering even if it's not set to automatically play.
  • preload: This attribute specifies that the video will be loaded at page load, and ready to run. Ignored if autoplay is present.
  • poster: This is a URL of an image to show until the user plays or seeks.


Embedding Audio

HTML5 Audio tag is used to play audio files, like mp3ogg and AAC. All browsers supporting audio tag are having built-in player. 

The easiest way to include Audio Tag in a webpage is to use audio tagsrc is compulsory attribute in audio tag. controls attribute can show control bar to user. User can play/pause, change timeline, mute, and increase the volume of audio playing using controls.

HTML5 Audio Example

<audio src = "sound.wav" controls autoplay>
        Your browser does not support the <audio> element.   
</audio>

You can use <source>; tag to specify media along with media type and many other attributes. An audio element allows multiple source elements and the browser will use the first recognized format. Audio Attribute Specification almost the same as the video. The HTML5 audio tag can have a number of attributes to control the look and feel and various functionalities of the control.