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

Steps to install MySQL 5.7 in binary mode and optimize the system under Linux

This article mainly introduces the installation/st...

Detailed installation and configuration of hadoop2.7.2 under ubuntu15.10

There are many Hadoop installation tutorials on L...

Example of using #include file in html

There are two files a.htm and b.htm. In the same d...

JavaScript Basics Operators

Table of contents 1. Operators Summarize 1. Opera...

Detailed tutorial on installing PHP and Nginx on Centos7

As the application of centos on the server side b...

SQL injection vulnerability process example and solution

Code example: public class JDBCDemo3 { public sta...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

Understanding what Node.js is is so easy

Table of contents Official introduction to Node.j...

Docker uses dockerfile to start node.js application

Writing a Dockerfile Taking the directory automat...

MySQL 8.0.25 installation and configuration method graphic tutorial

The latest download and installation tutorial of ...

Introduction to Royal Blue Color Matching for Web Design

Classical color combinations convey power and auth...

vue+springboot realizes login verification code

This article example shares the specific code of ...

Steps to export the fields and related attributes of MySQL tables

Need to export the fields and properties of the t...