Ham*_*ter 3 html css svg css-shapes
我有一些块,有一些设计:
我有一些 svg 代码:
.box {
position: relative;
margin: .75em auto 0;
max-width: 255px;
min-height: 56px;
}
svg {
position: absolute;
width: 100%; height: 100%;
}Run Code Online (Sandbox Code Playgroud)
<div class='box'>
<svg>
<mask id='m' fill='#fff'>
<rect id='r' width='256' height='56'/>
<circle id='c' r='10' fill='#000'/>
<use xlink:href='#c' x='100%'/>
<use xlink:href='#c' y='100%'/>
<use xlink:href='#c' x='100%' y='100%'/>
</mask>
<mask id='m2' fill='#fff'>
<rect id='r2' width='248' height='50' x="4" y="4" />
<circle id='c2' r='14' fill='#000' stroke='#000'/>
<use xlink:href='#c2' x='100%' />
<use xlink:href='#c2' y='100%'/>
<use xlink:href='#c2' x='100%' y='100%'/>
</mask>
<use xlink:href='#r' fill='red' mask='url(#m)'/>
<use xlink:href='#r2' fill='none' stroke="#000" mask='url(#m2)'/>
</svg>
</div>Run Code Online (Sandbox Code Playgroud)
问题:如何在块内部制作具有相同圆角的切角,但不具有实心填充和描边?
PS:它应该保留编辑圆角半径、缩进vn的能力。块。也许在css上有一个简单的实现(最大跨浏览器),也会合适。
我会选择使用多个背景的纯 CSS 解决方案。操作起来有点技巧,但使用一些 CSS 变量可以轻松调整:
.box {
--th:2px; /*thickness of the transparent part*/
--l:4px; /*height of border*/
--r:25px; /*radius*/
--rad:transparent calc(103% - var(--th) - var(--l) - 1px),
#000 calc(103% - var(--th) - var(--l))
calc(103% - var(--th) - 1px),
transparent calc(103% - var(--th)) 103%,
#000 103%;
--rad-s:var(--r) var(--r);
--border:#000 calc(var(--l)),transparent calc(var(--l)),transparent calc(var(--l) + var(--th));
--w:calc(100% - 2*var(--r) + 2*var(--th));
--h:calc(var(--l) + var(--th));
margin:10px;
display:inline-block;
padding:40px 20px;
background:
/*The 4 borders*/
linear-gradient(to bottom,var(--border)) top /var(--w) var(--h),
linear-gradient(to top ,var(--border)) bottom/var(--w) var(--h),
linear-gradient(to right ,var(--border)) left /var(--h) var(--w),
linear-gradient(to left ,var(--border)) right /var(--h) var(--w),
/*The 4 corners */
radial-gradient(farthest-side at top right,var(--rad)) top right/var(--rad-s),
radial-gradient(farthest-side at top left ,var(--rad)) top left /var(--rad-s),
radial-gradient(farthest-side at bottom right,var(--rad)) bottom right/var(--rad-s),
radial-gradient(farthest-side at bottom left ,var(--rad)) bottom left /var(--rad-s),
/*The main background*/
linear-gradient(#000,#000) center/calc(100% - 2*var(--r)) calc(100% - 2*var(--h)),
linear-gradient(#000,#000) center/calc(100% - 2*var(--h)) calc(100% - 2*var(--r));
background-repeat:no-repeat;
color:#fff;
text-align:center;
}
body {
background:pink;
}Run Code Online (Sandbox Code Playgroud)
<div class="box">
Some text inside
</div>
<div class="box" style="--th:3px;--r:20px">
Some text inside
</div>
<div class="box" style="--th:4px;--r:40px;--l:8px">
Some text inside
</div>
<div class="box" style="--th:5px;--r:30px">
Some text inside
</div>
<div class="box" style="--th:1px;--r:15px;--l:3px">
Some text inside
</div>Run Code Online (Sandbox Code Playgroud)
这个怎么样?只需更改 的尺寸,它就可以适用于任何尺寸<div class="box">。
.box {
position: relative;
margin: .75em auto 0;
width: 255px;
height: 56px;
}
.box svg {
position: absolute;
width: 100%;
height: 100%;
}
.size2 {
width: 455px;
height: 256px;
}Run Code Online (Sandbox Code Playgroud)
<div class="box">
<svg width="100%" height="100%">
<mask id="m" fill="#fff">
<rect width="100%" height="100%"/>
<rect width="100%" height="100%" fill="none" stroke="#000" stroke-width="12"/>
<circle r="16" fill="#000"/>
<circle cx="100%" r="16" fill="#000"/>
<circle cy="100%" r="16" fill="#000"/>
<circle cx="100%" cy="100%" r="16" fill="#000"/>
<rect width="100%" height="100%" fill="none" stroke="#fff" stroke-width="8"/>
<circle r="14" fill="#fff"/>
<circle cx="100%" r="14" fill="#fff"/>
<circle cy="100%" r="14" fill="#fff"/>
<circle cx="100%" cy="100%" r="14" fill="#fff"/>
<circle r="10" fill="#000"/>
<circle cx="100%" r="10" fill="#000"/>
<circle cy="100%" r="10" fill="#000"/>
<circle cx="100%" cy="100%" r="10" fill="#000"/>
</mask>
<rect width="100%" height="100%" mask="url(#m)"/>
</svg>
</div>Run Code Online (Sandbox Code Playgroud)