css3基础
1、什么是css3?
-
是css技术的升级版本,在css2的基础上,新增了许多特性,弥补了css2的不足,使web页面的开发变得更加高效、便捷
-
css3按模块化进行设计,比较重要的css3模块包含、 选择器、框模型、背景和边框、文本效果、2D|3D转换、动画、多列布局、用户界面
-
css3优点: CSS3是完全向后兼容,所以没有必要修改现在的设计来让它们继续运作。网络浏览器也还将继续支持CSS2。
2、浏览器的私有前缀(兼容性前缀)
-
有些css3的属性还只是最新版的预览版,还没有正式发版, 没有形成标准,只是某些浏览器的专属属性 ,就需要加前缀
-
当一个属性或规则成为标准,并被主流浏览器的最新版普遍兼容的时候,可以去掉私有前缀
-
书写顺序:先写私有css属性,然后再写标准css属性
-
为了兼容老旧版本的浏 览器我们要写以下代码:
-
浏览器、内核、私有前缀
浏览器 内核 私有前缀 chrome、safari webkit -webkit- Opera Presto -o- Firefox Gecko -moz- IE Trident -ms- -
Autoprefixer是一款自动管理浏览器前缀的插件,它可以解析CSS文件并且添加浏览器前缀到CSS内容里,使用Can I Use(caniuse网站)的数据来决定哪些前缀是需要的。
-
autoprefixer
-
在vscode中安装autoprefixer插件
-
在外部的css文件中书写相关属性和属性值
-
按下F1,选择autoprefixer:Run,之后会在css文件中添加私有前缀
-
注意:若快捷键冲突,点击fn+f1
input::-webkit-input-placeholder { color: tomato; } input::-moz-placeholder { color: tomato; } input:-ms-input-placeholder { color: tomato; } input::-ms-input-placeholder { color: tomato; } input::placeholder { color: tomato; } .box1 { width: 100px; height: 100px; background-color: tomato; -webkit-border-radius: 50%; -moz-border-radius: 50%; -o-border-radius: 50%; -ms-border-radius: 50%; border-radius: 50%; } -
3、新增选择器
1)属性选择器
-
css2属性选择器
-
ele[attr]{ } 指定了属性名为attr的ele元素
-
ele[attr=value]{ } 指定了属性名为attr且属性值为value的ele元素
-
ele[attr~=value]{ } 指定了属性名称attr,并且属性值词列表中包含value的ele元素
-
注意:要有词列表的时候,属性名=属性值是选择不上的
/*带有class属性的div元素*/ .wrap div[class]{ background: red; } /*带有class属性,并且属性值为box的div元素*/ .wrap div[class=box]{ background: pink; } /* 带有class属性,并且属性值的词列表中包含box的div元素 */ .wrap div[class~=box]{ background: yellow; } /*带有type属性,并且属性值为text的input元素*/ .wrap input[type=text] { border: 10px solid red; } /*带有type属性,并且属性值为password的input元素*/ .wrap input[type=password] { border: 10px dotted blue; } 注意: /*要有词列表的时候,属性名=属性值是选择不上的,用~选*/ /* 选不上 */ .warp div[class=box2] { background-color: burlywood; } .warp div[class~=box2] { background-color: burlywood; } <div class="box1 box2">box2</div> <div class="box3 box4">box4</div> -
-
css3新增属性选择器
-
ele[attr^=value]{ } 指定了属性名attr,且属性值为value开头的ele元素
-
ele[attr$=value]{ } 指定了属性名attr,且属性值为value结尾的ele元素
-
ele[attr*=value]{ } 指定了属性名attr,且属性值包含value的ele元素
-
-
注意哦:不能以数字结尾和开头
div { width: 100px; height: 100px; } /*带有class属性类名以box开头的div元素*/ .warp div[class^=box] { background-color: tomato; } /*带有class属性类名以b结尾的div元素*/ .warp div[class$=b] { background-color: yellow; } /*带有id属性id名为box3的div元素*/ .warp div[id=box3] { background-color: violet; } /*带有class属性,并且属性值有b的div元素*/ .warp div[class*=b] { font-size: 30px; } /*带有id属性,并且属性值有b的div元素*/ .warp div[id*=b] { font-size: 40px; }
2)结构伪类选择器
-
:root{ } 匹配文档根元素
-
ele:first-child{ } 选择一组相同元素中的第一个元素
-
ele:last-child{ } 选择一组相同元素中的最后一个元素
-
ele:nth-child(n){ } 选择一组相同元素中的第n个元素,n可以是数值、关键词、表达式
-
数值
-
关键词:odd(奇数)|even(偶数)
-
表达式:2n(偶数)|2n+1(奇数)
-
-
ele:nth-last-child(n){ } 选择一组相同元素中的倒数第n个元素,n表示数值、关键词、表达式
备注: 以上元素类型必须一样
-
ele:first-of-type 选择一组元素中特定类型的第一个子元素
-
ele:last-of-type 选择一组元素中特定类型的最后一个子元素
-
ele:nth-of-type(n){ } 选择一组元素中特定类型的第n个ele元素
-
ele:nth-last-of-type(n){ } 选择一组元素中特定类型的倒数第n个ele元素
备注 : 以上元素类型可以不一样
child一组与of-type一组的区别
-
nth-child强调结构 关系,查找子元素中的第几个
-
nth-of-type强调类型,查找类型中的第几个
**备注: 以上元素必须类型一样**
/* 根元素 */
:root {
background-color: azure;
}
.wrap div {
width: 100px;
height: 100px;
}
/* **备注: 以上元素必须类型一样** */
/* 第一个元素 */
.wrap div:first-child {
background-color: tomato;
}
/* 最后一个元素 */
.wrap div:last-child {
background-color: purple;
}
/* div第2个元素 */
.wrap div:nth-child(2) {
background-color: pink;
}
/* ele:nth-last-child(n) {}
选择一组相同元素中的倒数第n个元素,n表示数值、关键词、表达式 */
/* 倒数第2个div元素 */
.wrap div:nth-last-child(2) {
background-color: antiquewhite;
}
<div class="wrap">
<div>第一 1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5倒数第2</div>
<div>最后一个6</div>
</div>
- ele:nth-of-type(n){ } 选择一组元素中特定类型的第n个ele元素
- ele:nth-last-of-type(n){ } 选择一组元素中特定类型的倒数第n个ele元素
/* ele:first-of-type 选择一组元素中特定类型的第一个子元素的每个元素 */
.wrap span:first-of-type {
font-size: 40px;
}
.wrap p:first-of-type {
background-color: yellowgreen;
}
.wrap h3:first-of-type {
background-color: tomato;
}
/* - ele:last-of-type 选择一组元素中特定类型的最后一个子元素的每个元素 */
.wrap span:last-of-type {
font-size: 30px;
color: tomato;
}
.wrap p:last-of-type {
background-color: purple;
}
/* - ele:nth-of-type(n){ } 选择一组元素中特定类型的第n个ele元素 */
.wrap p:nth-of-type(2) {
color: blue;
font-size: 30px;
}
.wrap span:nth-of-type(2) {
color: blue;
font-size: 30px;
}
/* - ele:nth-last-of-type(n){ } 选择一组元素中特定类型的倒数第n个ele元素 */
.wrap p:nth-last-of-type(3) {
color: tomato;
font-size: 40px;
}
<div class="wrap">
<span>0span</span>
<p>p1</p>
<p>p2</p>
<h3>h3</h3>
<p>p3</p>
<p>p4</p>
<p>p5</p>
<span>6span</span>
<span>7span</span>
</div>
3)状态伪类选择器
-
ele:disabled{ } 选择界面上处于禁用状态的元素
-
ele:enabled{ } 选择界面上处于可用状态的元素
-
ele:checked{ } 选择界面上处于被选中状态的元素
/* ele:enabled{ } 选择界面上处于可用状态的元素 */ input:enabled { background-color: tomato; } input[type=password]:enabled { background-color: yellowgreen; } /* ele:disabled{ } 选择界面上处于禁用状态的元素 */ input:disabled { background-color: purple; } /* ele:checked{ } 选择界面上处于被选中状态的元素 */ input:checked { width: 50px; height: 50px; opacity: .5; } input[type=radio]:checked { width: 30px; height: 30px; opacity: .5; } <ol> <li> 账号:<input type="text"> </li> <li> 密码:<input type="password"> </li> <li> 账号1:<input type="text" disabled> </li> <li> 账号2:<input type="text" disabled> </li> <li> 性别:<input type="radio" checked>男 <input type="radio">女 </li> <li> 性别:<input type="checkbox">吃饭 <input type="checkbox" checked>睡觉 <input type="checkbox" checked>打豆豆 </li> </ol>
4)修改表单元素的placeholder的样式
/*修改placeholder默认样式*/
input::placeholder {
color: tomato;
}
/*修改获取焦点后placeholder的样式*/
input:focus::placeholder {
color: yellowgreen;
}
4、背景属性
1)多背景
background-image: url(图片路径), url(图片路径);
-
不同的背景图像【逗号】隔开,也可以给不同的图片设置多个不同的属性(如背景位置,背景重复等)
-
书写顺序越靠前,显示越靠上
-
单独定义
.box { background-image: url(img_flwr.gif), url(paper.gif); background-position: right bottom, left top; background-repeat: no-repeat, repeat; }-
简写
.box { background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat; } -
2)背景图尺寸设置
-
background-size: 数值
-
px:设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个 值会被设置为 “auto”。
-
percentage:以父元素的百分比来设置背景图像的宽度和高度。第一个值设置宽度,第二个值设置高度。如 果只设置一个值,则第二个值会被设置为 “auto”。
-
cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点)
-
contain:包含,等比例缩放背景图片到完整显示在盒子中,有一边到达边界就停止放大, 可能导致另一边留白 但是背景图可能在区域范围内铺不满(宽高给大点)
-
.wrap div {
width: 200px;
height: 200px;
border: 1px solid red;
margin: 5px;
}
/* 显示越靠前的背景,书写顺序越靠前 */
.wrap .box {
background-image: url(./img/bg1.jpg), url(./img/yd.jpg);
}
.wrap .box1 {
background: url(./img/bg1.jpg), url(./img/7.jpg);
}
.wrap .box2 {
background: url(./img/bg1.jpg) no-repeat, url(./img/7.jpg) no-repeat;
}
/* background-size: 数值 */
/* px */
.wrap .box3 {
background: url(./img/yd.jpg) no-repeat;
background-size: 100px 100px;
background-size: 100px;
}
/* 百分比 */
.wrap .box4 {
background: url(./img/7.jpg) no-repeat;
background-size: 50% 50%;
background-size: 50%;
background-size: 20%;
}
/* cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点) */
.wrap .box5 {
width: 900px;
height: 400px;
background: url(./img/7.jpg) no-repeat;
background-size: cover;
}
/* contain:包含,等比例缩放背景图片到完整显示在盒子中,但是背景图可能在区域范围内铺不满(宽高给大点) */
.wrap .box6 {
width: 600px;
height: 400px;
background: url(./img/7.jpg) no-repeat;
background-size: contain;
}
3)背景图定位区域——background-origin属性
-
background-origin: padding-box; 默认值,背景图相对于内填充区域来定位
-
background-origin: content-box; 背景图相对于内容来定位
-
background-origin: border-box; 背景图片相对于边框区域来定位
.wrap div {
width: 200px;
height: 200px;
border: 20px dotted red;
margin: 50px;
padding: 50px;
}
/* 默认值,背景图相对于内填充区域来定位 */
.wrap .box1 {
background: url(./img/bg2.jpg) no-repeat;
background-origin: padding-box;
}
/* 背景图相对于内容来定位 */
.wrap .box2 {
background: url(./img/bg2.jpg) no-repeat;
background-origin: content-box;
}
/* 背景图片相对于边框区域来定位 */
.wrap .box3 {
background: url(./img/bg2.jpg) no-repeat;
background-origin: border-box;
}
4)背景颜色绘制区域——background-clip属性
-
background-clip: border-box; 背景被裁剪到边框区域,在内容区、内填充区、边框区域显示,默认值
-
background-clip: padding-box; 背景被裁剪到内填充区域,在内容区、内填充区显示
-
background-clip: content-box; 背景被裁剪到内容区域,仅在内容区域显示
.wrap div {
width: 200px;
height: 200px;
background: plum;
padding: 30px;
margin: 30px;
border: 20px dotted black;
}
/* 背景被裁剪到内容区域,仅在内容区域显示 */
.wrap div:nth-child(1) {
/* content-box */
background-clip: content-box;
}
/* 背景被裁剪到内填充区域,在内容区、内填充区显示 */
.wrap div:nth-child(2) {
/* padding-box */
background-clip: padding-box;
}
/* 背景被裁剪到边框区域,在内容区、内填充区、边框区域显示,默认值 */
.wrap div:nth-child(3) {
/* border-box */
background-clip: border-box;
}
5、渐变
从一种颜色到其他颜色的过渡(两种及两种以上颜色)
1)线性渐变 linear-gradient
线型渐变 : 从一个方向到另一个方向的渐变
-
语法
background: linear-gradient(方向, 颜色1 范围1, 颜色2 范围2,...); 方向:数值(单位deg)、关键词(left|right top|bottom) 颜色:关键词、十六进制色值、rgb(r,g,b)、rgba(r,g,b,a) 范围:每个颜色结点的显示范围 取值: 1.px 2.百分比 注意: 方向:加前缀需要把to去掉,方向是相反的 默认方向:从上到下 取值: 1、使用起始位置关键字 - to right 方向自左向右 - to top 方向自下而上 - to bottom 方向自上而下 - to left 方向自右而左 - to right top 方向朝向右上角 - to right bottom 方向朝向右下角 - to top left 方向朝向左上角 - to left bottom方向朝向左下角 2、使用角度 - 0deg 相当于 to top - 90deg 相当于 to right
-
重复线性渐变
background:repeating-linear-gradient(方向, 颜色1 范围1, 颜色2 范围2,...);
background: repeating-linear-gradient(180deg, red 0%, red 10%, yellow 10%,yellow 20%);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
width: 300px;
height: 300px;
margin: 20px;
}
/* 方向朝向左上角,red--blue渐变 */
.box1 {
background: linear-gradient(to top left, red, yellow, green);
}
/* 线性渐变:
从45度方向开始:
0%-20%:纯黄色
20%-60%:黄色和粉色渐变
60%-100%:纯粉色 */
.box2 {
background: -webkit-linear-gradient(45deg, yellow 20%, pink 60%);
background: -o-linear-gradient(45deg, yellow 20%, pink 60%);
background: -moz-linear-gradient(45deg, yellow 20%, pink 60%);
background: -ms-linear-gradient(45deg, yellow 20%, pink 60%);
background: linear-gradient(45deg, yellow 20%, pink 60%);
}
/* 范围是百分比 */
.box3 {
background: linear-gradient(90deg, yellow 20%, pink 60%);
}
/* 范围是px */
.box7 {
background: linear-gradient(90deg, yellow 20px, pink 60px);
}
/* 方向:加前缀需要把to去掉,方向是相反的 */
.box4 {
background: -webkit-linear-gradient(right, red, yellow, green);
}
.box5 {
background: linear-gradient(to right, red, yellow, green);
}
/* 重复线性渐变 */
/* 兼容里的写法,先写私有前缀,在写标准的 */
.box6 {
background: -webkit-repeating-linear-gradient(top, red 10%, yellow 20%, green 30%);
background: -moz-repeating-linear-gradient(top, red 10%, yellow 20%, green 30%);
background: -o-repeating-linear-gradient(top, red 10%, yellow 20%, green 30%);
background: -ms-repeating-linear-gradient(top, red 10%, yellow 20%, green 30%);
/* 不加前缀记得加to */
background: repeating-linear-gradient(to top, red 10%, yellow 20%, green 30%);
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
<div class="box6"></div>
<div class="box7"></div>
</body>
</html>
2)径向渐变 radial-gradient
径向渐变 : 一个点到四周的渐变
-
语法
backgrond: radial-gradient(渐变形状, 颜色1 范围1, 颜色2 范围2, ...); 渐变形状:椭圆(ellipse,默认值)、圆形(circle) 圆心位置 语法:background:radial-gradient(形状 at 水平位置 垂直位置,颜色1,颜色2) 如果只给一个值,另一个值(y垂直方向)默认center 取值: - px(表示距左上角的0,0的坐标位置) - 关键字可以是以下词的组合 - left center right - top center bottom - 百分比 例:表示圆心在右侧中心 background: radial-gradient(circle at 100% 50%, red, yellow, green); ``` 例:表示圆心在左上角 background: radial-gradient(circle at left top, red, yellow, green);
-
重复径向渐变
backgrond: repeating-radial-gradient(渐变形状/圆心, 颜色1 范围1, 颜色2 范围2, ...);
div {
width: 400px;
height: 300px;
margin: 20px;
}
/* 椭圆(ellipse,默认值) */
.box1 {
background: -webkit-radial-gradient(ellipse, red, yellow, green);
background: -moz-radial-gradient(ellipse, red, yellow, green);
background: -o-radial-gradient(ellipse, red, yellow, green);
background: -ms-radial-gradient(ellipse, red, yellow, green);
background: radial-gradient(ellipse, red, yellow, green);
}
/* 圆形(circle) */
.box2 {
background: -webkit-radial-gradient(circle, red, yellow, green);
background: -moz-radial-gradient(circle, red, yellow, green);
background: -o-radial-gradient(circle, red, yellow, green);
background: -ms-radial-gradient(circle, red, yellow, green);
background: radial-gradient(circle, red, yellow, green);
}
/* 圆形(circle) 颜色,范围 */
.box7 {
background: radial-gradient(circle, red 10%, yellow 20%, green 30%);
}
/* 圆心位置 px(表示距左上角的0,0的坐标位置)*/
.box3 {
background: -webkit-radial-gradient(circle at 50px 100px, red, yellow, green);
background: -moz-radial-gradient(circle at 50px 100px, red, yellow, green);
background: -o-radial-gradient(circle at 50px 100px, red, yellow, green);
background: -ms-radial-gradient(circle at 50px 100px, red, yellow, green);
background: radial-gradient(circle at 50px 100px, red, yellow, green);
}
/* 百分比 表示圆心在右侧中心 */
.box3 {
background: -webkit-radial-gradient(circle at 100% 50%, red, yellow, green);
background: -moz-radial-gradient(circle at 100% 50%, red, yellow, green);
background: -o-radial-gradient(circle at 100% 50%, red, yellow, green);
background: -ms-radial-gradient(circle at 100% 50%, red, yellow, green);
background: radial-gradient(circle at 100% 50%, red, yellow, green);
}
/* 关键词 表示圆心在左上角 */
.box4 {
background: -webkit-radial-gradient(circle at left top, red, yellow, green);
background: -moz-radial-gradient(circle at left top, red, yellow, green);
background: -o-radial-gradient(circle at left top, red, yellow, green);
background: -ms-radial-gradient(circle at left top, red, yellow, green);
background: radial-gradient(circle at left top, red, yellow, green);
}
/* 重复径向渐变 */
div {
width: 500px;
height: 200px;
border: 1px solid tomato;
margin: 30px;
}
.box5 {
background: -webkit-repeating-radial-gradient(circle, red 10%, yellow 20%, green 30%);
background: -moz-repeating-radial-gradient(circle, red 10%, yellow 20%, green 30%);
background: -o-repeating-radial-gradient(circle, red 10%, yellow 20%, green 30%);
background: -ms-repeating-radial-gradient(circle, red 10%, yellow 20%, green 30%);
background: repeating-radial-gradient(circle, red 10%, yellow 20%, green 30%);
}
/* 重复径向渐变 */
.box6 {
background: -webkit-repeating-radial-gradient(circle at 50px 50px, red 10%, yellow 20%, green 30%);
background: -moz-repeating-radial-gradient(circle at 50px 50px, red 10%, yellow 20%, green 30%);
background: -o-repeating-radial-gradient(circle at 50px 50px, red 10%, yellow 20%, green 30%);
background: -ms-repeating-radial-gradient(circle at 50px 50px, red 10%, yellow 20%, green 30%);
background: repeating-radial-gradient(circle at 50px 50px, red 10%, yellow 20%, green 30%);
}
div:nth-child(4) {
background: repeating-radial-gradient(ellipse at right center, red 10%, orange 20%, yellow 30%, green 40%, aqua 50%, blue 60%, purple 70%);
}
div:nth-child(5) {
background: repeating-radial-gradient(ellipse at left center, red 10%, orange 20%, yellow 30%, green 40%, aqua 50%, blue 60%, purple 70%);
}
6、框模型
box-sizing属性
-
box-sizing: content-box;
-
默认值,【标准盒模型】
-
在width和height属性之外绘制元素的内填充和边框
-
-
box-sizing: border-box;
-
【怪异盒模型】
-
通过已有的设置好的width属性和height属性中分别减去内填充和边框宽度,得到内容宽和高
-
7、多列布局
1、元素被分隔的列数——column-count属性
-
column-count: n; 元素内容被划分的列数,没有单位
-
column-count: auto; 由其他属性决定列数
2、列的宽度——column-width属性
-
column-width: npx; 分列之后每一列的宽度
-
column-width: auto; 由其他属性决定列宽
3、列间距——column-gap属性
-
column-gap: npx; 两列之间的间隔为npx
-
column-gap: normal; 两列之间间隔为常规间隔,W3C建议值为1em
4、列与列之间的分割线——column-rule属性
-
语法
column-rule: column-rule-width column-rule-style column-rule-color; column-rule-width属性:分割线宽度 column-rule-style属性:分割线线型(solid double dotted dashed) column-rule-color属性:关键词、十六进制色值、rgb(r,g,b)、rgba(r,g,b,a)
.box {
width: 800px;
border: 2px solid red;
margin: 50px auto;
/* 元素被分隔的列数 */
/* column-count: 3; */
/* 列的宽度 会自动计算列数*/
column-width: 30px;
/* 列间距 */
column-gap: 20px;
/* 列与列之间的分割线 */
column-rule: 20px dotted orange;
}
1721

被折叠的 条评论
为什么被折叠?



