使用CSS修剪容器的角落。

huangapple go评论69阅读模式
英文:

Cut corners of container using CSS

问题

我尝试使用CSS来裁剪容器的角落,类似于这样:

我想要实现的效果

我尝试使用clip-path,但我无法使裁剪部分像上面的图片那样圆润。

目前的代码

clip-path: polygon(0 0, 100% 0, 100% calc(100% - 50px), calc(100% - 100px) calc(100% - 50px), calc(100% - 120.40px) 100%, 0 100%);

如需进一步帮助,请告诉我。

英文:

Im trying to cut the corners of a container using css like this

what im trying to achieve

I tried using clip-path but I cant manage to make the cuts rounded like in the picture above
what i have so far

clip-path: polygon(0 0,100% 0,100% calc(100% - 50px),calc(100% - 100px) calc(100% - 50px),calc(100% - 120.40px) 100%,0 100%);

答案1

得分: 0

以下是您要翻译的内容:

"I don't think you can do that with polygon clip-path only, there are outer angles and inside angles...

With svg it can be possible but a bit tricky.

First needed to design the svg shape by itself (red fill is only to view something in the svg editor: inkscape here).

<svg id="clipContainer" width="1920" height="1080" viewBox="0 0 1920 1080" xmlns="http://www.w3.org/2000/svg">
    <path style="fill:#ff0000;stroke:none;"
          d="m 35,0 h 654.05318 c 6.84803,0 13.02979,2.8828963 16.6429,6.7452744 l 63.1577,76.5094516 C 772.54893,87.959545 779.6304,90 785,90 h 350 c 5.6694,-0.04033 12.8528,-3.528812 17.4966,-8.545814 L 1213.8538,7.1954094 C 1219.2498,1.0400203 1224.6973,0 1230.1965,0 H 1885 c 18.8682,0 35,16.120082 35,35 v 875 c -0.2052,10.54136 -8.9432,19.77096 -20,20 h -608.8105 c -13.0687,-0.12776 -25.0997,3.20977 -33.0435,11.97991 L 1147.2068,1064.9984 C 1134.8748,1081.5357 1126.0043,1080 1106.409,1080 H 35 C 16.13187,1080 0,1063.8799 0,1045 V 35 C 0,16.120082 16.13187,0 35,0 Z"/>
</svg>

现在需要将它放入HTML主体,但作为剪辑路径:

<div class="container">
</div>
<svg width="0" height="0">
    <defs>
        <clipPath id="svg-clip">
            <path style="fill:#ff0000;stroke:none;"
                    d="m 0.01822917,0 h 0.34065265 c 0.003567,0 0.006786,0.00266925 0.008668,0.0062454 l 0.0328946,0.07083957 c 0.001926,0.0043561 0.005613,0.0062455 0.008408,0.0062455 h 0.18229167 c 0.002953,-3.739e-5 0.006695,-0.0032675 0.009113,-0.0079124 L 0.63221469,0.00666224 C 0.63502588,9.6294739e-4 0.63786313,0 0.64072729,0 H 0.98177075 C 0.99159837,0 1,0.01492547 1,0.03240625 v 0.8101562 c -1.0691e-4,0.009762 -0.004658,0.0183059 -0.0104162,0.0185171 H 0.67249445 c -0.006807,-1.1867e-4 -0.0130727,0.00297 -0.0172102,0.0110924 l -0.0577808,0.11390178 c -0.006423,0.0153109 -0.0110433,0.0138917 -0.0212488,0.0138917 H 0.01822917 C 0.00840202,0.99996424 0,0.98503874 0,0.96755799 V 0.03240625 C 0,0.01492547 0.00840202,0 0.01822917,0 Z"/>
        </clipPath>
    </defs>
</svg>

再加上一些CSS,表示容器使用了剪辑路径svg-clip:

