【发布时间】:2015-04-03 09:44:47
【问题描述】:
在有背景的元素上(图像或纯色并不重要):
<header id="block-header"></header>
我正在尝试使用 SVG 应用剪辑路径。为了实现这一点,我将 SVG 内联放入相同的元素中,如下所示:
<header id="block-header">
…
<svg width="100%" height="100%" viewBox="0 0 4000 1696" preserveAspectRatio="none">
<defs>
<clipPath id="myClip">
<path d="M0 1568.18V0h4000v1568.18S3206.25 1696 2000 1696C984.37 1696 0 1568.18 0 1568.18z"/>
</clipPath>
</defs>
</svg>
…
</header>
您可以运行下面的代码 sn-p 或查看JSFiddle。您可以看到内嵌的原始 SVG 图像(黑色),底部有曲线并且反应灵敏。相比之下,红色矩形显示与clip-path 相同的应用(或者更确切地说,未应用)的图像。
我想我误解了viewBox 或preserveAspectRatio 属性,但在这里找不到确切的问题。任何帮助将不胜感激。
#block-header {
background: Red;
min-height: 100px;
-webkit-clip-path: url(#myClip);
clip-path: url(#myClip);
}
<h1>SVG image</h1>
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100" viewBox="0 0 4000 1696" preserveAspectRatio="none"><path d="M0 1568.18V0h4000v1568.18S3206.25 1696 2000 1696C984.37 1696 0 1568.18 0 1568.18z"/></svg>
<h1><code>clip-path</code> using the same SVG</h1>
<header id="block-header">
<svg width="100%" height="100" viewBox="0 0 4000 1696" preserveAspectRatio="none">
<defs>
<clipPath id="myClip">
<path d="M0 1568.18V0h4000v1568.18S3206.25 1696 2000 1696C984.37 1696 0 1568.18 0 1568.18z"/>
</clipPath>
</defs>
</svg>
</header>
【问题讨论】:
标签: css svg responsive-design clip-path