代码之家  ›  专栏  ›  技术社区  ›  Iter Ator

为什么垂直滚动条会出现在iframe的父级中?

  •  0
  • Iter Ator  · 技术社区  · 6 年前

    我想在元素中创建一个iframe。iframe应该填充整个元素。

    HTML代码:

    <div class="parent">
      <iframe class="child" src="https://example.com"></iframe>
    </div>
    

    样式定义:

    .parent {
      background: red;
      width: 200px;
      height: 200px;
      overflow:auto;
    }
    
    .child {
      width:100%;
      height:100%;
      border: 0;
    
      margin: 0;
      padding: 0;
    }
    

    不幸的是,出现了一个不必要的垂直滚动条:

    enter image description here

    jsfiddle公司: https://jsfiddle.net/4hqjt9w3/1/

    如何可以去掉父元素上的滚动条 overflow: hidden ,还是绝对定位?

    2 回复  |  直到 6 年前
        1
  •  1
  •   Temani Afif    6 年前

    使iframe成为块元素。默认情况下,其计算的显示值为inline so you will face a whitespace issue at the bottom 这将导致溢出:

    enter image description here

    .parent {
      background: red;
      width: 200px;
      height: 200px;
      overflow:auto;
    }
    
    .child {
      display:block;
      width:100%;
      height:100%;
      border: 0;
      
      margin: 0;
      padding: 0;
    }
    <div class="parent">
      <iframe class="child" src="https://example.com"></iframe>
    </div>
        2
  •  0
  •   Nick    6 年前

    你是说滚动属性吗?用下面的代码替换您的代码。

    <div class="parent">
      <iframe class="child" src="https://example.com" scrolling=no></iframe>
    </div>