Using iframe and css

How to Create a Responsive Iframe with CSS

In this snippet, you can learn how to create a responsive iframe with CSS. First, let’s see how a simple iframe looks like and then, we’ll show how to make it responsive.

Example of creating a simple tag:

html> html> head> title>Title of the document title> head> body> iframe src="https://www.w3docs.com"> iframe> body> html>

It is by default 300×150 pixels in size. That’s why very often we use the width and height attributes to set different size of an iframe.

Next, we’ll use CSS to make the iframe responsive. Let’s try our example on Youtube. Here are the steps to follow.

Create HTML

  • Go on Youtube, click on the «share» button under the video and then «embed» it to copy the link in your HTML code.
iframe width="560" height="315" src="https://www.youtube.com/embed/UF8uR6Z6KLc" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen> iframe>
  • Remove the sizes defined automatically as we’re going to change them.
  • Place your iframe in a element, which is important for sizing the iframe. Add a class attribute to it.
  • Use the class , src , gesture , and allow attributes with the element.
div iframe >iframe" src="https://www.youtube.com/embed/UF8uR6Z6KLc" gesture="media" allow="encrypted-media" allowfullscreen> 

Add CSS

  • Style the «wrap-element» class by setting the position property to «relative», the overflow to «hidden» and specifying the padding-top property. In our example, we want to keep the ratio of 56.26% (height 9 ÷ width 16) as this is the default ratio for YouTube videos. However, you are free to use other ratios as well.
  • Style the «wrapped-iframe» class by specifying its position as «absolute» and setting the top and left properties to 0 so as to position the iframe at the center of the container. Set also the width and height properties to «100%» and add a border.
.wrap-element < position: relative; overflow: hidden; padding-top: 56.25%; > .wrapped-iframe < position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0; >

Let’s see the complete code example and enjoy the result!

Читайте также:  Php hashing with salt

Example of creating a responsive iframe:

html> html> head> title>Title of the document title> style> .wrap-element < position: relative; overflow: hidden; padding-top: 56.25%; > .wrapped-iframe < position: absolute; top: 0; left: 0; width: 100%; height: 100%; border: 0; > style> head> body> div class="wrap-element"> iframe class="wrapped-iframe" src="https://www.youtube.com/embed/UF8uR6Z6KLc" gesture="media" allow="encrypted-media" allowfullscreen> iframe> div> body> html>

Use CSS Frameworks

Some CSS frameworks have predefined classes that will do exactly the same as described above, but in each case, you need to create a wrapping element to give it a certain class.

Responsive Iframe with Bootstrap

In Bootstrap 3.2 and over, you should use the predefined class «embed-responsive» and an aspect ratio class modifier like «embed-responsive-16by9», which will add padding-top with different percentages depending on the given modifier class. Then, give your iframe the «embed-responsive-item» class.

div class="embed-responsive embed-responsive-16by9"> iframe class="embed-responsive-item" src="https://www.youtube.com/embed/UF8uR6Z6KLc" allowfullscreen> iframe> div>

But you can also create your own modifier class, like this:

.embed-responsive-10by3 < padding-top: 30%; >

Example of creating a responsive iframe with the CSS Bootstrap framework:

html> html> head> link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"> title>Title of the document title> head> body> div class="embed-responsive embed-responsive-16by9"> iframe class="embed-responsive-item" src="https://www.youtube.com/embed/UF8uR6Z6KLc" allowfullscreen> iframe> div> body> html>

Responsive Iframe with Materialize

With the Materialize CSS framework, all you need to do is adding the «video-container» class to the wrapper:

div >video-container"> iframe src="//www.youtube.com/embed/Q8TXgCzxEnw?rel=0" frameborder="0" allowfullscreen>iframe> div>

Example of creating a responsive iframe with the CSS Materialize framework:

html> html> head> link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> title>Title of the document title> head> body> div class="video-container"> iframe src="//www.youtube.com/embed/Q8TXgCzxEnw?rel=0" frameborder="0" allowfullscreen> iframe> div> body> html>

Источник

How to apply css to iframe in Html?

Iframes are often used to embed other web pages into a current page, but it can be a challenge to apply CSS styles to the content within an iframe. There are several methods to apply CSS to an iframe, but each has its own limitations and considerations. Here are some ways to apply CSS to iframes:

Method 1: Inline Style Attribute

To apply CSS to an iframe using the inline style attribute, you can follow these steps:

iframe id="my-iframe" src="https://example.com">iframe>
iframe id="my-iframe" src="https://example.com" style="">iframe>
iframe id="my-iframe" src="https://example.com" style="border: 1px solid red; width: 100%; height: 500px;">iframe>

Here’s an example of applying a border and setting the width and height of the iframe using the inline style attribute:

iframe id="my-iframe" src="https://example.com" style="border: 1px solid red; width: 100%; height: 500px;">iframe>

You can also apply CSS to the content inside the iframe by targeting its body element using the contentDocument property.

iframe id="my-iframe" src="https://example.com">iframe>
var iframe = document.getElementById('my-iframe'); var iframeBody = iframe.contentDocument.body; iframeBody.style.backgroundColor = 'blue';

Here’s an example of changing the background color of the content inside the iframe to blue:

iframe id="my-iframe" src="https://example.com">iframe>
var iframe = document.getElementById('my-iframe'); var iframeBody = iframe.contentDocument.body; iframeBody.style.backgroundColor = 'blue';

That’s it! These are some examples of how to apply CSS to an iframe using the inline style attribute.

Method 2: Stylesheet Linked to the Parent Page

To apply CSS to an iframe using a stylesheet linked to the parent page, follow these steps:

  1. Create a CSS file with the styles you want to apply to the iframe. Let’s call it «iframe-styles.css».
  2. Link the CSS file to the parent page using the tag in the section of the HTML document. For example:
