简体   繁体   English

我无法将 SVG 包裹在 div 中以正确缩放

[英]I can't get an SVG wrapped in a div to scale properly

I have the following HTML hierarchy:我有以下 HTML 层次结构:

div: flex 1 1 auto height=56px (modified by JS)
  div: grid 1fr
    svg with viewBox, width=100% height=100%

This correctly scales the svg to fit within the grid div as the top-level div's height gets changed by JS.当顶级 div 的高度被 JS 更改时,这会正确缩放 svg 以适应网格 div。 But when there's one more level of div in there, the svg disregards the parent scale and overflows the parents:但是当那里有更多一层 div 时,svg 会忽略父级比例并溢出父级:

div: flex 1 1 auto height=56px (modified by JS)
  div: grid 1fr
    wrapper div ***PROBLEM***
      svg with viewBox, width=100% height=100%

I have a snippet below showing the behavior of the two cases.我在下面有一个片段显示了这两种情况的行为。 Unfortunately in my real app, I sometimes need that wrapper (it might be an <a> instead of a <div> , but that also shows the same problem).不幸的是,在我的真实应用程序中,我有时需要那个包装器(它可能是<a>而不是<div> ,但这也显示了同样的问题)。 Can I somehow make that element not affect the SVG scaling algorithm?我能否以某种方式使该元素不影响 SVG 缩放算法? I've reviewed resize svg wrapped in div that is wrapped in div but none of the solutions there help.我已经审查了resize svg wrapped in div which is wrapped in div但那里的解决方案都没有帮助。 I'm looking for a pure HTML/CSS solution, since in my real code the JS resizing is happening in a framework.我正在寻找纯 HTML/CSS 解决方案,因为在我的真实代码中,JS 大小调整发生在框架中。 I'm using recent Chrome btw.我正在使用最近的 Chrome 浏览器。

Here's a snippet of both cases:这是两种情况的片段:

 function resizeElt(id, h) { const elt = document.getElementById(id) elt.style.height = h } let large = false function resize(id) { large=,large resizeElt(id? large: '100px' : '50px') }
 .top { width: 30vw; height: 50px; border: 1px solid blue; display: flex; flex: 1 1 auto; }.svg { margin: auto; /* center */ display: block; border: 1px dashed red; }.grid { display: grid; grid-template-columns: 1fr; column-gap: 10px; align-items: center; width: 100%; }.wrapper { }
 <h3> Show fit to width and height: SVG stays within bounds </h3> <button onclick="resize('top1')"> Resize container taller/shorter </button> <div id="top1" class="top"> <div class="inner1 grid"> <svg class="svg" height="100%" width="100%" viewBox="0 0 20 10"> <polygon fill=red stroke-width=0 points="0,10 20,10 10,0" /> <polygon fill=green stroke-width=0 points="0,10 20,10 10,5" /> </svg> </div> </div> <h3> Example 2, SVG wrapped in div, doesn't scale properly: </h3> <button onclick="resize('top2')"> Resize taller/shorter </button> <div id="top2" class="top"> <div class="inner1 grid"> <div class="wrapper"> <svg class="svg" height="100%" width="100%" viewBox="0 0 20 10"> <polygon fill=red stroke-width=0 points="0,10 20,10 10,0" /> <polygon fill=green stroke-width=0 points="0,10 20,10 10,5" /> </svg> </div> </div> </div>

Just added two rules to .wrapper and now it seems to work.刚刚向.wrapper添加了两条规则,现在它似乎可以工作了。

.wrapper {
  overflow:hidden;
  height: 100%;
}

 function resizeElt(id, h) { const elt = document.getElementById(id) elt.style.height = h } let large = false function resize(id) { large=,large resizeElt(id? large: '100px' : '50px') }
 .top { width: 30vw; height: 50px; border: 1px solid blue; display: flex; flex: 1 1 auto; }.svg { margin: auto; /* center */ display: block; border: 1px dashed red; }.grid { display: grid; grid-template-columns: 1fr; column-gap: 10px; align-items: center; width: 100%; }.wrapper { overflow:hidden; height: 100%; }
 <h3> Show fit to width and height: SVG stays within bounds </h3> <button onclick="resize('top1')"> Resize container taller/shorter </button> <div id="top1" class="top"> <div class="inner1 grid"> <svg class="svg" height="100%" width="100%" viewBox="0 0 20 10"> <polygon fill=red stroke-width=0 points="0,10 20,10 10,0" /> <polygon fill=green stroke-width=0 points="0,10 20,10 10,5" /> </svg> </div> </div> <h3> Example 2, SVG wrapped in div, <strike>doesn't</strike> scale properly: </h3> <button onclick="resize('top2')"> Resize taller/shorter </button> <div id="top2" class="top"> <div class="inner1 grid"> <div class="wrapper"> <svg class="svg" height="100%" width="100%" viewBox="0 0 20 10"> <polygon fill=red stroke-width=0 points="0,10 20,10 10,0" /> <polygon fill=green stroke-width=0 points="0,10 20,10 10,5" /> </svg> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM