Search code examples
htmlcsssvgcss-shapes

svg border corners with inner border


I have a some block, with some design:

enter image description here

And I have some svg code:

.box {
	position: relative;
	margin: .75em auto 0;
	max-width: 255px;
	min-height: 56px;
}

svg {
	position: absolute;
	width: 100%; height: 100%;
}
<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>

Question: how to make inside a block with the same rounded cut corners, but not with a solid fill and stroke?

P.S: it should remain the ability to edit the radius of the rounding corners, indents vn. blocks. Perhaps there is a simple implementation on css (maximum cross-browser), would also be suitable.


Solution

  • How about this? It should work for any size by just changing the dimensions of <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;
    }
    <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>