每日知识总结

## CSS3

# 阴影

## 1、盒子阴影

语法

```css

box-shadow: h-shadow v-shadow blur spread color inset;

box-shadow: x轴偏移量 y轴偏移量 模糊值 阴影大小 阴影颜色 内阴影inset|外阴影(outset默认)

h-shadow:阴影的水平偏移量。正数向右偏移,负数向左偏移。 必需 单位: px

v-shadow:阴影的垂直偏移量。正数向下偏移,负数向上偏移。 必需 单位: px

blur:阴影模糊度,不能取负数。 可选 单位: px

spread:阴影大小。正数阴影扩大(阴影大小大于盒子大小),负数阴影缩小(阴影大小小于盒子大小),0阴影与盒子同等大小。 可选 单位: px

inset:表示添加内阴影,默认为外阴影。可选

box-shadow: 0 0 20px 10px green inset;

当 spread 为0时,阴影大小与元素大小相同。

.box1 {

background-color: yellow;

/* 多个阴影 */

/* 盒子阴影 */

box-shadow: 5px 2px 2px 1px pink, 8px 4px 3px 2px tomato;

}

```

> 可以在一个元素上设置一个或多个阴影,阴影之间用逗号隔开

## 2、文字阴影

语法

```html

text-shadow: h-shadow v-shadow blur color;

text-shadow: x轴偏移量 y轴偏移量 模糊值 阴影颜色;

h-shadow 必需,水平阴影的位置,允许负值;

v-shadow 必需,垂直阴影的位置,允许负值;

blur 可选,模糊距离;

color 可选,阴影的颜色;

.box{ text-shadow: 5px 5px 2px #ff0, -5px -5px 2px skyblue;}

```

> 可以在一个文字中设置一个或多个阴影,阴影之间用逗号隔开

### 2、背景

#### 2.1 多背景

```html

background-image: url(图片路径), url(图片路径);

```

- 不同的背景图像【逗号】隔开,也可以给不同的图片设置多个不同的属性(如背景位置,背景重复等)

- 书写顺序越靠前,显示越靠上

- 单独定义

```css

.box {

background-image: url(img_flwr.gif), url(paper.gif);

background-position: right bottom, left top;

background-repeat: no-repeat, repeat;

}

```

- 简写

```css

.box {

background: url(img_flwr.gif) right bottom no-repeat, url(paper.gif) left top repeat;

}

```

#### 2.2 背景图尺寸设置

- background-size: 数值

* px:设置背景图像的高度和宽度。第一个值设置宽度,第二个值设置高度。如果只设置一个值,则第二个 值会被设置为 “auto”。

* percentage:以父元素的百分比来设置背景图像的宽度和高度。第一个值设置宽度,第二个值设置高度。如 果只设置一个值,则第二个值会被设置为 “auto”。

* cover:覆盖,等比例缩放背景图片到铺满整个盒子,但是背景图可能会无法完整显示在盒子中(宽高给大点)

* contain:包含,等比例缩放背景图片到完整显示在盒子中,有一边到达边界就停止放大, 可能导致另一边留白 但是背景图可能在区域范围内铺不满(宽高给大点)

```css

.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;

}

```

#### 2.4 背景图定位区域——background-origin属性

* background-origin: padding-box; 默认值,背景图相对于内填充区域来定位

- background-origin: content-box; 背景图相对于内容来定位

- background-origin: border-box; 背景图片相对于边框区域来定位

```css

.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;

}

```

#### 2.5 背景颜色绘制区域——background-clip属性

* background-clip: border-box; 背景被裁剪到边框区域,在内容区、内填充区、边框区域显示,默认值

* background-clip: padding-box; 背景被裁剪到内填充区域,在内容区、内填充区显示

- background-clip: content-box; 背景被裁剪到内容区域,仅在内容区域显示

```css

.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;

}

```

#### 2.6 渐变

从一种颜色到其他颜色的过渡(两种及两种以上颜色)

##### 2.6.1 线性渐变 linear-gradient

线型渐变 : 从一个方向到另一个方向的渐变

- 语法

```html

background: linear-gradient(方向, 颜色1 范围1, 颜色2 范围2,...);

方向:数值(单位deg)、关键词(left|right top|bottom)

颜色:关键词、十六进制色值、rgb(r,g,b)、rgba(r,g,b,a)

范围:每个颜色结点的显示范围

注意:

方向:加前缀需要把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

3、百分比

```

- 重复线性渐变

```

background:repeating-linear-gradient(方向, 颜色1 范围1, 颜色2 范围2,...);

```

```

background: repeating-linear-gradient(180deg, red 0%, red 10%, yellow 10%,yellow 20%);

```

```css

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%);

}

/* 方向:加前缀需要把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%);

}

```

##### 2.6.2 径向渐变 radial-gradient

径向渐变 : 一个点到四周的渐变

- 语法

```html

backgrond: radial-gradient(渐变形状, 颜色1 范围1, 颜色2 范围2, ...);

渐变形状:椭圆(ellipse,默认值)、圆形(circle)

圆心位置

语法:background:radial-gradient(形状 at 水平位置 垂直位置,颜色1,颜色2)

取值:

- 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);

```

- 重复径向渐变

```html

backgrond: repeating-radial-gradient(渐变形状/圆心, 颜色1 范围1, 颜色2 范围2, ...);

```

```css

div {

width: 300px;

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);

}

/* 圆心位置 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);

}

/* 重复径向渐变 */

.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%);

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值