Method of iframe adaptation in web responsive layout

Method of iframe adaptation in web responsive layout

Problem <br />In responsive layout, we should be careful with iframe elements. The width and height attributes of the iframe element set its width and height, but when the width or height of the containing block is smaller than the width or height of the iframe, the iframe element will overflow:

Such overflowing iframe will destroy the layout of the page. We can find a way to make the iframe element responsive as well, so stay tuned.

Workaround
The iframe element itself cannot be stretched unless its width is explicitly set via js. However, we can wrap the iframe with an iframe-container element, make the width of the iframe-container element fill the width of the containing block, and set the padding-bottom percentage of the iframe-container element according to the aspect ratio of the iframe.

In fact, the essence of this method lies in setting the padding-bottom attribute of the iframe-container element. The purpose of setting this attribute is to set the height of the element in disguise. Because setting a percentage for padding-bottom is relative to the width of the parent element. If you set a percentage for the height attribute, it is relative to the height of the parent element. We usually use the default auto for the height value of the parent element, so the height of the child element will also be 0. Therefore, we can only set attributes for padding-bottom. This way, just let the iframe element fill the iframe-container.

Copy code
The code is as follows:

.wrap{
width: 400px;
margin: auto;
border: 5px solid greenyellow;
}
.iframe-container{
height: 0;
padding-bottom: 97.6%;
position: relative;
}
.iframe-container iframe{
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
}
@media screen and (max-width: 400px) {
.wrap{
width: 300px;
}
}


Copy code
The code is as follows:

<div class="wrap">
<div class="iframe-container">
<iframe height=498 width=510 src="<a href="http://player.youku.com/embed/XOTE0MjkyODgw">http://player.youku.com/embed/XOTE0MjkyODgw</a>" frameborder=0 allowfullscreen></iframe>
</div>
</div>

The result shows the status:

When the viewport width is greater than 400px:

When the viewport width is less than 400px:

Summary <br />The above is the entire content of this article. I hope that the content of this article can bring certain help to your study or work. If you have any questions, you can leave a message to communicate.

<<:  Example of converting JS one-dimensional array into three-dimensional array

>>:  How to quickly return to the top from the bottom when there is too much content on the web page

Recommend

Recommend some useful learning materials for newbies in web design

Many people also asked me what books I read when ...

Example of how to configure nginx in centos server

Download the secure terminal MobaXterm_Personal F...

HTML left and right layout example code

CSS: Copy code The code is as follows: html,body{ ...

Detailed steps for using AES.js in Vue

Use of AES encryption Data transmission encryptio...

Graphical introduction to the difference between := and = in MySQL

The difference between := and = = Only when setti...

How to handle forgotten passwords in Windows Server 2008 R2

What to do if you forget Windows Server 2008R2 So...

How to implement data persistence using the vuex third-party package

Purpose: Allow the state data managed in vuex to ...

17 404 Pages You'll Want to Experience

How can we say that we should avoid 404? The reas...

Steps to package and deploy the Vue project to the Apache server

In the development environment, the vue project i...

How to restore data using binlog in mysql5.7

Step 1: Ensure that MySQL has binlog enabled show...

Compile CPP files using G++ in Ubuntu

When I used g++ to compile the cpp file for the f...

JS gets the position of the nth occurrence of a specified string in a string

Learn about similar methods for getting character...

JavaScript built-in date and time formatting time example code

1. Basic knowledge (methods of date objects) 😜 ge...