【问题标题】:SVG width in IE9IE9 中的 SVG 宽度
【发布时间】:2023-03-29 00:56:01
【问题描述】:

我有一个SVG。 在 Firefox 和 Chrome 中,width:100%height:100% 运行良好。但在 Internet Explorer 9 中则不然。

有人知道怎么解决吗?

谢谢。

更新:

我真正的问题是将这个 SVG 放入 table(IE9 中的宽度很糟糕)。 <td> 是动态的,我不能为 SVG 的容器设置 width

【问题讨论】:

    标签: internet-explorer svg internet-explorer-9


    【解决方案1】:

    这是因为 html、body 和 div 元素正在折叠到 SVG 的固有高度。

    要解决这个问题,您可以将 html、body 和 div 元素设置为 100% 高度:

    html, body, div { width: 100%; height: 100%; }
    

    http://jsfiddle.net/7Z8kg/2/


    更新:

    好的,据我所知,IE9 无法在 SVG 元素上计算 widthheight,因此它回退到 default for replaced elements (300×150px)。您在父级 (.ic3-svg-arrow) 上设置了 height 而不是 width,因此 SVG 仍默认为 300px 宽。

    我在这里使用the Intrinsic Ratio trick 解决了这个问题:基本上给父元素一个纵横比(在本例中为 1:1)并使用绝对定位使 SVG 适合该大小。这取决于您事先知道纵横比,但似乎可以很好地解决这个问题:

    /* Use padding to create a 1:1 aspect ratio */
    .ic3-svg-arrow {
        height: 0;
        padding-bottom: 100%;
        position: relative;
    }
    /* Use positioning to make the SVG fit the ratio */
    .ic3-svg-arrow svg {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
    }
    

    请参阅http://jsfiddle.net/7Z8kg/15/,它在 IE9、IE11、Firefox 和 Chrome 上对我来说都是一样的。

    我的灵感来自this article, which has lots of useful SVG sizing tips

    【讨论】:

    • 它有效,我更新了问题。我真正的问题是更新版本,只是我想简化问题。行为是一样的,但我不能在容器上设置width
    • 你能在你的CSS中设置table-layout: fixed;吗?例如jsfiddle.net/7Z8kg/7
    • 奇数。我把高度放回.ic3-svg-arrow,它在Chrome、Firefox和IE11中完美运行。你有什么理由不能在.ic3-svg-arrow 上指定像素宽度吗?
    • jsfiddle.net/7Z8kg/7 在(实际的,非模拟的)IE9、10 和 11、当前的 Chrome 和 Firefox 中对我来说看起来是一样的。为什么不能设置容器的宽度?
    • 我需要它没有表格宽度:jsfiddle.net/7Z8kg/9fixed 布局没有帮助。我无法为容器设置宽度,因为宽度是从 SVG 给定的,并且单元格不同。
    猜你喜欢
    • 2015-03-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-14
    相关资源
    最近更新 更多