HTML | Adding Youtube videos

In the early days, adding a video to a webpage was a real challenge since one had to convert the videos to different formats to make them play in all browsers. Converting videos to different formats can be difficult and time-consuming. Now, adding a video to a webpage has become as easy as copying and pasting and a very apt solution to add videos to a website is using Youtube. Youtube helps to host a video for a user so that they can be further embedded on webpages.

YouTube displays an id like “BGAk3_2zi8k”, whenever a video is saved or played. This id is further used as a referral for the youtube video to be embedded in the webpage.

Steps to add a Youtube video on a Webpage :

  1. Upload the video that you want to embed on your webpage on YouTube.
  2. Copy the video id of the video.
  3. Use iframe, object or ’embed’ element in your web page for video definition.
  4. Use the src attribute to point to the URL of the video.
  5. Dimensions of the player can be adjusted using the width and height attributes.

Steps to get the Video Id of a youtube video :

  1. Open the youtube video whose Id you want.
  2. Right click on the video, from the menu select “Stats for nerds”.

  3. The first value in the box is the Video ID.

The video id of this video is : il_t1WVLNxk



Adding Youtube video :

1) Using iFrame tag:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<iframe height="480" width="500"  
</iframe>
  
</body>
</html>

chevron_right


Output :


Enabling YouTube autoplay feature:
Youtube’s autoplay feature can be used to automatically play a video when a user visits that page.

There are two types of parameters that can be used :

  1. Value 1 : The video starts playing automatically when the player loads.
  2. Value 0 (default case) : The video does not play automatically when the player loads.
filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<iframe height="480" width="500"
</iframe>
  
</body>
</html>

chevron_right


Output :


Creating a YouTube playlist :

A playlist of youtube videos can be created using comma character which separates the list of videos to play.

The loop parameter is used to loop the number of playbacks on the videos :

  1. Value 1 : The video will keep on looping again and again.
  2. Value 0 (default case) : The video plays only once.
filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<iframe height="480" width="500"   
           AS_dAPN1Dlk?playlist=AfxHGNRtFac&loop=1">
</iframe>
  
</body>
</html>

chevron_right


Output :


Enabling / Disabling Youtube controls :

The Youtube Player offers controls like play, pause, volume etc that can be disabled or enabled using the controls parameter.
There are two parameters available that can be used :

  1. Value 1 (default case) : Player controls are displayed.
  2. Value 0 : Player controls are not displayed.

For Enabling Controls :



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<iframe  width="440" height="372" 
</iframe>
  
</body>
</html>

chevron_right


Output :


For Disabling Controls :

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<iframe  width="440" height="372" 
</iframe>
  
</body>
</html>

chevron_right


Output :


2) Using object tag:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<object width="480" height="500"
</object>
  
</body>
</html>

chevron_right


Output :


3) Using embed tag:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<body>
  
<embed width="480" height="500"
  
</body>
</html>

chevron_right


Output :


Note: Nowadays, the object and the embed tag are not appreciated, therefore it is recommended to use the iframe tag.



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : prakhar7



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.