CSS 中的 4 个渐变边框
4 gradient borders in CSS
我需要帮助在盒子的所有 4 个边上应用 渐变边框。我试过了,但它只适用于两侧。查看所有链接和 SO 答案后,我得到了这个:
.test{
height:250px;
border: 2px solid;
border-image: linear-gradient(to left,rgba(78,137,176,1) 1%, rgba(115,192,85,1) 100%) 100% 0 100% 0/2px 0 2px 0;
padding-top:50px;
}
<div class="test">
This is a box and I want borders for all the sides
</div>
如有任何帮助,我将不胜感激。我正在尝试类似于下图的东西。
谢谢。
使用背景图片:(生成与您的图片完全相同的输出)
你似乎在每一边都有 不同的 的渐变,所以很难用 border-image
属性。您可以尝试使用 background-image
模仿行为,如下面的代码片段所示。
基本上下面的代码片段所做的是,它为 4 个边中的每一个创建渐变作为渐变背景图像条,然后使用 background-position
将它们放置在正确的位置。
父项上的透明边框是一个占位符,模仿的边框最终会出现在该位置。 background-origin: border-box
使元素的背景从 border-box
区域本身开始(而不是 padding-box
或 content-box
)。这两个只是额外的步骤,以避免在 background-position
.
中使用不必要的 calc
内容
.test {
height: 250px;
border: 2px solid transparent;
background-image: linear-gradient(to right, rgb(187, 210, 224), rgb(203, 231, 190)), linear-gradient(to bottom, rgb(114, 191, 87), rgb(116, 191, 86)), linear-gradient(to left, rgb(204, 233, 187), rgb(187, 210, 224)), linear-gradient(to top, rgb(84, 144, 184), rgb(80, 138, 176));
background-origin: border-box;
background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
background-position: top left, top right, bottom right, bottom left;
background-repeat: no-repeat;
padding-top: 50px;
}
<div class="test">
This is a box and i want border for all the side
</div>
使用边框图像:(在所有 4 个边上生成边框,但输出与图像不同)
您可以使用 border-image
属性 获得的最佳输出如下所示,但正如您从演示中看到的那样,它与您的图像(或第一个片段的输出)不完全相同):
.test {
height: 250px;
border: 2px solid;
border-image: linear-gradient(to left, rgba(78, 137, 176, 1) 1%, rgba(115, 192, 85, 1) 100%);
border-image-slice: 1;
padding-top:50px;
}
<div class="test">
This is a box and i want border for all the side
</div>
我以这种方式为自己实现了这一点:
background-image 中的背景变化。
div {
width: 170px;
height: 48px;
border-style: solid;
border-width: 2px;
border-image-source: linear-gradient(to bottom, #fff042, #ff5451);
border-image-slice: 1;
background-image: linear-gradient(to bottom, #f9e6e6, #c5e0c3), linear-gradient(to bottom, #fff042, #ff5451);
background-origin: border-box;
background-clip: content-box, border-box;
display: flex;
align-items: center;
justify-content: center;
text-transform: uppercase;
}
<div>text</div>
我需要帮助在盒子的所有 4 个边上应用 渐变边框。我试过了,但它只适用于两侧。查看所有链接和 SO 答案后,我得到了这个:
.test{
height:250px;
border: 2px solid;
border-image: linear-gradient(to left,rgba(78,137,176,1) 1%, rgba(115,192,85,1) 100%) 100% 0 100% 0/2px 0 2px 0;
padding-top:50px;
}
<div class="test">
This is a box and I want borders for all the sides
</div>
如有任何帮助,我将不胜感激。我正在尝试类似于下图的东西。 谢谢。
使用背景图片:(生成与您的图片完全相同的输出)
你似乎在每一边都有 不同的 的渐变,所以很难用 border-image
属性。您可以尝试使用 background-image
模仿行为,如下面的代码片段所示。
基本上下面的代码片段所做的是,它为 4 个边中的每一个创建渐变作为渐变背景图像条,然后使用 background-position
将它们放置在正确的位置。
父项上的透明边框是一个占位符,模仿的边框最终会出现在该位置。 background-origin: border-box
使元素的背景从 border-box
区域本身开始(而不是 padding-box
或 content-box
)。这两个只是额外的步骤,以避免在 background-position
.
calc
内容
.test {
height: 250px;
border: 2px solid transparent;
background-image: linear-gradient(to right, rgb(187, 210, 224), rgb(203, 231, 190)), linear-gradient(to bottom, rgb(114, 191, 87), rgb(116, 191, 86)), linear-gradient(to left, rgb(204, 233, 187), rgb(187, 210, 224)), linear-gradient(to top, rgb(84, 144, 184), rgb(80, 138, 176));
background-origin: border-box;
background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
background-position: top left, top right, bottom right, bottom left;
background-repeat: no-repeat;
padding-top: 50px;
}
<div class="test">
This is a box and i want border for all the side
</div>
使用边框图像:(在所有 4 个边上生成边框,但输出与图像不同)
您可以使用 border-image
属性 获得的最佳输出如下所示,但正如您从演示中看到的那样,它与您的图像(或第一个片段的输出)不完全相同):
.test {
height: 250px;
border: 2px solid;
border-image: linear-gradient(to left, rgba(78, 137, 176, 1) 1%, rgba(115, 192, 85, 1) 100%);
border-image-slice: 1;
padding-top:50px;
}
<div class="test">
This is a box and i want border for all the side
</div>
我以这种方式为自己实现了这一点:
background-image 中的背景变化。
div {
width: 170px;
height: 48px;
border-style: solid;
border-width: 2px;
border-image-source: linear-gradient(to bottom, #fff042, #ff5451);
border-image-slice: 1;
background-image: linear-gradient(to bottom, #f9e6e6, #c5e0c3), linear-gradient(to bottom, #fff042, #ff5451);
background-origin: border-box;
background-clip: content-box, border-box;
display: flex;
align-items: center;
justify-content: center;
text-transform: uppercase;
}
<div>text</div>