kenzolek
kenzolek

Reputation: 344

Border radius with custom spaces between dotted

I am using custom dotted style border in my div element. I have to create custom border using background because I must define spaces between dotted. But in the corners it's not displaying due to the border radius. How can I fix that or any other solution?

I want the custom border to also follow the radius.

.element {
  width: 400px;
  height: 400px;
  background: linear-gradient(to right, black 50%, rgba(255, 255, 255, 0) 0%), linear-gradient(black 50%, rgba(255, 255, 255, 0) 0%), linear-gradient(to right, black 50%, rgba(255, 255, 255, 0) 0%), linear-gradient(black 50%, rgba(255, 255, 255, 0) 0%);
  background-position: top, right, bottom, left;
  background-repeat: repeat-x, repeat-y;
  background-size: 20px 1px, 1px 20px;

  border-radius: 70px;
}
<div class="element">
</div>

Upvotes: 6

Views: 676

Answers (2)

Temani Afif
Temani Afif

Reputation: 273004

This is probably more suitable for SVG where you can easily control the border using stroke-dasharray

svg {
  width: 250px;
}

path {
  fill: none;
  stroke-dasharray: 13, 20;
}
path.more {
  fill: none;
  stroke-dasharray: 10, 30;
}
path.less {
  fill: none;
  stroke-dasharray: 25, 15;
}
<svg viewBox="50 70 300 300">
  <path d="M100,100 h200 a80,80 0 0 1 20,20 v200 a80,80 0 0 1 -20,20 h-200 a80,80 0 0 1 -20,-20 v-200 a80,80 0 0 1 20,-20 z"  stroke="black" stroke-width="2" />
</svg>
<svg viewBox="50 70 300 300">
  <path d="M100,100 h200 a80,80 0 0 1 20,20 v200 a80,80 0 0 1 -20,20 h-200 a80,80 0 0 1 -20,-20 v-200 a80,80 0 0 1 20,-20 z" class="more"  stroke="black" stroke-width="2" />
</svg>
<svg viewBox="50 70 300 300">
  <path d="M100,100 h200 a80,80 0 0 1 20,20 v200 a80,80 0 0 1 -20,20 h-200 a80,80 0 0 1 -20,-20 v-200 a80,80 0 0 1 20,-20 z" class="less"  stroke="black" stroke-width="2" />
</svg>

Check this question for more ways about how to define/control the radius using SVG: SVG rounded corner

Another related question if you want to deal with a circle: How to create dashed circles with uniform spacing?

Upvotes: 6

jsadev.net
jsadev.net

Reputation: 2590

You could get this done using a SVG-Rectangle and stroke-dasharray like i did here:

.element {
  width: 400px;
  height: 400px;
  border-radius: 70px;
  position: relative;
}

.element svg {
  stroke-width: 0.5;
  stroke-dasharray: 10, 12;
  fill: none;
  stroke: black;
}

.element .content {
  position: absolute;
  top: 25px;
  left: 25px;
}
<div class="element">
  <svg width="400" height="400">
    <rect x="1" y="1" rx="70" ry="70" width="398" height="398">
  </svg>
  <div class="content">
    put content here...
  </div>
</div>

Upvotes: -2

Related Questions