线性渐变(linear gradients)沿着一根轴线改变颜色,从起点到终点颜色进行顺序渐变。

语法:

background:linear-gradient(direction,color-stop1,color-stop2,……);

1、线性渐变从上到下(默认)

语法

background:linear-gradient(color-stop1,color-stop2……);

实例

div {
width: 800px;
height: 500px;
margin: 0 auto;
background: -webkit-linear-gradient(red, blue);
/* Safari 5.1 - 6.0*/
background: -o-linear-gradient(red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(red, blue);
/* 标准的语法 */
}

  

2、线性渐变从左到右

语法:

background: -webkit-linear-gradient(begin-level begin-vertical,color-stop1,color-stop2,……);

/* Safari 5.1 - 6.0 */

background: -o-linear-gradient(end-level end-vertical,color-stop1,color-stop2,……);

/* Opera 11.1 - 12.0 */

background: -moz-linear-gradient(end-level end-vertical,color-stop1,color-stop2,……);

/* Firefox 3.6 - 15 */

background: linear-gradient(to end-level end-vertical,color-stop1,color-stop2,……);

/*标准的语法*/

实例

div {
width: 800px;
height: 500px;
margin: 0 auto;
background: -webkit-linear-gradient(left, red, blue);
/* Safari 5.1 - 6.0 */
background: -o-linear-gradient(right, red, blue);
/* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(right, red, blue);
/* Firefox 3.6 - 15 */
background: linear-gradient(to right, red, blue);
/* 标准的语法 */
}

  

3、线性渐变对角

语法

background: -webkit-linear-gradient(begin-level begin-vertical,color-stop1,color-stop2,……);

/* Safari 5.1 - 6.0 */

background: -o-linear-gradient(end-level end-vertical,color-stop1,color-stop2,……);

/* Opera 11.1 - 12.0 */

background: -moz-linear-gradient(end-level end-vertical,color-stop1,color-stop2,……);

/* Firefox 3.6 - 15 */

background: linear-gradient(to end-level end-vertical,color-stop1,color-stop2,……);

/*标准的语法*/

小提示:线性渐变对角时关键字的顺序无关紧要,“to left bottom”与“to bottom left”相同。

实例

div {
width: 800px;
height: 500px;
margin: 0 auto;
background: -webkit-linear-gradient(left top, red , blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(bottom right, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(bottom right, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(to bottom right, red , blue); /* 标准的语法 */
}

  

3-3编程练习

小伙伴们,我们学习了怎样使用CSS3写颜色的渐变效果,那么根据效果图,写出代码,实现3中方式的渐变把!

效果图如下:

任务

第一步:写出html代码(3个div元素)

第二部:给div元素设置CSS样式(宽和高。三个元素在一行上)

第三部:给三个div元素设置相应的背景颜色

(1)第一个div元素是从上向下发生渐变,暗色由黄色变成红色

(2)第二个div元素是从左到右发生渐变,颜色由黄色变为红色

第三个div元素是从左上角到右下角发生渐变,颜色由红色变为黄色

参考代码:

<!DOCTYPE html>
<html lang="zh-CN"> <head>
<meta charset="UTF-8">
<title>background-clip</title>
<meta name='description' content='background-clip'>
<meta name='keywords' content='background-clip'>
<style>
/* 此处写代码 */
html,
body {
margin: 0;
padding: 0; } .container {
width: 1000px;
margin: 20px auto;
display: flex;
justify-content: space-between;
}
.font{
display:flex;
justify-content:center;
width: 200px; } div.item{
width: 200px;
height: 200px;
background: #ccc;
} div.item:nth-child(1) {
background: linear-gradient(yellow, red); } div.item:nth-child(2) {
background: linear-gradient(to right,yellow, red); }
div.item:nth-child(3) {
background: linear-gradient(to bottom right, red,yellow); }
</style> </head> <body>
<!-- 此处写代码 --> <div class='container'>
<div class='font'>上下方向渐变</div>
<div class='font'>左右方向渐变</div>
<div class='font'>对角线方向渐变</div>
</div>
<div class='container'>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
</div>
</body> </html>

  

4、线性渐变-使用角度

语法:

background:linear-gradient(angle,color-stop1,color-stop2,……);

角度说明

0deg创建一个从下到上的渐变,正角表示顺时针旋转,90deg创建一个从左到右的渐变。

实例

#grad {
background: -webkit-linear-gradient(180deg, red, blue); /* Safari 5.1 - 6.0 */
background: -o-linear-gradient(180deg, red, blue); /* Opera 11.1 - 12.0 */
background: -moz-linear-gradient(180deg, red, blue); /* Firefox 3.6 - 15 */
background: linear-gradient(180deg, red, blue); /* 标准的语法 */
}

  

编程练习

小伙伴们,根据效果图,补充代码,实现元素的背景颜色在水平方向上发生渐变,其中:

(1)0%-20%处,颜色是红色全透明

(2)20%-50%处,颜色是从白色渐变到红色

(3)50%-90%处,颜色从红色渐变到绿色,其中绿色的透明度是0.7

效果图如下:

任务

给元素设置背景颜色渐变

(1)用角度表示法让颜色在水平方向发生渐变

(2)水平方向的0%-20%处,渐变的颜色是红色全透明

(3)水平方向的20%-50%处,渐变的颜色是红色,颜色没有透明度

(4)水平方向50%-90%处,渐变的颜色是绿色,透明度为0.7

参考代码:

<!DOCTYPE html>
<html lang="zh-CN"> <head>
<meta charset="UTF-8">
<title>em</title>
<meta name='description' content='em'>
<meta name='keywords' content='em'>
<style>
/* 此处写代码 */
div {
width: 400px;
height: 400px;
border:1px solid red;
background:linear-gradient(90deg,rgba(255, 0, 0,0) 20%,rgba(255, 0, 0,1) 50%,rgba(0,255,0,0.7) 90%);
margin:0 auto;
}
</style> </head> <body>
<!-- 此处写代码 -->
<div></div> </body> </html>

  

5、线性渐变-重复渐变

语法:

background:repeating-linear-gradient(color1 length|percentage,color2 length|percentage,……);

实例

div {

background:repeating-linear-gradient(red,blue 20%);

}

3-9编程练习

小伙伴们,我们学习了CSS3现象渐变中的重复渐变,接下来,根据效果图补充代码,实现元素中多个彩虹的重复。

提示:彩虹的颜色,用英语单词表达即可

效果图如下

任务

给元素设置背景颜色重复渐变

(1)效果图中的完整彩虹重复了最少3次,占据了元素的90%的位置,所以一个彩虹占据元素的比例是30%

(2)将一个彩虹的7个颜色,均摊到30%中,每个颜色的比重是5%,比如红色占据的位置是0%,橙色占据的是5%,黄色占据的是10%,以此类推。

参考代码:

<!DOCTYPE html>
<html lang="zh-CN"> <head>
<meta charset="UTF-8">
<title>em</title>
<meta name='description' content='em'>
<meta name='keywords' content='em'>
<style>
/* 此处写代码 */
div {
width: 400px;
height: 300px;
border:1px solid red;
background:repeating-linear-gradient(90deg,red 0%,orange 5%,yellow 10%,green 15%,blue 20%,indigo 25%,purple 30%);
margin:0 auto;
}
</style> </head> <body>
<!-- 此处写代码 -->
<div></div> </body> </html>

  

CSS3之线性渐变(linear gradients)的更多相关文章

  1. CSS3 linear-gradient线性渐变实现虚线等简单实用图形

    一.作为图片存在的CSS3 gradient渐变 我觉得CSS3 Backgrounds比较厉害的一个地方就是支持多背景,也就是背景图片个数可以无限累加,正好CSS3的gradient渐变性质是bac ...

  2. CSS3 Gradient线性渐变

    废话小说,看代码 <!DOCTYPE html > <html > <head> <meta charset="utf-8"> &l ...

  3. CSS3的线性渐变(linear-gradient)

    CSS3渐变(gradient)可分为线性渐变(linear-gradient)和径向渐变(radial-gradient).今天给大家说一说线性渐变. 以webkit内核浏览器为例, 语法: div ...

  4. CSS3———linear-gradient() 线性渐变

    线性渐变linear-gradient() 遇到了这样的css样式 body { height: 100%; background-color: #ffffff; background-image: ...

  5. 【CSS3】 线性渐变

    参考地址:http://www.w3cplus.com/css3/new-css3-linear-gradient.html background-image: linear-gradient(to ...

  6. CSS3渐变(Gradients)-线性渐变

    CSS3渐变(Gradients)可以让你在两个或多个指定颜色之间显示平稳的过度,包括透明度. 以前,你必须使用图像来实现这些效果.但是,通过Css3渐变(Gradients),你可以减少下载的事件和 ...

  7. CSS3背景渐变属性 linear-gradient(线性渐变)和radial-gradient(径向渐变)

    CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变). 为了更好的应用CSS3 Gradient,我们需要先了解一下目前的几种现代浏览器 ...

  8. 再说CSS3渐变——线性渐变

    渐变背景一直以来在Web页面中都是一种常见的视觉元素.但一直以来,Web设计师都是通过图形软件设计这些渐变效果,然后以图片形式或者背景图片的形式运用到页面中.Web页面上实现的效果,仅从页面的视觉效果 ...

  9. CSS3线性渐变linear-gradient

    转自 http://www.w3cplus.com/content/css3-gradient CSS3的线性渐变 一.线性渐变在Mozilla下的应用 -moz-linear-gradient( [ ...

随机推荐

  1. ansible-playbook流程控制-when条件判断

    1. ansible-playbook添加判断     when相当于shell脚本里的if 判断,when语句就是用来实现这个功能的,它是一个jinja2的语法,但是不需要双大括号,用法很简单  1 ...

  2. V4L2摄像头应用编程(转)

    Video for Linuxtwo(Video4Linux2)简称V4L2,是V4L的改进版.V4L2是linux操作系统下用于采集图片.视频和音频数据的API接口,配合适当的视频采集设备和相应的驱 ...

  3. 多测师_测试理轮_002(v模型和H模型)

    为什么要测试?1.软件非正常运行或自身缺陷会引发问题2.代码和文档是人写的,难免会出错3.环境原因影响软件(内存不足,存储,数据库溢出等)4.软件测试活动是保证软件质量的关键之一 什么是测试?软件行业 ...

  4. java读取中文乱码解决方法

    Java读取文本文件(例如csv文件.txt文件等),遇到中文就变成乱码.读取代码如下: List<String> lines=new ArrayList<String>(); ...

  5. 数据库SQL Server 2016“功能选择”详细说明及精简安装选择

    前言 在平时大家安装数据库的时候,一般默认功能选择都会选择全选.但是前两天公司同事问我:"那么多功能为什么都能用到嘛?"顿时,我思考了一下确实没有详细了解每个功能的详细作用,于是花 ...

  6. ▶ 0001 No application 'E:\www\go\blog' found in your GOPATH

    go mod 配置 beego 首先cmd bee new blog go mod init 然后复制到任意目录 bee run 就会报错, 要退出该目录,进入上级目录 bee run blog 才行

  7. FrameworkElementFactory中的SetBinding与SetValue

    public static Microsoft.Windows.Controls.DataGridColumn CreateDateColumn(string path, string header) ...

  8. Unity-根据时间开灯与关灯

    声明:本人只是学生,并且只是自学Unity,如有大神,不喜勿喷,不足之处,请指出! 本项目使用了UniStorm 3.0(天气)插件,时间也是调用它本身的API,实际并不影响,用系统的时间的是也是可以 ...

  9. Disconnected from the target VM, address: '127.0.0.1:1135', transport: 'socket'-SpringBoot启动报错

    一.问题由来 本地代码在一次打包后,再次启动项目时报了一个错误,详细的错误信息如下: 2020-10-23 15:10:26.724 [] [main] INFO o.s.c.a.Annotation ...

  10. 论文解读《Learning Deep CNN Denoiser Prior for Image Restoration》

    CVPR2017的一篇论文 Learning Deep CNN Denoiser Prior for Image Restoration: 一般的,image restoration(IR)任务旨在从 ...