body {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: grey;
}
.container {
    position: relative;
    width: 90vw;
    height: 90vh;
    background-color: #222222;
    border-radius: 2vw;
    clip-path: url(#svg-clip);
}

现在的问题是缩放!这可以实现,但不是完全按照容器的比例。我们将使用clipPathUnits="objectBoundingBox",但为此,需要将路径的值设置在0到1之间(是的,是1像素!)。因此,在Inkscape中编辑SVG后,代码如下:

<svg id="clipContainer" width="1920" height="1080" viewBox="0 0 1920 1080" xmlns="http://www.w3.org/2000/svg">
    <path style="fill:#ff0000;stroke:none;"
               d="m 0.01822917,0 h 0.34065265 c 0.003567,0 0.006786,0.00266925 0.008668,0.0062454 l 0.0328946,0.07083957 c 0.001926,0.0043561 0.005613,0.0062455 0.008408,0.0062455 h 0.18229167 c 0.002953,-3.739e-5 0.006695,-0.0032675 0.009113,-0.0079124 L 0.63221469,0.00666224 C 0.63502588,9.6294739e-4 0.63786313,0 0.64072729,0 H 0.98177075 C 0.99159837,0 1,0.01492547 1,0.03240625 v 

<details>
<summary>英文:</summary>

I don&#39;t think you can do that with polygon clip-path only, there are outer angles and inside angles...

With svg it can be possible but a bit tricky.

First needed to design the svg shape by itself (red fill is only to view something in the svg editor: inkscape here).

```html
&lt;svg id=&quot;clipContainer&quot; width=&quot;1920&quot; height=&quot;1080&quot; viewBox=&quot;0 0 1920 1080&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
    &lt;path style=&quot;fill:#ff0000;stroke:none;&quot;
          d=&quot;m 35,0 h 654.05318 c 6.84803,0 13.02979,2.8828963 16.6429,6.7452744 l 63.1577,76.5094516 C 772.54893,87.959545 779.6304,90 785,90 h 350 c 5.6694,-0.04033 12.8528,-3.528812 17.4966,-8.545814 L 1213.8538,7.1954094 C 1219.2498,1.0400203 1224.6973,0 1230.1965,0 H 1885 c 18.8682,0 35,16.120082 35,35 v 875 c -0.2052,10.54136 -8.9432,19.77096 -20,20 h -608.8105 c -13.0687,-0.12776 -25.0997,3.20977 -33.0435,11.97991 L 1147.2068,1064.9984 C 1134.8748,1081.5357 1126.0043,1080 1106.409,1080 H 35 C 16.13187,1080 0,1063.8799 0,1045 V 35 C 0,16.120082 16.13187,0 35,0 Z&quot;/&gt;
&lt;/svg&gt;

now have to put it in HTML body but as a clip-path:

&lt;div class=&quot;container&quot;&gt;
&lt;/div&gt;
&lt;svg width=&quot;0&quot; height=&quot;0&quot;&gt;
    &lt;defs&gt;
        &lt;clipPath id=&quot;svg-clip&quot;&gt;
            &lt;path style=&quot;fill:#ff0000;stroke:none;&quot;
                    d=&quot;m 0.01822917,0 h 0.34065265 c 0.003567,0 0.006786,0.00266925 0.008668,0.0062454 l 0.0328946,0.07083957 c 0.001926,0.0043561 0.005613,0.0062455 0.008408,0.0062455 h 0.18229167 c 0.002953,-3.739e-5 0.006695,-0.0032675 0.009113,-0.0079124 L 0.63221469,0.00666224 C 0.63502588,9.6294739e-4 0.63786313,0 0.64072729,0 H 0.98177075 C 0.99159837,0 1,0.01492547 1,0.03240625 v 0.8101562 c -1.0691e-4,0.009762 -0.004658,0.0183059 -0.0104162,0.0185171 H 0.67249445 c -0.006807,-1.1867e-4 -0.0130727,0.00297 -0.0172102,0.0110924 l -0.0577808,0.11390178 c -0.006423,0.0153109 -0.0110433,0.0138917 -0.0212488,0.0138917 H 0.01822917 C 0.00840202,0.99996424 0,0.98503874 0,0.96755799 V 0.03240625 C 0,0.01492547 0.00840202,0 0.01822917,0 Z&quot;/&gt;
        &lt;/clipPath&gt;
    &lt;/defs&gt;
&lt;/svg&gt;

plus a bit of css saying container using the clip-path svg-clip:

        body {
            display: flex;
            justify-content: center;
            align-items: center;
            background-color: grey;
        }
        .container {
            position: relative;
            width: 90vw;
            height: 90vh;
            background-color: #222222;
            border-radius: 2vw;
            clip-path: url(#svg-clip);
        }

Now the problem is the scale! That works but not really following the scale of the container. We will use clipPathUnits="objectBoundingBox" but to do so we need to have value of the path between 0 and 1 (yes yes 1 px!!!). So after edit in inkscape the svg is:

&lt;svg id=&quot;clipContainer&quot; width=&quot;1920&quot; height=&quot;1080&quot; viewBox=&quot;0 0 1920 1080&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
    &lt;path style=&quot;fill:#ff0000;stroke:none;&quot;
               d=&quot;m 0.01822917,0 h 0.34065265 c 0.003567,0 0.006786,0.00266925 0.008668,0.0062454 l 0.0328946,0.07083957 c 0.001926,0.0043561 0.005613,0.0062455 0.008408,0.0062455 h 0.18229167 c 0.002953,-3.739e-5 0.006695,-0.0032675 0.009113,-0.0079124 L 0.63221469,0.00666224 C 0.63502588,9.6294739e-4 0.63786313,0 0.64072729,0 H 0.98177075 C 0.99159837,0 1,0.01492547 1,0.03240625 v 0.8101562 c -1.0691e-4,0.009762 -0.004658,0.0183059 -0.0104162,0.0185171 H 0.67249445 c -0.006807,-1.1867e-4 -0.0130727,0.00297 -0.0172102,0.0110924 l -0.0577808,0.11390178 c -0.006423,0.0153109 -0.0110433,0.0138917 -0.0212488,0.0138917 H 0.01822917 C 0.00840202,0.99996424 0,0.98503874 0,0.96755799 V 0.03240625 C 0,0.01492547 0.00840202,0 0.01822917,0 Z&quot;/&gt;
&lt;/svg&gt;

That gives the following snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: grey;
}

.container {
  position: relative;
  width: 90vw;
  height: 90vh;
  background-color: #222222;
  border-radius: 2vw;
  clip-path: url(#svg-clip);
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
&lt;/div&gt;
&lt;svg width=&quot;0&quot; height=&quot;0&quot;&gt;
    &lt;defs&gt;
        &lt;clipPath id=&quot;svg-clip&quot; clipPathUnits=&quot;objectBoundingBox&quot;&gt;
            &lt;path style=&quot;fill:#ff0000;stroke:none;&quot;
                    d=&quot;m 0.01822917,0 h 0.34065265 c 0.003567,0 0.006786,0.00266925 0.008668,0.0062454 l 0.0328946,0.07083957 c 0.001926,0.0043561 0.005613,0.0062455 0.008408,0.0062455 h 0.18229167 c 0.002953,-3.739e-5 0.006695,-0.0032675 0.009113,-0.0079124 L 0.63221469,0.00666224 C 0.63502588,9.6294739e-4 0.63786313,0 0.64072729,0 H 0.98177075 C 0.99159837,0 1,0.01492547 1,0.03240625 v 0.8101562 c -1.0691e-4,0.009762 -0.004658,0.0183059 -0.0104162,0.0185171 H 0.67249445 c -0.006807,-1.1867e-4 -0.0130727,0.00297 -0.0172102,0.0110924 l -0.0577808,0.11390178 c -0.006423,0.0153109 -0.0110433,0.0138917 -0.0212488,0.0138917 H 0.01822917 C 0.00840202,0.99996424 0,0.98503874 0,0.96755799 V 0.03240625 C 0,0.01492547 0.00840202,0 0.01822917,0 Z&quot;/&gt;
        &lt;/clipPath&gt;
    &lt;/defs&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 20:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定