Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round border with gradient color

enter image description here

I have an issue, I would like to have a rounded gradient border, but apparently border-radius is not working with border-image.

I attached the result, I would like the square border to be rounded.

Thank you in advanced.

.luna-icon-wrap{
  float: right;
  padding: 5px 5px;
  -webkit-border-radius: 50% 50%;
  -moz-border-radius: 50% 50%;
  border-radius: 50% 50%;
  border: 2px solid transparent;
  -moz-border-image: -moz-linear-gradient(top, #3acfd5 0%, #3a4ed5 100%);
  -webkit-border-image: -webkit-linear-gradient(top, #3acfd5 0%, #3a4ed5 100%);
  border-image: linear-gradient(to bottom, #3acfd5 0%, #3a4ed5 100%);
  border-image-slice: 1;
}	


.luna-featbox2-icon{	
  width: 70px;
  height: 70px;
  text-align: center;
  -webkit-border-radius: 50% 50%;
  -moz-border-radius: 50% 50%;
  border-radius: 50% 50%;
  background: #43257f; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(left top, #43257f, #40c4ff); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(bottom right, #43257f, #40c4ff); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(bottom right, #43257f, #40c4ff); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to bottom right, #43257f, #40c4ff); /* Standard syntax */
}
<div class="luna-icon-wrap">
  <div class="luna-featbox2-icon">
    <i class="fa fa-diamond"></i>
  </div>
</div>
like image 242
Ioana S Avatar asked Oct 25 '17 11:10

Ioana S


People also ask

How do you make a gradient border color?

To show gradients for a border with CSS you can use the border-image property. It allows setting gradient values in the same way as the background-image property. Besides the border-image property, you should specify additional properties to actually show border gradient.

Can Borders have gradients CSS?

How to create gradient borders in CSS. The '1' after the linear-gradient declaration is the border-image-slice value. By using 1, we specify we want a single border region.

Can a border have linear gradient?

Using Gradient BordersThere are three types of gradients that are supported: linear, radial, and conic. With gradients, you will need to specify a border-image-slice value of 1 .

Can I use linear gradient in border color?

You can use whatever linear-gradient or repeat-gradient you want. The border-image slice property needs to be 1 for linear gradient.


2 Answers

I'll recommend you to use SVG to create this shape. It offers simplicity and scale-ability.

We can use SVG's circle element to create a shape like above and fill / stroke it with some solid color, gradient or a pattern.

Following code will create circle shape:

<circle cx="50" cy="50" r="39" fill="blue" />

Below is a brief description of parameters used in above code:

  • cx defines the x position of circle.
  • cy defines the y position of circle.
  • r defines radius of circle.

To fill a closed shape with gradient we need to create a gradient object:

<defs>
    <linearGradient id="grad1" x1="1" y2="1">
      <stop offset="0" stop-color="#3acfd5" />
      <stop offset="1" stop-color="#3a4ed5" />
    </linearGradient>
</defs>

This object can be referenced by fill and stroke attribute in a shape using id like fill="url(#grad1) or stroke="url(#grad1). And the direction of gradient can be controlled by its x1, y1, x2 and y2 attributes.

Output Image:

Circle with gradient border

Working Example:

<svg width="100" height="100" viewBox="0 0 100 100">
  <defs>
    <linearGradient id="grad1" x1="1" y2="1">
      <stop offset="0" stop-color="#3acfd5" />
      <stop offset="1" stop-color="#3a4ed5" />
    </linearGradient>
    <linearGradient id="grad2" y2="1">
      <stop offset="0" stop-color="#43257f" />
      <stop offset="1" stop-color="#40c4ff" />
    </linearGradient>
  </defs>
  <circle cx="50" cy="50" r="45" fill="none" stroke="url(#grad1)" stroke-width="2" />
  <circle cx="50" cy="50" r="39" fill="url(#grad2)" />
</svg>

Useful Resources:

Below are some useful links for SVG:

  • Specification
  • MDN
like image 130
Mohammad Usman Avatar answered Sep 28 '22 18:09

Mohammad Usman


This can be achieved with CSS using a pseudo class like :before.

Sadly, it doesnt have the transparent part between the border and the circle itself but if you know its always going to be on a certain colored background, that shouldn't be a problem.

.luna-icon-wrap{
  float: right;
  padding: 1px 1px;
  -webkit-border-radius: 50% 50%;
  -moz-border-radius: 50% 50%;
  border-radius: 50% 50%;
  border: 2px solid transparent;
  position: relative;
}	
.luna-icon-wrap:before {
content: '';
  position: absolute;
  top: -2px;
  bottom: -2px;
  left: -2px;
  right: -2px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1e5799 0%,#7db9e8 100%);
  z-index: -2;
}

.luna-featbox2-icon{	
  width: 70px;
  height: 70px;
  text-align: center;
  -webkit-border-radius: 50% 50%;
  -moz-border-radius: 50% 50%;
  border-radius: 50% 50%;
  border: 4px solid white;
  background: #43257f; /* For browsers that do not support gradients */
  background: -webkit-linear-gradient(left top, #43257f, #40c4ff); /* For Safari 5.1 to 6.0 */
  background: -o-linear-gradient(bottom right, #43257f, #40c4ff); /* For Opera 11.1 to 12.0 */
  background: -moz-linear-gradient(bottom right, #43257f, #40c4ff); /* For Firefox 3.6 to 15 */
  background: linear-gradient(to bottom right, #43257f, #40c4ff); /* Standard syntax */
}
<div class="luna-icon-wrap">
  <div class="luna-featbox2-icon">
    <i class="fa fa-diamond"></i>
  </div>
</div>
like image 41
Stewartside Avatar answered Sep 28 '22 19:09

Stewartside