css3新特性第四章(渐变)

渐变

  • 线性渐变

  • 径向渐变

  • 重复渐变

  • 使用:

    background-image: xx 渐变

    background-image: linear-gradient(red,yellow,green);

  • 公共代码

    markdown 复制代码
     .box {
           width: 300px;
           height: 200px;
           border: 1px solid black;
           float: left;
           margin-left: 30px;
           margin-top: 30px;
           text-align: center;
           line-height: 200px;
           font-size: 20px;
         }
    <body>
        <div class="box box1">线性渐变-默认方式,从上到下</div>
        <div class="box box2">线性渐变-使用关键字</div>
        <div class="box box3">线性渐变-使用角度</div>
        <div class="box box4">线性渐变-使用像素位置 </div>
        <div class="box box5">线性渐变演示效果</div>
    </body>

2.1 线性渐变

一条线一条线的变化,看起来是同一水平线;

使用:background-image: linear-gradient(颜色1,颜色2,颜色3)

默认多个颜色渐变

background-image: linear-gradient(red,yellow,green);

总结:多个颜色之间的渐变, 默认从上到下渐变

使用关键词设置渐变方向

/* 第二种方式 关键词设置方向 */

.box2 {

/* 从下到上 */

​ /* background-image: linear-gradient(to top,red,yellow,green); */

​ /* 到右上角 那么右上角就是 绿色 */

​ background-image: linear-gradient(to right top,red,yellow,green);

}

使用角度单位设置渐变方向

/* 第三种方式 使用角度 */

.box3 {

background-image: linear-gradient(30deg,red,yellow,green);

}

使用像素位置设置渐变方向

/* 第四种方式 使用位置 */

.box4 {

background-image: linear-gradient(red 50px,yellow 100px,green 150px);

}

小设计综合

.box5 {

​ background-image: linear-gradient(375deg,red 50px,yellow 100px,green 150px);

​ font-size: 40px;

​ color: transparent;

​ font-weight: bold;

​ /* */

​ -webkit-background-clip: text;

}

2.2 径向渐变

多个颜色从圆心散开,最终是一个圆形,从而为径向渐变,有半径

默认多个颜色渐变

  • 默认从圆心四散。(注意:不一定是正圆,要看容器本身宽高比)

/* 第一种方式 默认 从圆心向四周发散 */

.box1 {

​ background-image: radial-gradient(red,yellow,green);

}

关键字设置渐变位置

/* 第二种方式 关键字 从右上角发散 */

.box2 {

​ background-image: radial-gradient(at right top,red,yellow,green);

}

使用像素值调整渐变圆的圆心位置

/* 第三种方式 像素 从x轴开始 */

.box3 {

​ background-image: radial-gradient(red 50px,yellow 100px,green 150px);

}

调整渐变形状为正圆

/* 第四种方式 正圆,本来默认是根据容器的宽高计算,如果是正方形默认就是圆心了 */

.box4 {

​ background-image: radial-gradient(circle,red,yellow,green);

}

圆心半径

/* 圆心半径 */

.box5 {

​ background-image: radial-gradient(150px,red,yellow,green);

}

2.3 重复渐变

无论线性渐变,还是径向渐变,在没有发生渐变的位置,继续进行渐变,就为重复渐变。

  • 使用 repeating-linear-gradient 进行重复线性渐变,具体参数同 linear-gradient 。
  • 使用 repeating-radial-gradient 进行重复径向渐变,具体参数同 radial-gradient 。

我们可以利用渐变,做出很多有意思的效果:例如:横格纸、立体球等等。

重复径向线性渐变

  • 使用方法
    • repeating-linear-gradient:单位
markdown 复制代码
  .box {
       width: 300px;
       height: 200px;
       border: 1px solid black;
       float: left;
       margin-left: 30px;
       margin-top: 30px;
       text-align: center;
       line-height: 200px;
       font-size: 20px;
     }
<body>
    <div class="box box1">重复渐变-线性渐变</div>
  
</body>

/* 第四种方式 使用位置 */

.box1 {

​ background-image: repeating-linear-gradient(red 50px,yellow 100px,green 150px);

}

重复径向渐变

​ .box {

​ width: 300px;

​ height: 200px;

​ border: 1px solid black;

​ float: left;

​ margin-left: 30px;

​ margin-top: 30px;

​ text-align: center;

​ line-height: 200px;

​ font-size: 20px;

​ }

​ /* 第四种方式 使用位置 */

​ .box1 {

​ background-image: repeating-linear-gradient(red 50px,yellow 100px,green 150px);

​ }

​ .box2 {

​ background-image: repeating-radial-gradient(circle,red 50px,yellow 100px,green 150px);

​ }
重复渐变-线性渐变
重复渐变-镜像渐变

应用小案例

  • 代码

    markdown 复制代码
    <style>
         .box {
           width: 900px;
           height: 700px;
           border: 1px solid black;
           margin: 20px auto;
           padding: 15px;
         }
    
         .box1 {
           background-image: repeating-linear-gradient(transparent 0,transparent 34px,gray 35px);
           background-clip: content-box;
         }
    
         .ball {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            position: relative;
         }
    
    
         .ball-1,.ball-2 {
           background-image: repeating-radial-gradient(white 0,#333 100px);
         }
    
         .ball-3,.ball-4 {
           background-image: repeating-radial-gradient(at 80px 80px ,white,#333);
         }
    
    
         .ball-1 {
           position: absolute;
           top: 10px;
           left: 20px;
         }
    
         .ball-2 {
           position: absolute;
           top: 10px;
           right: 20px;
         }
    
         
         .ball-3 {
           position: absolute;
           bottom: 10px;
           left: 20px;
         }
    
         .ball-4 {
           position: absolute;
           bottom: 10px;
           right: 20px;
         }
         
    
      </style>
    
    
    </head>
    <body>
        <div class="box box1"></div>
        <div class="float-ball">
            <div class="ball ball-1"></div>
            <div class="ball ball-2"></div>
            <div class="ball ball-3"></div>
            <div class="ball ball-4"></div>
        </div>
    </body>
相关推荐
IT专家-大狗几秒前
Edge浏览器安卓版流畅度与广告拦截功能评测【不卡还净】
android·前端·edge
Kx…………10 分钟前
Day3:个人中心页面布局前端项目uniapp壁纸实战
前端·学习·uni-app·实战·项目
肠胃炎13 分钟前
认识Vue
前端·javascript·vue.js
七月丶16 分钟前
🛠 用 Node.js 和 commander 快速搭建一个 CLI 工具骨架(gix 实战)
前端·后端·github
砖吐筷筷18 分钟前
我理想的房间是什么样的丨去明日方舟 Only 玩 - 筷筷月报#18
前端·github
七月丶18 分钟前
🔀 打造更智能的 Git 提交合并命令:gix merge 实战
前端·后端·github
iguang20 分钟前
通过实现一个mcp-server来理解mcp
前端
Lafar20 分钟前
OC-runtime使用场景
前端
sunbyte20 分钟前
Three.js + React 实战系列-3D 个人主页 :完成 Navbar 导航栏组件
开发语言·javascript·react.js
三原24 分钟前
实现多选树形组件,我把递归用明白了
前端·数据结构·vue.js