Embedding YouTube videos on your website is standard practice, but you don't always have to rely on the traditional <iframe> method. Using the <object> element in HTML allows you to integrate various media seamlessly onto your webpage. This step-by-step guide shows you how to embed YouTube videos in HTML without using an iframe.
A potential pitfall is using incorrect video URLs or forgetting required parameters, which can prevent the video from loading or playing correctly. Follow along to set up the <object> tag and YouTube API integration step by step.
What You Need Before Embedding YouTube Videos Without Iframe
Ensure you have these ready:
- Basic understanding of HTML tags and attributes
- The URL or video ID of the YouTube video you want to embed
- A Google account (if you plan to use the YouTube Data API method)
- Any code editor or browser to test your HTML locally
Method 1: Embed YouTube Video Using the <object> Element
Using the <object> element is a straightforward way to load a YouTube video without an iframe. Here are the steps to set it up:
Step 1: Get the Video URL.
At first, you will need to locate the YouTube video that you wish to embed. Click on the Share button below the video, then click on Embed. You’ll see an iframe code snippet. Within this snippet, find the src attribute. The value of this attribute is the URL you need.
Step 2: Use the <object> Element.
Next, you’ll use the <object> element in your HTML code. The <object> tag requires two attributes: data and type. The data attribute should be set to the URL you obtained in Step 1, and the type attribute should be set to "text/html".
Here’s an example of the <object> element:
<object
data="https://www.youtube.com/embed/dQw4w9WgXcQ"
type="text/html"
width="640"
height="360">
</object>
Change the URL in the data attribute in the code above to the URL of the YouTube video you want to watch. The properties for width and height can be changed to fit the size of your page.
Step 3: Add the <param> Element (Optional)
You can add more options to the <object> element using the <param> element. For example, you can use it to turn on fullscreen mode:
<object
data="https://www.youtube.com/embed/dQw4w9WgXcQ"
type="text/html"
width="640"
height="360">
<param name="allowfullscreen" value="true">
</object>
In this example, the <param> element with the name attribute set to "allowfullscreen" and the value attribute set to "true" enables fullscreen mode for the embedded video.
Try this code yourself — free
Run it in the EduSeekho online compiler. No setup needed.
Method 2: Embed YouTube Video by Using the YouTube Data API
Embedding a YouTube video directly into your HTML code without using an iframe can also be achieved using the YouTube Data API. This method involves fetching the video data from YouTube and then using a video player like HTML5’s <video> tag to play it.
Step 1: Get the Video ID.
The first step is to get the YouTube video ID. This is the unique identifier that YouTube uses for each video. It’s usually found in the video’s URL after watch?v=.
For example, in the URL https://www.youtube.com/watch?v=dQw4w9WgXcQ, the video ID is dQw4w9WgXcQ.
Step 2: Fetch Video Data from YouTube.
Next, you’ll need to fetch the video data from YouTube. This can be done using the YouTube Data API. You’ll need to sign up for a free API key and then use that key to make a GET request to the following URL:
https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID&key=YOUR_API_KEY&part=snippet,contentDetails,statistics,status
Replace VIDEO_ID with your video’s ID and YOUR_API_KEY with your API key. The response will include a direct link to the video file.
How do I get a YouTube API key?
To get a YouTube API key, follow these steps:
- Log in to the Google Cloud Console with your Google account.
- Create a new project and select the YouTube Data API v3 as one of the services that your application is registered to use.
- Access the API credentials section and generate an API key. The API key will be displayed on your screen. Put a copy aside in a secure location.
Step 3: Embed Video Using HTML5 Video Tag.
Finally, the HTML5 <video> tag allows you to directly include the video into your HTML code. In this case, we have:
<video width="320" height="240" controls>
<source src="VIDEO_URL" type="video/mp4">
Your browser does not support the video tag.
</video>
Replace VIDEO_URL with the direct link to the video file you got from the YouTube Data API.
That’s all there is to it! You were able to embed a YouTube video in HTML without an iframe. Please keep in mind that this way takes a little more work than using an iframe alone, but it gives you more control over how the video plays and looks.
flowchart TD A([Start]) -- Choose Method --> B[Object Element Method] A -- Choose Method --> C[API Method] B --> D[Get Video URL] D --> E[Create object tag with data=URL] E --> F[Add parameters like allowfullscreen] C --> G[Get Video ID] G --> H[Fetch Video metadata via Data API] H --> I[Embed streaming URL in video source] F --> J([End]) I --> J
Quick Quiz — test your understanding
Tap an option to check your answerWhich attribute of the <object> element is used to specify the video source URL?
data attribute specifies the resource URL for the <object> element.<object> differs from <iframe>.Common mistake: Always verify that you have the right permissions to embed and stream a video. Attempting to download or modify copyrighted content via API requests violates YouTube's terms of service.
Key takeaway: While using <object> gives you alternative control, it is more complex than a standard iframe. Choose the method that best fits your design and security requirements.
FAQs About Embedding YouTube Videos in HTML
Here are some common questions web developers have:
<object> element provides more flexibility for fallback content and parameters, and can help bypass iframe-specific rendering limitations in older systems.<object> element. The API key is only necessary if you are calling the YouTube Data API to fetch raw video details.<video> tag requires a direct video file link (like a .mp4 source). Standard YouTube links must be processed or served via YouTube's own player logic.Conclusion
Embedding YouTube videos using alternative HTML methods like <object> or custom players powered by the YouTube Data API can expand your web design control. For simple projects, the standard iframe remains the easiest approach, but these alternatives are useful tools to keep in your front-end arsenal.
If you have any questions or run into issues while setting up your embeds, feel free to ask on EduSeekho.