一、背景属性
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%);
}
二、多列布局
1、元素被分隔的列数——column-count属性
-
column-count: n; 元素内容被划分的列数,没有单位
2、列的宽度——column-width属性
-
column-width: npx; 分列之后每一列的宽度
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;
}
三、弹性盒子 重点
1、什么是弹性盒子?
-
弹性盒子是CSS3的一种新的布局模式。
引入弹性盒布局模型的目的是提供一种更加有效的方式来对一个容器中的子元素进行排列、对齐和空白空间的分配
-
-
操作方便,布局简单,移动端使用广泛
-
PC端浏览器支持情况较差
-
IE11或更低版本中,不支持或部分支持
-
在盒模型中较为灵活
-
-
弹性盒模型的内容包括:弹性容器、弹性子元素——项目
-
原理:为父元素设置flex属性,控制子元素的位置及排列方式
-
应用场景 : 移动端
2、设置弹性盒子——display属性
-
display: flex; 将对象作为块级弹性伸缩盒显示
-
display: inline-flex; 将对象作为内联块级弹性伸缩盒显示
-
注意:
将容器设置为flex布局之后,子元素中的float、clear、vertical-align属性都会失效
弹性子元素-类似于行内块元素,如果不设置宽高,由内容撑开;即使是行内元素也可以设置宽高
3、基本概念
-
flex容器、项目——弹性子元素
-
默认在容器中有两根轴线
-
默认主轴方向——x轴方向,水平向右(左侧为主轴起点,右侧为主轴终点)
-
默认交叉轴方向——y轴方向,水平向下(上方为交叉轴起点,下方交叉轴终点)
弹性子元素通常在弹性盒子内一行显示。默认情况每个容器只有一行。
注意: 主轴不一定是x轴,还可以是y轴,有一边是主轴,另外 一边就是侧轴
-
4、容器属性——添加弹性容器上
-
flex-direction属性:设置主轴的方向,子元素的排列方向
-
flex-direction: row; 默认值,主轴方向为水平方向,起点在左端
-
flex-direction: row-reverse; 主轴方向为水平方向,起点在右端
-
flex-direction: column; 主轴方向为垂直方向,起点在上方
-
flex-direction: column-reverse; 主轴方向垂直方向,起点在下方
<!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> * { margin: 0; padding: 0; } .warp, .warp1 { width: 300px; height: 300px; background-color: aqua; /* 设置弹性盒*/ display: flex; } div { width: 100px; height: 100px; } /* X为主轴 */ .warp { flex-direction: row; flex-direction: row-reverse; } /* Y为主轴 */ .warp1 { flex-direction: column; flex-direction: column-reverse; } .warp .box1 { background-color: red; } .warp .box2 { background-color: pink; } .warp1 .box1 { background-color: purple; } .warp1 .box2 { background-color: blue; } </style> </head> <body> <div class="warp"> <div class="box1">box1</div> <div class="box2">box2</div> </div> <hr> <div class="warp1"> <div class="box1">box1</div> <div class="box2">box2</div> </div> </body> </html> -
-
justify-content 属性:设置主轴的对齐方式,弹性子元素在主轴方向上的对齐方式,
-
justify-content: flex-start; 默认值,主轴顶端对齐
-
justify-content: flex-end; 主轴的末端对齐
-
justify-content: center; 居中对齐,子元素位于弹性容器的中心
-
justify-content: space-between; 两端对齐,子元素和子元素之间有空白空间,项目之间的间隔都相等。
-
justify-content: space-around; 四周对齐,子元素之前、之间、之后都留有空白空间,且空间自行分配,项目之间的间隔比项目与边框的间隔大一倍。
-
justify-content: space-evenly;平均对齐 弹性项目平均分布在该行上,相邻项目的间隔,项目与容器之间空间相等
/* X为主轴 */ .warp { /* 设置弹性盒*/ display: flex; flex-direction: row; justify-content: flex-start; justify-content: flex-end; justify-content: center; justify-content: space-between; justify-content: space-around; justify-content: space-evenly; } /* Y为主轴 */ .warp1 { /* 设置弹性盒*/ display: flex; flex-direction: column; justify-content: flex-start; justify-content: flex-end; justify-content: center; justify-content: space-between; justify-content: space-around; justify-content: space-evenly; } -
-
align-items属性:弹性子元素在(侧轴)交叉轴上的对齐方式
-
align-items: stretch; 默认值,如果弹性子元素没有高度或高度为auto,将占满整个容器的高度
-
align-items: flex-start;子元素在侧轴顶端对齐
-
align-items: flex-end; 子元素在侧轴末端对齐
-
align-items: center; 子元素在侧轴中间对齐
-
align-items: baseline; 子元素在第一行文字的基线对齐
.warp { /* 设置弹性盒*/ display: flex; flex-direction: row; align-items: flex-start; align-items: flex-end; align-items: center; align-items: stretch;(height: auto;) } /* align-items: baseline; */ <style> /* align-items: baseline; */ .box { width: 400px; height: 400px; background-color: pink; display: flex; align-items: baseline; margin: 50px auto; } .box span { width: 100px; height: 100px; background-color: red; font-size: 20px; color: #fff; } .box img { height: 150px; } </style> </head> <body> <div class="box"> <span>XJX</span> <img src="../img/bg1.jpg" alt=""> </div> </body> </html> -
5、弹性盒水平垂直居中
- 水平垂直居中的元素的父元素上设置相关属性 - display: flex; - justify-content: center; 主轴上子元素的对齐方式 - align-items: center; 交叉轴上子元素的对齐方式
<style>
.wrap {
width: 400px;
height: 400px;
background-color: chartreuse;
display: flex;
/* 水平居中 */
justify-content: center;
/* 垂直居中 */
align-items: center;
}
.wrap div {
width: 100px;
height: 100px;
background-color: pink;
font-size: 30px;
}
</style>
</head>
<body>
<div class="wrap">
<div>绝对居中</div>
</div>
</body>
</html>
本文深入探讨了CSS中的背景属性,包括多背景、尺寸设置、定位区域和颜色渐变。接着介绍了多列布局的实现,如列数、宽度、间距和分割线的控制。重点讲解了弹性盒子模型,阐述了其概念、显示方式以及通过display属性、flex-direction和justify-content、align-items属性实现的布局技巧。内容包含了丰富的代码示例,适合前端开发者学习参考。
144

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



