html - 宽度为 100% 的 SVG 溢出其容器

标签 html css svg box-sizing

在下面的代码片段中,添加 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 显示的可滚动内容稍微多一些

最佳答案

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>

关于html - 宽度为 100% 的 SVG 溢出其容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348413/

相关文章:

javascript - 点击事件不会在点击时触发?

html - 使用 rem 的 Chrome 和 Firefox 之间的字体大小差异

html - 无法突出显示导航菜单

html - 调整 svg 文件中一组路径的大小

html - 如何使访问过的链接不被访问?

html - 难以为响应定制 Bootstrap 导航栏

javascript - 使用 jQuery 内联 SVG 中的目标 id

html - 使用 css content 属性渲染内容

javascript - 如何将一个div分成四部分?

html - 自定义字体未显示在电子邮件中