是否可以用此图像遮盖div的形状?
这是我要实现的目标:
这是我到目前为止使用SVG标签所做的。但这并没有给我确切的输出:
代码
<svg viewBox="0 0 643 525">
<defs>
<clipPath id="shape">
<rect width="150" height="200" style="fill:rgb(0,0,255);stroke-width:5" />
<rect x="160" y="100" width="150" height="100" style="fill:rgb(0,0,255);stroke-width:5" />
<rect x="50" y="210" width="100" height="80" />
<rect x="160" y="210" width="225" height="190" />
</clipPath>
</defs>
<image width="643" height="343" clip-path="url(#shape)" xlink:href="../assets/images/crain.png" x="-50"></image>
</svg>
答案 0 :(得分:0)
使用CSS蒙版,您可以执行以下操作:
body {
margin:0;
background:grey;
}
img {
-webkit-mask:
/* top right part*/
linear-gradient(#fff,#fff) calc(82% + 10px) calc(33% - 10px)/35% 25%,
/* bottom left part*/
linear-gradient(#fff,#fff) calc(34% - 10px) calc(80% + 10px)/25% 25%,
/* Top left part */
linear-gradient(#fff,#fff) top left/calc(50% - 10px) calc(50% + 30px),
linear-gradient(#fff,#fff) top left/calc(50% + 10px) calc(50% - 10px),
/* Bottom right part*/
linear-gradient(#fff,#fff) bottom right/50% 50%;
mask:
/* top right part*/
linear-gradient(#fff,#fff) calc(82% + 10px) calc(33% - 10px)/35% 25%,
/* bottom left part*/
linear-gradient(#fff,#fff) calc(34% - 10px) calc(80% + 10px)/25% 25%,
/* Top left part */
linear-gradient(#fff,#fff) top left/calc(50% - 10px) calc(50% + 30px),
linear-gradient(#fff,#fff) top left/calc(50% + 10px) calc(50% - 10px),
/* Bottom right part*/
linear-gradient(#fff,#fff) bottom right/50% 50%;
-webkit-mask-repeat:no-repeat;
mask-repeat:no-repeat;
}
<img src="https://picsum.photos/id/1028/300/300" >
您可以添加CSS变量来控制间距
body {
margin:0;
background:grey;
}
img {
margin:5px;
--g:10px; /* the gap */
-webkit-mask:
/* top right part*/
linear-gradient(#fff,#fff) calc(84.5% + var(--g)) calc(33.5% - var(--g))/35% 25%,
/* bottom left part*/
linear-gradient(#fff,#fff) calc(34% - var(--g)) calc(80% + var(--g))/25% 25%,
/* Top left part */
linear-gradient(#fff,#fff) top left/calc(50% - var(--g)) 60%,
linear-gradient(#fff,#fff) top left/55% calc(50% - var(--g)),
/* Bottom right part*/
linear-gradient(#fff,#fff) bottom right/50% 50%;
mask:
/* top right part*/
linear-gradient(#fff,#fff) calc(84.5% + var(--g)) calc(33.5% - var(--g))/35% 25%,
/* bottom left part*/
linear-gradient(#fff,#fff) calc(34% - var(--g)) calc(80% + var(--g))/25% 25%,
/* Top left part */
linear-gradient(#fff,#fff) top left/calc(50% - var(--g)) 60%,
linear-gradient(#fff,#fff) top left/55% calc(50% - var(--g)),
/* Bottom right part*/
linear-gradient(#fff,#fff) bottom right/50% 50%;
-webkit-mask-repeat:no-repeat;
mask-repeat:no-repeat;
}
<img src="https://picsum.photos/id/1028/300/300" style="--g:20px">
<img src="https://picsum.photos/id/90/250/250" >
<img src="https://picsum.photos/id/102/200/200" style="--g:4px" >
要了解技巧,请使用渐变作为div元素的背景以查看拼图。由于我使用白色,因此遮罩将仅显示渐变部分:
body {
margin:0;
background:grey;
}
.box {
width:300px;
height:300px;
border:1px solid;
--g:10px; /* the gap */
background:
/* bottom left part*/
linear-gradient(red,red) calc(84.5% + var(--g)) calc(33.5% - var(--g))/35% 25%,
/* bottom left part*/
linear-gradient(blue,blue) calc(34% - var(--g)) calc(80% + var(--g))/25% 25%,
/* Top left part */
linear-gradient(green,green) top left/calc(50% - var(--g)) 60%,
linear-gradient(yellow,yellow) top left/55% calc(50% - var(--g)),
/* Bottom right part*/
linear-gradient(purple,purple) bottom right/50% 50%;
background-repeat:no-repeat;
}
<div class="box"></div>
要了解我使用的所有百分比值背后的逻辑,请检查以下内容:Using percentage values with background-position on a linear gradient