【发布时间】:2021-06-10 11:12:18
【问题描述】:
当在一个固定宽度的 SVG 中显示一些 SVG 图标时,它们应该被剪裁到该 SVG 容器的宽度。
在所有明智的浏览器中,这都可以正常工作,但在 IE11 中,图标超出了容器的宽度。
是否有任何解决方法来应对这种行为?
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<svg class="task" width="50">
<rect class="task-rectangle" fill="#FFE5E5" width="50" height="50"></rect>
<svg class="svgs-using-def-with-image-href" x="4" y="5">
<use href="#GreenTick" x="0"/>
<use href="#Triangle" x="18"/>
<use href="#Facebook" x="36"/>
</svg>
<svg class="reusable-icons" width="0" height="0">
<defs>
<svg id="GreenTick" width="18" height="18">
<image href="https://svgur.com/i/XvH.svg" width="18" height="18"/>
</svg>
<svg id="Triangle" width="18" height="18">
<image href="https://svgur.com/i/XwA.svg" width="18" height="18"/>
</svg>
<svg id="Facebook" width="18" height="18">
<image href="https://svgur.com/i/Xx8.svg" width="18" height="18"/>
</svg>
</defs>
</svg>
</svg>
</body>
</html>
【问题讨论】:
-
不要嵌套 SVG 标签——现在有更多的人这样做,但是浏览器没有测试这个用例——人们遇到了很多错误。请改用 g 元素。
-
尝试在溢出内容的 svg 标签上显式设置 overflow="hidden"。这是默认设置,但 IE 可能需要轻推。
-
@MichaelMullany 我不明白你为什么不嵌套 SVG,你只使用 g 元素严重限制了你的选择,我从未见过或读过任何东西说这是一个坏主意。为了繁荣,我将其更改为没有嵌套的 svg 元素,并且只使用 g 元素。出现同样的问题。
-
@RobertLongson 我确实尝试在各个级别添加 overflow="hidden",但功能上没有任何变化似乎无法兑现。
-
您可能需要定义并使用显式剪辑路径。
标签: html svg icons internet-explorer-11