CSS之实现线性渐变背景

1. background: linear-gradient()

background: linear-gradient是CSS中用于创建线性渐变背景的属性,这个属性允许你定义一个在元素的背景中进行渐变的效果,可以从一个颜色过渡到另一个颜色。
基本语法

background: linear-gradient(direction, color-stop1, color-stop2, ...);
  • direction: 渐变的方向,可以是角度(deg)或关键词(to top, to right, to bottom, to left,to left bottom, to left top…),其中角度自下而上方向为0deg,延顺时针方向角度数增加。
  • color-stop: 渐变中的颜色和它们的位置(写法:颜色 位置)。颜色可以是任何有效的颜色值,位置是一个百分比或长度值。

下面我们通过一个简单的例子来学会以上基本语法:
.vue

<template>
    <div>
    </div>
</template>

<style lang="scss" scoped>
* {
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 20%);
}
div {
    width: 600px;
    height: 400px;
    background: linear-gradient(0deg, #f00 100px, #00f calc(100% - 100px));
}
</style>

运行上述代码会得到方向自上而下,颜色在自上下方向上0px到100px红色,100px到300px红色渐变为蓝色,300px到400px为蓝色的包含渐变效果的矩形,如下图:
在这里插入图片描述

参数direction的角度取自下而上为0度,角度参数是按顺时针旋转,如下图:
在这里插入图片描述
我们可以通过浏览器来验证角度变化的效果:
在这里插入图片描述

通过改变上图中的指针指向我们可以得到不同角度的渐变效果,如下图:
90deg
在这里插入图片描述

180deg

在这里插入图片描述

270deg
在这里插入图片描述

2. 实战

1. 菱角矩形

在这里插入图片描述

代码如下:

<template>
    <div>
    </div>
</template>

<style lang="scss" scoped>
* {
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 20%);
}
div {
    width: 300px;
    height: 400px;
    background: linear-gradient(-45deg, 
    transparent 20px, 
    #f0f0f0 20px, 
    #f0f0f0 calc(100% - 20px), 
    transparent calc(100% - 20px),
     transparent 100%);
}
</style>

实现效果如下:
在这里插入图片描述

2. 折角矩形

在这里插入图片描述
代码如下:

<template>
    <div>
    </div>
</template>

<style lang="scss" scoped>
* {
    position: relative;
    top: 50%;
    left: 50%;
    transform: translate(-50%, 20%);
}

div {
    width: 600px;
    height: 400px;
    // 注意点:
    // 1. 层级高的linear-gradient应该写在前面 
    // 2. to left bottom等同于225deg 
    // 3. 背景图的尺寸使用%相对于的是盒子的尺寸,而linear-gradient内的渐变尺寸使用%是相对于背景图的尺寸的
    // 4. linear-gradient内的位置计算是跟角度相关的,如45%,则位置会从左上角开始算起,向右下角移动45%的距离
    //    因此这里的右上角小三角的宽高为 42px 42px(30/sin(45°)算出)
    background: linear-gradient(to left bottom,
    transparent 50%, 
    rgba(0,0,0.6) 50%, 
    rgba(0,0,0.6) 100%) no-repeat 100% 0 / 42px 42px,
    linear-gradient(225deg, 
    transparent 30px, 
    #5186ac 30px, 
    #5186ac 100%) no-repeat 100% 100%,
    ;
}
</style>

实现效果:
在这里插入图片描述
注意点:

  1. 层级高的linear-gradient应该写在前面(如上图的右上角小三角)
  2. to left bottom等同于225deg(常规转换)
  3. 背景图的尺寸使用%时是相对于盒子的尺寸,而linear-gradient内的渐变尺寸使用%是相对于背景图的尺寸
  4. linear-gradient内的位置计算是跟角度相关的,如45%,则位置会从左上角开始算起,向右下角移动45%的距离(因此这里的右上角小三角的宽高为 42px 42px(由30/sin(45°)算出))
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
CSS背景颜色线性渐变可以通过使用CSS3渐变gradients)来实现线性渐变的语法是:background: linear-gradient(direction, color-stop1, color-stop2, ...);其中direction表示渐变的方向,默认为从上到下(to bottom)。color-stop表示颜色的分布位置,默认均匀分布。例如,如果有3个颜色,各个颜色的color-stop可以设置为33.33%。\[1\] 如果想要实现重复的线性渐变,可以使用repeating-linear-gradient的语法:background: repeating-linear-gradient(direction, color-stop1, color-stop2, ...);同样,direction表示渐变的方向。\[2\] 如果想要实现无过渡色渐变,可以使用linear-gradient的语法,并在颜色值后面添加百分比来指定颜色的分布位置。例如,background: linear-gradient(color1 0%, color1 50%, color2 50%, color2 100%);表示0-50%的位置为color1,50-100%的位置为color2。\[3\] #### 引用[.reference_title] - *1* [web前端入门——CSS3背景颜色渐变属性(gradients)](https://blog.csdn.net/qq_39347364/article/details/105034488)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [CSS背景属性之颜色渐变](https://blog.csdn.net/LuoYi_ly_/article/details/109273094)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值