用css设置背景以及边框渐变

1.设置背景颜色的渐变

        颜色的渐变可以通过改变背景颜色透明度来实现,(不建议使用opacity属性来实现背景颜色的透明度,因为如果给父元素设置了透明度,之后的子元素的幽冥毒也会被影响),使用rbga(0,0,0,0)函数来定义颜色的结点。

        rbga()函数的第四个参数可以是从 0 到 1 的值,它定义了颜色的透明度:0 表示完全透明,1 表示完全不透明。rgba()后面的百分比表示位置。

         linear-gradient 是一种实现线性渐变的属性,功能是控制渐变,特点是线性的进行控制。

        书写方式:

(1)Safari、Chrome

                background: -webkit-linear-gradient(top, rgba(72, 61, 139, 0)0%,rgba(72, 61, 139, 0.5)33%,rgba(72, 61, 139, 0.5)66%,rgba(72, 61, 139, 0)98%);

(2)Opera

                background: -o-linear-gradient(bottom, rgba(72, 61, 139, 0)0%,rgba(72, 61, 139, 0.5)33%,rgba(72, 61, 139, 0.5)66%,rgba(72, 61, 139, 0)98%);

(3)Firefox

                background: -moz-linear-gradient(bottom,rgba(72, 61, 139, 0)0%,rgba(72, 61, 139, 0.5)33%,rgba(72, 61, 139, 0.5)66%,rgba(72, 61, 139, 0)98%);

(4)标准的语法

                background: linear-gradient(to left, rgba(72, 61, 139, 0)0%,rgba(72, 61, 139, 0.5)33%,rgba(72, 61, 139, 0.5)66%,rgba(72, 61, 139, 0)98%);
       

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>渐变</title>
    <style>
        .back{
            width: 800px;
            height: 100px;
            margin: 0 auto;
            display: flex;
            background: linear-gradient(to left, rgba(72, 61, 139, 0)0%,rgba(72, 61, 139, 0.5)33%,rgba(72, 61, 139, 0.5)66%,rgba(72, 61, 139, 0)98%);
         
        }
        
        .box{
            width: 450px;
            margin: 10px auto;
            height: 80px;
            display: flex;
            justify-content: space-around;
        }
        .box div{
            width: 120px;
            height: 80px;
        }
        .box div img{
            height: 100%;
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="back">
        <div class="box">
            <div>
                <img src="./images/平衡补丁.jpg" alt="">
            </div>
            <div>
                <img src="./images/平衡补丁.jpg" alt="">
            </div>
            <div>
                <img src="./images/平衡补丁.jpg" alt="">
            </div>
        </div>
        
    </div>
</body>
</html>

 2.边框的渐变

        这个方法是用padding内边距来实现边框的渐变色,渐变的原理和设置背景

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul li{
            list-style: none;/*取消列表的样式*/
        }
        .res{
            width:300px;
            height: 100px;
            margin: 0 auto;
        }
        .res ul{
            display: flex;
            justify-content:space-around;
        }
        .res li{
            width: 100px;
            padding: 4px;
            height: 50px;
            background: linear-gradient(#bcaecf,#477fce);
            cursor:pointer;
            border-radius: 20%;
        }
        .res a{
            height: 100%;
            display: block;
            border-radius: 20%;
            background-color:rgb(173, 109, 233);
            line-height: 50px;
            font-size: 20px;
            color: #fff;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="res">
        <ul>
         
          <li><a>登录</a></li>
          <li><a>注册</a></li>
        </ul>
      </div>
</body>
</html>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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
发出的红包

打赏作者

卡夫卡的小熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值