Why does my border-image not respect the border-Radius?

Asked

Viewed 1,786 times

1

I’m wearing a linear-gradient as border-imagem in an element, but thus the edge does not respect the border-radius that I placed and does not realize the curvature in the vertices.

I’d like it to stay that way: inserir a descrição da imagem aqui

But it’s getting like this: inserir a descrição da imagem aqui

Here is the code referring to the image above. I left the box-shadow for you to see that the element .box is with the border-radius functioning properly, but the border-imagem does not respect that border-radius and continues to perform no curvature at the vertices.

How can I fix this? (other than with SVG)

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    display: flex;
}

.box {
  position: relative;
  width: 400px;
  height: 160px;
  margin: auto;	
  border: 2px solid;
  border-image: linear-gradient(red, blue);
  border-image-slice: 1;
  border-radius: 20px;

  box-shadow: 0 0 0 1px green;
}
<div class="box"></div>

1 answer

2


Hello, all right?

Try the following approach, please:

I had the same problem some time ago and formulated my solution based on this topic https://gist.github.com/stereokai/36dc0095b9d24ce93b045e2ddc60d7a0

I simplified the editing of the answer, we used the border with double to double the thickness and Transparent to inhibit the default color. This way we use the new colors via background-image, and the linear-gradient contemplates the internal filling of the and the radial-gradient (can be here also linear, as you prefer) is responsible for the border color, Background-origin with border-box defines the positioning area of the background. The Background-clip: content-box. , the background is drawn inside (cut) the content box, already border-box also applied here, the background extends to outside the border of the border (but below the edge in the z-sort). I hope I’ve helped.

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    display: flex;
}

.box {
  position: relative;
  width: 400px;
  height: 160px;
  margin: auto;	
  border: double 2px transparent;
  border-radius: 20px;
  background-image: linear-gradient(white, white), radial-gradient(circle at top left, red, blue);
  background-origin: border-box;
  background-clip: content-box, border-box;
}
<div class="box"></div>

  • 2

    Friend would be interesting you put details of what you did, although it seems correct I did not understand what was done and I do not want to give only a crtl+c/v without understanding what was done there. If you copied the code from somewhere it would be interesting to quote the source

  • I simplified the editing of the answer, we used the border with double to double the thickness and Transparent to inhibit the default color. In this way we use the new colors via the background-image, and the linear-gradient contemplates the internal filling of the <div> and the radial-gradient is responsible for the color of the border. Background-origin with border-box defines the background positioning area. I hope I helped :)

  • Sorry friend but I still did not understand... Why radial-gradient and not linear-gradient? And the background-clip is not good for anything? It would be interesting to add an explanation in the field of the answer and not here in the comment, I think it is better for those who have the same doubt to be able to see right without reading the comments here

  • Yes friend, I will edit again and try to approach a more coherent explanation, thanks for the feedback, I hope I can help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.