CSS 设置渐变背景 CSS 设置渐变边框

一、css渐变背景添加透明度opacity

css渐变背景经常会在项目开发中遇到,此时UI如果给出的是单一的渐变背景(没有背景透明度),这个我们会很快的写出代码,如下:

<div class="btn">这是一个按钮</div>
.btn {
  background: linear-gradient(to right, #8B78E6, #50A5A5);
  color: #fff;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: background 0.3s ease;
}

.btn:hover {
  background: linear-gradient(to right, #50A5A5, #8B78E6);
}

但偶尔的时候UI会给煎饼的背景色添加一个透明度,但是使用opacity属性会导致文本也会有透明度,接下来给出我的解决办法

.btn {
  position: relative;
  color: #fff;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: background 0.3s ease;
  z-index: 0;
}

.btn:hover {
  background: linear-gradient(to right, #50A5A5, #8B78E6);
}

.btn::after{
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  content: "";
  opacity: 0.3;
  background: linear-gradient(to right, #8B78E6, #50A5A5);
}

 二、css按钮添加渐变边框

1、使用border-image

.btn {
  border: 4px solid;
  border-image: linear-gradient(to right, #8f41e9, #578aef) 1;
}

/* 或者 */
.btn {
  border: 4px solid;
  border-image-source: linear-gradient(to right, #8f41e9, #578aef);
  border-image-slice: 1;
}

2、使用伪元素

<div class="border-box">
   <div class="content">
        这是一个使用伪元素实现渐变边框的按钮
   </div>
</div>
.border-box {
  border: 1px solid transparent;
  border-radius: 16px;
  position: relative;
  color: #F2B05E;
  background-color: #fff;
  background-clip: padding-box; /*important*/
}

.border-box::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: -1;
  margin: -1px;
  border-radius: inherit; /*important*/
  background: linear-gradient(to right, #8F41E9, #578AEF);
}

3、使用单个元素background-clipbackground-originbackground-image 

.border-box {
  border: 1px solid transparent;
  border-radius: 16px;
  color: #F2B05E;
  background-clip: padding-box, border-box;
  background-origin: padding-box, border-box;
  background-image: linear-gradient(to right, #fff,#fff), linear-gradient(90deg, #8F41E9, #578AEF);
}

background-clip 、background-origin属性添加了解释链接,没弄清的童鞋可以点击看下~

分享完毕~~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 CSS 中的 linear-gradient() 函数来实现 div 的渐变背景渐变边框。 下面是一个设置渐变背景渐变边框的示例代码: ```html <div class="gradient-box"></div> ``` ```css .gradient-box { width: 200px; height: 200px; background: linear-gradient(to bottom right, #ff0080, #ff8c00); border: 2px solid; border-image: linear-gradient(to bottom right, #ff0080, #ff8c00) 1; } ``` 在上面的代码中,使用 `background` 属性来设置 div 的渐变背景。`linear-gradient()` 函数中的参数 `to bottom right` 表示渐变方向为从左上角到右下角,`#ff0080` 和 `#ff8c00` 分别表示渐变的起始颜色和结束颜色。 使用 `border` 属性来设置 div 的边框,同时使用 `border-image` 属性来设置边框渐变。`linear-gradient()` 函数中的参数同样表示渐变方向,`1` 表示边框宽度为 1 像素。 需要注意的是,`border-image` 属性在一些浏览器中可能不被支持,此时可以使用 `background-clip` 属性来实现相同的效果。代码如下: ```css .gradient-box { width: 200px; height: 200px; background: linear-gradient(to bottom right, #ff0080, #ff8c00); border: 2px solid transparent; padding: 2px; background-clip: content-box, border; } ``` 在上面的代码中,使用 `padding` 属性来为 div 添加内边距,同时将 `border` 属性的颜色设置为 `transparent`,使边框不可见。使用 `background-clip` 属性来指定背景的绘制区域,`content-box` 表示背景仅在内容区域绘制,`border` 表示背景边框区域绘制。这样,就可以实现相同的渐变边框效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值