在MDN上有一个关于如何在图像上使用剪辑路径SVG的示例。但是,相同的剪辑路径似乎不能应用于
元素。有人能够澄清一下吗?
- 为什么这段代码无法按预期工作
- 让SVG剪辑路径在div上起作用的方法
示例代码(基于MDN文档link)对图像进行裁剪
#clipped {
clip-path: url(#cross);
}
<img id="clipped" src="https://mdn.mozillademos.org/files/12668/MDN.svg"
alt="MDN logo">
<svg height="0" width="0">
<defs>
<clipPath id="cross">
<rect y="110" x="137" width="90" height="90"/>
<rect x="0" y="110" width="90" height="90"/>
<rect x="137" y="0" width="90" height="90"/>
<rect x="0" y="0" width="90" height="90"/>
</clipPath>
</defs>
</svg>
上相同的剪切路径(似乎不起作用)
#clipped {
width: 100px;
height: 100px;
background: black;
clip-path: url(#cross);
}
<div id="clipped"></div>
<svg height="0" width="0">
<defs>
<clipPath id="cross">
<rect y="110" x="137" width="90" height="90"/>
<rect x="0" y="110" width="90" height="90"/>
<rect x="137" y="0" width="90" height="90"/>
<rect x="0" y="0" width="90" height="90"/>
</clipPath>
</defs>
</svg>
width: 300px; height: 300px;
。 - enxaneta