【问题标题】:SVG with width 100% overflows its container宽度为 100% 的 SVG 溢出其容器
【发布时间】:2017-07-27 10:39:30
【问题描述】:

在下面的 sn-p 中,添加 svg 元素会导致出现垂直滚动条。删除 svg 会删除滚动条。我想了解它为什么会发生以及是否有一个不可怕的解决方案(例如 width:99%; height:98% 解决了它,但那是一个令人作呕的解决方案)。

我无法真正删除上面的 DIV 样式,因为其他 html 结构也位于这些需要它们存在的容器中。

.menuquery {
  border: 1px solid #ccc;
  overflow: auto;
  box-sizing: border-box;
}

.xainnersubformdefault {
  /* allows the svg to autosize */
  width: 100%;
  height: 100%;
}

.xadatabox {
  height: 100%;
  /* essential for jtable to scroll and not leak */
}

.datachart {
  width: 100%;
  height: 100%;
  /* position:relative; */
  /* to make an svg fill its container, this is required, see stackoverflow 9566792 */
}

svg {
  width: 100%;
  height: 100%;
  border: 1px solid green;
  box-sizing: border-box;
}
<div class="menuquery" style="width:300px;height:200px">
  <div class="xa xafield xadatabox">
    <div class="xainnersubformdefault">
      <div class="datachart">
        <svg></svg>
      </div>
    </div>
  </div>
</div>

svg 上的绿色边框和框大小仅存在,因此我们可以看到 svg 的边缘,最终不需要它

如果我将 svg 更改为 div,并将 svg css 应用于该 div,则不会出现滚动条,因此 svg 元素似乎有些不同。

我已经在 Firefox 和 IE 中对此进行了测试。两者都显示滚动条,但 IE 显示的可滚动内容稍微多一些

【问题讨论】:

    标签: html css svg box-sizing


    【解决方案1】:

    Svginline 元素,将font-size: 0; 设置为.menuquery 将解决此问题

    .menuquery {
        border: 1px solid #ccc;
        overflow: auto;
        box-sizing: border-box;
        font-size: 0;
    }
    .xainnersubformdefault {
      /* allows the svg to autosize */
      width: 100%;
      height: 100%;
    }
    
    .xadatabox {
      height: 100%;
      /* essential for jtable to scroll and not leak */
    }
    
    .datachart {
      width: 100%;
      height: 100%;
      /* position:relative; */
      /* to make an svg fill its container, this is required, see stackoverflow 9566792 */
    }
    
    svg {
      width: 100%;
      height: 100%;
      border: 1px solid green;
      box-sizing: border-box;
    }
    <div class="menuquery" style="width:300px;height:200px">
      <div class="xa xafield xadatabox">
        <div class="xainnersubformdefault">
          <div class="datachart">
            <svg></svg>
          </div>
        </div>
      </div>
    </div>

    或者您可以将display:block 设置为svg。更新于your comment

    .menuquery {
        border: 1px solid #ccc;
        overflow: auto;
        box-sizing: border-box;
    }
    .xainnersubformdefault {
      /* allows the svg to autosize */
      width: 100%;
      height: 100%;
    }
    
    .xadatabox {
      height: 100%;
      /* essential for jtable to scroll and not leak */
    }
    
    .datachart {
      width: 100%;
      height: 100%;
      /* position:relative; */
      /* to make an svg fill its container, this is required, see stackoverflow 9566792 */
    }
    
    svg {
      width: 100%;
      height: 100%;
      border: 1px solid green;
      box-sizing: border-box;
      display:block;
    }
    <div class="menuquery" style="width:300px;height:200px">
      <div class="xa xafield xadatabox">
        <div class="xainnersubformdefault">
          <div class="datachart">
            <svg></svg>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 啊,所以我只是在 svg 元素上尝试 display:block,因为这似乎比将字体大小设置得更高
    • 当然,您可以将svg 转换为block 元素,但如果元素是inline,那么font-size 是最好的选择。
    • 谢谢,svg 不需要内联,因为它永远不会有相邻元素,所以在这种情况下,我认为两者都是很好的解决方案。我可以确认 display:block 也有效。
    • 您的解决方案运行良好,我还是不明白为什么内联 svg 会导致出现垂直滚动条?
    • @OlivierBoissé 内联元素在 HTML 中被视为文本,因此它们具有 tracking and kerning,使空格和文本看起来可读。
    【解决方案2】:

    .menuquery 中删除overflow: auto;

    .menuquery {
      border: 1px solid #ccc;
      box-sizing: border-box;
    }
    
    .xainnersubformdefault {
      /* allows the svg to autosize */
      width: 100%;
      height: 100%;
    }
    
    .xadatabox {
      height: 100%;
      /* essential for jtable to scroll and not leak */
    }
    
    .datachart {
      width: 100%;
      height: 100%;
      /* position:relative; */
      /* to make an svg fill its container, this is required, see stackoverflow 9566792 */
    }
    
    svg {
      width: 100%;
      height: 100%;
      border: 1px solid green;
      box-sizing: border-box;
    }
    <div class="menuquery" style="width:300px;height:200px">
      <div class="xa xafield xadatabox">
        <div class="xainnersubformdefault">
          <div class="datachart">
            <svg></svg>
          </div>
        </div>
      </div>
    </div>

    【讨论】:

    • 谢谢,这似乎可行,但我会在标记为答案之前将它推回主代码,因为我只是添加它来调试 svg 比它的容器更宽的问题(所以我很惊讶这个问题现在已经消失了!!)。知道为什么会这样吗?谢谢
    • 我相信 Abhishek Pandey 的回答解释了这一点
    猜你喜欢
    • 2020-12-27
    • 2011-09-29
    • 2020-10-22
    • 1970-01-01
    • 2012-02-04
    • 2011-06-22
    • 2019-07-17
    • 2020-04-11
    • 2022-01-18
    相关资源
    最近更新 更多