head> link rel="stylesheet" type="text/css" href="iframe-styles.css"> head>
iframe class="iframe-styling" src="https://www.example.com">iframe>
  1. In the «iframe-styles.css» file, add a CSS rule that targets the class attribute of the tag. For example:
.iframe-styling  border: 2px solid red; width: 100%; height: 500px; >

This will apply a red border, set the width to 100%, and the height to 500 pixels to the iframe with the class attribute of «iframe-styling».

Here is the complete code example:

head> link rel="stylesheet" type="text/css" href="iframe-styles.css"> head> body> iframe class="iframe-styling" src="https://www.example.com">iframe> body>
.iframe-styling  border: 2px solid red; width: 100%; height: 500px; >

By following these steps, you can easily apply CSS styles to an iframe using a stylesheet linked to the parent page.

Method 3: Stylesheet Linked to the Iframe Page

To apply CSS to an iframe using a stylesheet linked to the iframe page, follow these steps:

  1. Create a CSS file with the desired styles. For example, create a file named «iframe-style.css» with the following styles:
body  background-color: #eee; >
  1. In the iframe page, link to the CSS file using the tag. For example, add the following code to the section of the iframe page:
head> link rel="stylesheet" type="text/css" href="iframe-style.css"> head>
  1. In the parent page, add the allowfullscreen and seamless attributes to the tag. For example, add the following code to the parent page:
iframe src="iframe-page.html" allowfullscreen seamless>iframe>

That’s it! The CSS styles defined in the «iframe-style.css» file will be applied to the content of the iframe.

Here is the complete code for the iframe page:

DOCTYPE html> html> head> link rel="stylesheet" type="text/css" href="iframe-style.css"> head> body> h1>Hello World!h1> p>This is an example of an iframe.p> body> html>

And here is the complete code for the parent page:

DOCTYPE html> html> head> title>Parent Pagetitle> head> body> h1>Welcome to the Parent Pageh1> iframe src="iframe-page.html" allowfullscreen seamless>iframe> body> html>

Method 4: CSS in the Parent Page Targeting the Iframe

To apply CSS to an iframe using CSS in the parent page, you can use the contentDocument property of the iframe to target its contents. Here are the steps to do it:

iframe id="my-iframe" src="http://example.com">iframe>
#my-iframe  width: 100%; height: 300px; border: none; >
  1. To target elements inside the iframe, you can use the contentDocument property to get the iframe’s document object. Here’s an example that changes the background color of a div inside the iframe:
#my-iframe  width: 100%; height: 300px; border: none; > #my-iframe .my-div  background-color: red; >
var iframe = document.getElementById('my-iframe'); var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var myDiv = iframeDoc.getElementById('my-div'); myDiv.style.backgroundColor = 'red';
  1. If you want to apply styles to the entire iframe document, you can use the contentDocument property to add a tag to the iframe’s head:
#my-iframe  width: 100%; height: 300px; border: none; > #my-iframe body  background-color: red; >
var iframe = document.getElementById('my-iframe'); var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var style = iframeDoc.createElement('style'); style.innerHTML = 'body < background-color: red; >'; iframeDoc.head.appendChild(style);

That’s it! With these steps, you can easily apply CSS styles to an iframe using CSS in the parent page.

Method 5: CSS in the Iframe Page Targeting the Parent Page

To apply CSS to an iframe from the parent page, you can use the contentDocument property to access the iframe’s document object and then manipulate its CSS styles using JavaScript. Here is an example code snippet:

DOCTYPE html> html> head> title>Parent Pagetitle> style> iframe  width: 100%; height: 300px; > style> head> body> iframe id="myIframe" src="iframe.html">iframe> script> var iframe = document.getElementById('myIframe'); var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; var style = document.createElement('style'); style.textContent = 'body '; iframeDoc.head.appendChild(style); script> body> html>

In this example, we have an iframe element with an ID of myIframe that loads the iframe.html file. In the parent page, we have defined some CSS styles for the iframe element itself.

To apply additional CSS styles to the iframe’s document, we first get a reference to the iframe element using getElementById . We then use the contentDocument property to get the iframe’s document object. If the iframe is from a different domain, we need to use contentWindow.document instead.

Next, we create a new style element using createElement and set its textContent property to the CSS styles we want to apply to the iframe’s document. In this case, we are setting the background color of the body element to #f0f0f0 .

Finally, we append the new style element to the head element of the iframe’s document using appendChild .

With this approach, you can apply any CSS styles you want to the iframe’s document from the parent page. Just make sure to wait for the iframe to load before trying to access its document object.

Источник

How TO — Responsive Iframe

Create an iframe that will keep the aspect ratio (4:3, 16:9, etc.) when resized:

What is aspect ratio?

The aspect ratio of an element describes the proportional relationship between its width and its height. Two common video aspect ratios are 4:3 (the universal video format of the 20th century), and 16:9 (universal for HD television and European digital television, and for YouTube videos).

How To — Responsive Iframes

Step 1) Add HTML:

Use a container element, like , and add the iframe inside of it:

Example

Step 2) Add CSS:

Add a percentage value for padding-top to maintain the aspect ratio of the container DIV. The following example will create an aspect ratio of 16:9, which is the default aspect ratio of YouTube videos.

Example 16:9 Aspect Ratio

.container <
position: relative;
overflow: hidden;
width: 100%;
padding-top: 56.25%; /* 16:9 Aspect Ratio (divide 9 by 16 = 0.5625) */
>

/* Then style the iframe to fit in the container div with full height and width */
.responsive-iframe position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
>

Источник

Оцените статью