make iframe height dynamic based on content inside- JQUERY/Javascript

前端 未结 20 2221
情书的邮戳
情书的邮戳 2020-11-22 07:42

I am loading an aspx web page in an iframe. The content in the Iframe can be of more height than the iframe\'s height. The iframe should not have scroll bars.

I have

20条回答
  •  时光取名叫无心
    2020-11-22 08:15

    The simple solution is to measure the width and height of the content area, and then use those measurements to calculate the bottom padding percentage.

    In this case, the measurements are 1680 x 720 px, so the padding on the bottom is 720 / 1680 = 0.43 * 100, which comes out to 43%.

    .canvas-container {    
        position: relative;
        padding-bottom: 43%; // (720 ÷ 1680 = 0.4286 = 43%)
        height: 0;
        overflow: hidden;   
    }
    
    .canvas-container iframe {    
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;   
    }
    

提交回复
热议问题