简体   繁体   English

CSS渐变(背景和边框)

[英]CSS gradient (background and border)

I'm trying to style an <a> tag with a gradient background and a gradient border. 我正在尝试使用渐变背景和渐变边框设置<a>标签的样式。

I followed a tutorial online and tweaked it with the right colours, however then realised it needs to set the background-image property in order to give the borders a gradient. 我在网上跟踪了一个教程并用正确的颜色调整了它,然后意识到它需要设置background-image属性以便为边框赋予渐变。

.btn-primary {
    background-color: blue;
    background-image: linear-gradient(to bottom, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100%);
    background-position: 0 0, 100% 0;
    background-repeat: no-repeat;
    background-size: 10% 100%;
    border-bottom: 4px solid #f15a24;
    border-top: 4px solid #f7931e;
    box-sizing: border-box;
    margin: 50px auto;
}

Is there a way I can modify the code so that I can specify a different gradient for the background of the button? 有没有办法可以修改代码,以便我可以为按钮的背景指定不同的渐变?

you can use border-image for the border, and just use background-image for the background gradient. 你可以使用border-image作为边框,只需使用background-image作为背景渐变。 Just like this: 像这样:

  .btn-primary { background-color: blue; background-image: linear-gradient(to bottom, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100%); background-position: 0 0, 100% 0; background-repeat: no-repeat; background-size: 10% 100%; border-bottom: 4px solid #f15a24; border-top: 4px solid #f7931e; box-sizing: border-box; margin: 50px auto; border-image: linear-gradient(to left, #f7931e 0%, #f15a24 100%), linear-gradient(to bottom, #f7931e 0%, #f15a24 100% } 

You could probably apply your rule to a pseudo-element :before or :after and then position the pseudo-element over your anchor. 您可以将规则应用于伪元素:before或:after然后将伪元素放在锚点上。

how to add a pseudo-element gradient effect 如何添加伪元素渐变效果

Hope that helps! 希望有所帮助!

Here is a way to create the illusion of a gradient border using multiple background gradients: 这是一种使用多个背景渐变创建渐变边框错觉的方法:

 .btn-primary { display:inline-block; padding:80px; background: linear-gradient(to bottom, transparent 0, transparent 30px, blue 30px, white calc(100% - 30px), transparent calc(100% - 30px), transparent 100%), linear-gradient(to left, red 0%, yellow 100%); background-repeat: no-repeat; background-position: 30px 0, 0 0; background-size: calc(100% - 60px), auto; } 
 <a class="btn-primary">test</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM