1
2
Source link 3
To create an iframe that embeds a video based on a URL, you'll need to follow these steps:
Extract the Video URL: Identify and extract the video URL from your content. For example, if your content had a URL like https://www.youtube.com/watch?v=dQw4w9WgXcQ
, that would be your video URL.
Construct the Iframe: Use your extracted URL to embed the video in an iframe with the specified format.
Here's an example of the complete HTML code for embedding a YouTube video responsively:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Video Embed</title>
<style>
.video-container {
position: relative;
padding-bottom: 56.25%; /* 16:9 Aspect Ratio */
height: 0;
overflow: hidden;
max-width: 100%;
}
.video-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
</style>
</head>
<body>
<div class="video-container">
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" scrolling="no" allowfullscreen></iframe>
</div>
</body>
</html>
"https://www.youtube.com/embed/dQw4w9WgXcQ"
in the iframe's src
attribute with the actual video URL you extracted.56.25%
maintains a 16:9 aspect ratio, which is common for videos.width: 100%
and height: 100%
allows the iframe to adapt to various screen sizes.You can use this code as a template to embed any video URL. Just ensure to correctly format the URL for embedding, especially for platforms like YouTube, where the URL should include /embed/
instead of /watch?v=
.