目录
3.设置一个元素相对于父盒子的水平垂直居中——margin: auto;
定义:层叠样式表,用来修饰html的,目前遵循的W3C发布的CSS3.0

- style>
- h1{
- color: rgb(239, 21, 21);
- font-style: italic;
- }
- </style>
内容:/*注释内容*/
使用CSS属性来实现最终可以将CSS代码和HTML代码进行分离,同时也可以提高代码的复用性
行内CSS>内部CSS>外部CSS
注意:加属性值后加!important 的所对应的属性优先级最高
- h1{
- color: rgb(228, 15, 33)!important;
- }

<link rel="stylesheet" type="text/css" href="index.css"/>
属性
注意:将内部样式表<style>……</style>里的资源剪切到css文件中(不包含style标签)并在表中引入<link rel="两表之间的关系" type="表的类型" href="css表的地址"/>
- <style type="text/css">
- @import url("外部css的地址");
- </style>
注意:css.html与index.css在同一个文件夹下
- <!-- css.html -->
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>外部css</title>
- <style type="text/css">
- @import url("index.css");
- </style>
- </head>
- <body>
- <h1>我是大神</h1>
- </body>
- </html>
- /* index.css */
- h1{
- color: aqua;
- }

结构:<div style="width: 200px; height: 200px; color:blue">我是div</div>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>行内css</title>
- </head>
- <body>
- <div style="width: 200px; height: 200px; color:blue">我是div</div><!--行内css-->
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>内部css</title>
- </head>
- <body>
- <h1>实验</h1>
- </body>
- <style>
- h1{
- color: aqua;
- }
- </style>
- </html>
目的:要使用CSS对HTML页面中的元素实现一对一,一对多或者多对一的控制
- <body>
- <h1>实验</h1>
- </body>
- <style>
- /* 标签名选择器 */
- h1{
- color: aqua;
- }
- </style>
- <body>
- <div class="a">111111</div>
- <div class="a">222222</div>
- <div>333333</div>
- </body>
- <style>
- /* 类选择器 */
- .a{
- color: aqua;
- }
- </style>
- <body>
- <div class="a">111111</div><!-- 绿色 -->
- <!--可以出现属于2类的情况-->
- <div class="a b">222222</div><!-- 绿色,以style标签靠后的类选择器为主,和啊a,b的标签内顺序无关(选择器加载覆盖) -->
- <div>333333</div>
- </body>
- <style>
- /* 类选择器 */
- .b{
- color: red;
- }
- .a{
- color: green;
- }
- </style>
选择器1选择器2{
属性:属性值;
}
注意: 选择器和选择器之间没有任何连接符号
- <body>
- <div class="a">我是div</div>
- <p class="a">我是p</p>
- </body>
- <style>
- /* 只选择div标签下的a类 */
- div.a{
- color: aquamarine;
- }
- </style>
- <body>
- <div>
- <p name="a">一段话</p>
- <div name="a">二段话</div>
- <p name="b">三段话</p>
- </div>
- </body>
- <style>
- p[name="a"]{
- color: blue;
- }
- </style>
- <body>
- <div id="a">111111</div>
- <div>222222</div>
- <div>333333</div>
- </body>
- <style>
- /* id选择器 */
- #a{
- color: red;
- }
- </style>
- /* 通配符选择武器,不管什么都具有下面的属性 */
- *{
- color: aqua;
- }
- <body>
- <div id="a">111111</div>
- <div class="b">222222</div>
- <div>333333</div>
- </body>
- <style>
- /* 分组选择器,多个选择器之间用,连接 */
- div,.b,#a{
- color: red;
- }
- </style>
- <body>
- <div>
- <p>我是一段话</p>
- </div>
- </body>
- <style>
- /* 包含选择器,下面表示选中div标签里的p标签,匹配原则:从右到左-由p到div */
- div p{
- color: red;
- }
- </style>
- <body>
- <div>
- <p>我是一段话</p>
- </div>
- </body>
- <style>
- /* 相对于上面的空格,这种形式说明的是选择div下的一级p标签 */
- div>p{
- color: red;
- }
- </style>

- <body>
- <div class="a">
- <p>道生一</p>
- <p>一生二</p>
- <p>二生三</p>
- <p>三生万物</p>
- </div>
- <div class="b"></div>
- <div class="c"></div>
- </body>
- <style>
- /* 匹配a类p标签下的最后一个标签 */
- .a p:last-child{
- color: red;
- }
- .a p:first-child{
- color: blue;
- }
- /* 匹配a类p标签下的第二个标签 */
- .a p:nth-child(2){
- color: yellow;
- }
- /* 匹配a类p标签下的偶数位(even)标签奇数(odd)为2n+1/2n-1 */
- .a p:nth-child(even){
- color: pink;
- }
- </style>
- <body>
- <div class="a">
- <p>道生一</p>
- <p>一生二</p>
- </div>
- <div class="b">
- <p>二生三</p>
- </div>
- <div class="c"></div>
- </body>
- <style>
- /* 用:root代替html */
- :root,body{
- height: 100%;
- background: yellow;
- }
- div{
- width: 200px;
- height: 200px;
- }
- /* 选出有且仅有一个孩子的标签 */
- div p:only-child{
- color: red;
- }
- /* 选出没有孩子的p元素,注意,空格和文字也算孩子 */
- div:empty{
- background: green;
- }
- </style>
- <body>
- <div class="a" id="a">a</div>
- <div class="b" id="b">b</div>
- <div class="c" id="c">c</div>
- <ul>
- <li><a href="#a">点我到a</a></li>
- <li><a href="#b">点我到b</a></li>
- <li><a href="#c">点我到c</a></li>
- </ul>
- </body>
- <style>
- div{
- width: 1000px;
- height: 1000px;
- }
- ul{
- list-style: none;
- position: fixed;
- right: 0;
- bottom: 300px;
- }
- a{
- display: inline-block;
- border: 1px solid blue;
- text-decoration: none;
- }
- /* 选定div标签下锚点点到的div目标 */
- div:target{
- background: yellow;
- }
- </style>

E:focus:匹配获取焦点的元素E
- <body>
- <form action="#">
- 用户名:<input type="text"><br>
- 密码:<input type="password" disabled><br>
- 记住用户名:<input type="checkbox"><br>
- <input type="submit" value="提交">
- </form>
- <div>总有一天,我会改变世界</div>
- </body>
- <style>
- /* 选中input标签可用状态的元素 */
- input:enabled{
- background: red;
- }
- /* 选中input标签不可用状态的元素 */
- input:disabled{
- background: yellow;
- }
- /* 选中input标签获取焦点的元素 */
- input:focus{
- background: green;
- }
- input:checked{
- background: blue;
- }
- /* checked生效的条件,去掉默认样式,外观表现不生效(有个性) */
- input[type=checkbox]{
- /* 外观表现不生效 */
- appearance: none;
- /* 加样式 */
- width: 20px;
- height: 20px;
- border: 1px solid pink;
- }
- /* 匹配div中被用户选中的部分 */
- div::selection{
- background: red;
- color: aqua;
- }
- </style>
- <body>
- <ul>
- <li>111</li>
- <li>222</li>
- <li>333</li>
- </ul>
- </body>
- <style>
- /* 否定伪类选择器,不选择第一个li */
- li:not(:first-child){
- background: red;
- }
- </style>
语法(style里面有顺序如下)

- <body>
- <ul>
- <li>111</li>
- <li class="a">222</li>
- <li>333</li>
- <li>444</li>
- <li>555</li>
- </ul>
- </body>
- <style>
- /* 将a类后的第二个li标签进行选择 */
- .a+li+li{
- background: yellow;
- }
- </style>
- <body>
- <ul>
- <li>111</li>
- <li class="a">222</li>
- <li>333</li>
- <li>444</li>
- <li>555</li>
- </ul>
- </body>
- <style>
- /* 将a类后所有的li标签进行选择 */
- .a~li{
- background: yellow;
- }
- </style>
注意:以上两种选择器生效于兄弟节点
- <body>
- <input type="text"/>
- </body>
- <style>
- /* 属性选择器3种 */
- [type]{
- background-color: aquamarine;
- }
- /*完全匹配,如果类有2个值其中有一个值符合则不生效*/
- [class="text"]{
- height: 200px;
- }
- /*同上*/
- input[type="text"]{
- color: blue;
- }
- /* 类中有一个包含box1则生效 */
- div[class~=box1]{
- border: 1px solid blue;
- }
- </style>
- <body>
- <div class="bcc"></div>
- <div class="cbc"></div>
- <div class="ccb"></div>
- </body>
- <style>
- /* 选择以b开头的 */
- div[class^="b"]{
- width: 20px;
- height: 20px;
- background: red;
- }
- /* 选择以b结尾的 */
- div[class$="b"]{
- width: 20px;
- height: 20px;
- background: green;
- }
- /* 选择包含b的 */
- div[class*="b"]{
- width: 20px;
- height: 20px;
- border: 1px solid blue;
- }
- </style>
- <body>
- <div>i am a dog</div>
- </body>
- <style>
- div::before{
- /* 在div的前面加为元素,内容如下 */
- content: "hello world";
- }
- div::after{
- content: "haha";
- }
- </style>
- <style>
- div::before{
- /* 在div的前面加为元素,内容如下 */
- content: "hello world";
- }
- /* 不占宽高的伪元素 */
- div::after{
- content: "我是个大字";
- clear: both;
- display: block;
- width: 0;
- height: 0;
- visibility: hidden;/* 设置内容的可见性为隐藏 */
- }
- </style>


word-spacling:词间距
color:字体颜色
text-transform: capitalize;
text-overflow:ellipsis;(溢出并隐藏后的文本用…显示)
text-shadow: 水平方向位移 垂直方向位移 模糊程度 阴影颜色;(文本阴影——单)
text-shadow: 水平方向位移 垂直方向位移 模糊程度 阴影颜色,水平方向位移 垂直方向位移 模糊程度 阴影颜色;(文本阴影——多)
注意:多阴影之间的多个阴影用,隔开
- body>
- <p class="a">我是一段话</p>
- </body>
- <style>
- .a{/* 首行缩进2个汉字 */
- text-indent: 2em;
- }
- </style>
- <body>
- <p class="a">hello world</p>
- </body>
- <style>
- p{
- /* 每一个单词首字母大写,如果是uppercase表示每一个字母均转大写 */
- text-transform: capitalize;
- }
- </style>
- <body>
- <div>只要学不死,就往死里学</div>
- </body>
- <style>
- div{
- text-shadow: 10px 10px 2px red,-10px -10px 2px yellow;
- }
- </style>

- <body>
- <ul>
- <li>11111</li>
- <li>22222</li>
- <li>33333</li>
- </ul>
- </body>
- <style>
- ul{
- list-style-type: disc;
- }
- </style>
- <body>
- <ul>
- <li>11111</li>
- <li>22222</li>
- <li>33333</li>
- </ul>
- </body>
- <style>
- ul{
- /* 图片作为图标方式 */
- list-style-image: url(../HTML/timg.jpg);
- }
- </style>
作用:将标记放到盒子的里面还是外面
- <body>
- <ul>
- <li>11111</li>
- <li>22222</li>
- <li>33333</li>
- </ul>
- </body>
- <style>
- li{
- border: 1px solid red;
- list-style-position: outside;/* 外面 */
- }
- </style>

相当于把list-style-type,list-style-image,list-style-position合并在一起
- <body>
- <ul>
- <li>11111</li>
- <li>22222</li>
- <li>33333</li>
- </ul>
- </body>
- <style>
- li{
- border: 1px solid red;
- list-style:disc none inside;/* 里面 */
- }
- </style>


background-size——背景图片大小(复合属性——宽 高)
- <body>
- <div>我是一段话</div>
- </body>
- <style>
- div{
- /* 背景颜色红色,半透明 */
- background-color: rgba(255,0,0,0.5);
- }
- </style>
注意:背景图片默认为平铺(当背景图片大小小于盒子大小时)
属性
- <body>
- <div>我是一段话</div>
- </body>
- <style>
- div{
- width: 300px;
- height: 300px;
- background-image: url(../HTML/图片.jpg);
- background-position: 20px 20px;/* 当然里面也可以写百分数,也可以写英文(left、center、right,top、center、bottom) */
- opacity: 0.6;/* 设置图片透明度 */
- }
- </style>
属性:background-size(复合属性——宽 高)
cover:把背景图片(等比例)扩展至足够大,使背景图片完全覆盖整个区域,但可能会丢失图片部分
contion:会把图片(等比例)扩展,但会将图片完完整整显示在墙上
- <body>
- <div>我是一段话</div>
- </body>
- <style>
- div{
- width: 300px;
- height: 300px;
- background-image: url(../HTML/图片.jpg);
- background-size: 300px 300px;/* 宽 高(当然也可以用百分数) */
- }
- </style>
scroll:相对于盒子固定(默认值)
background属性
- <body>
- <div>我是一段话</div>
- </body>
- <style>
- div{
- width: 300px;
- height: 300px;
- /*这里看不到图片,因为盒子大小不够*/
- background:yellow url(../HTML/图片.jpg) no-repeat center fixed;
- }
- </style>
注意:会有覆盖现象
- <style>
- div{
- width: 300px;
- height: 300px;
- background-color: yellow;
- background:url(../HTML/图片.jpg) no-repeat;/* 因为这里没写属性,会把上面的颜色属性覆盖,认为没有颜色 */
- }
- </style>
语法:visibility: 属性值;
属性值
display:none;与visibility:hidden;区别
语法:opacity:0;
当其值为0时表示透明,值为1时表示不透明
- <body>
- <div></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: blue;
- }
- div:hover{
- opacity:0;
- }
- </style>
语法:cursor:属性值;
表示鼠标放在被cursor属性修饰的块内光标的显示结果
属性值

注意:多个div如果都浮动那么他们就会按照先后浮动顺序依次排成一行,一行不够那么就开第二行,浮动的元素和不浮动的元素在一起则浮动的会把不浮动的元素盖住,但是文字不会被盖住
- <body>
- <div class="red">我是一段</div>
- <div class="green">我是二段</div>
- <div class="blue">我是三段</div>
- </body>
- <style>
- div{
- width: 300px;
- height: 300px;
- }
- .red{
- background: red;
- float: left;
- }
- .green{
- background: green;
- float: left;
- }
- .blue{
- height: 400px;
- background: blue;
- }
- </style>

注意:蓝色的部分虽然被红色覆盖,但是文字依然看得见
浮动规则:见缝插针
- <body>
- <div class="red">我是一段</div>
- <div class="green">我是二段</div>
- <div class="blue">我是三段</div>
- </body>
- <style>
- .red{
- width: 1000px;
- height: 900px;
- background: red;
- float: left;
- }
- .green{
- width: 800px;
- height: 800px;
- background: green;
- float: left;
- }
- .blue{
- width: 600px;
- height: 100px;
- background: blue;
- float: left;
- }
- </style>


含义:清除浮动元素盖住不浮动元素的效果

- <body>
- <div class="red">我是一段</div>
- <div class="green">我是二段</div>
- <div class="blue">我是三段</div>
- </body>
- <style>
- div{
- width: 300px;
- height: 300px;
- }
- .red{
- background: red;
- float: left;
- }
- .green{
- width: 600px;
- background: green;
- clear:left;/* 清浮动 */
- }
- .blue{
- background: blue;
- }
- </style>

- <body>
- <div class="contain">
- <div class="red">我是一段</div>
- </div>
- <div class="blue">我是三段</div>
- </body>
- <style>
- .contain{
- overflow: hidden;
- }
- .red{
- width: 300px;
- height: 300px;
- background: red;
- float: left;
- }
- .blue{
- width: 300px;
- height: 300px;
- background: blue;
- }
- </style>
- <body>
- <div class="box1"><img src="../HTML/图片.jpg"/></div>
- </body>
- <style>
- .box1{
- width:200px ;
- height: 200px;
- background: yellow;
- overflow-x: auto;
- overflow-y: hidden;
- }
- </style>
white-space属性

static:默认按照文档流的顺序,从左到右,从上到下将网页填满(和行内与块级元素有关)
- <body>
- <div class="box1"></div>
- <div class="box2"></div>
- <div class="box3"></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- }
- .box1{
- background:red ;
- }
- .box2{
- background:blue ;
- position: relative;
- top: 100px;
- left: 100px;
- }
- .box3{
- background: yellow;
- }
- </style>
- <body>
- <div class="box1"></div>
- <div class="box2"></div>
- <div class="box3"></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- }
- .box1{
- height: 600px;
- background:red ;
- }
- .box2{
- background:blue ;
- position: sticky;
- top: 0px;
- }
- .box3{
- height: 800px;
- background: yellow;
- }
- </style>
如果两个兄弟块级元素定位到相同位置,那么后面定位的块会把前面定位的块盖住(后来居上),如果想要让之前的块级元素盖住后面定位的块级元素,那么需要设置层级,并且需要前面块级元素的层级大于后面块级元素的层级
层级为10
z-index:10;
- <body>
- <div class="box1"></div>
- <div class="box2"></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- }
- .box1{
- background:red ;
- position: relative;
- top: 10px;
- right: 10px;
- z-index: 10;/* 设置层级为10 */
-
- }
- .box2{
- background:blue ;
- position: relative;
- z-index: 1;
- }
- </style>
width:auto;
width/height属性不写或者auto都为宽/高自适应(自适应窗口大小,不会出滚动条——窗口盒子大小不变)
min-height:500px;
如果被修饰的块没内容,那么他会占高度500px,有内容则高度大于500px
定义:盒子根据窗口大小进行改变
/* 窗口自适应设定 */
html,body{height: 100%;}
- <body>
- <div>i am a pig</div>
- </body>
- <style>
- div{
- width: 100%;
- height: 100%;
- background:red;
- }
- /* 窗口自适应设定 */
- html,body{
- height: 100%;
- }
- </style>
用途:用于动态计算长度值
- <body>
- <div class="box1"></div>
- <div class="box2"></div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 100%;
- background:red;
- float: left;
- }
- .box2{
- /* calc函数使用场景 */
- width: calc(100% - 200px);
- height: 100%;
- background:rgb(16, 195, 64);
- float: left;
- }
- /* 窗口自适应设定 */
- html,body{
- height: 100%;
- }
- </style>
盒子模型是CSS布局的基石,它规定了网页元素如何显示以及元素间的关系。CSS定义所有元素都可以拥有像盒子一样的外形和平面空间,即都包含边框、边界、补白、内容区。

内部包含3种属性
线的样式
- <body>
- <div class="box1">
- 我是个大神
- </div>
- </body>
- <style>
- .box1{
- width: 100px;
- height: 100px;
- background: yellow;
- border: 10px dashed transparent;/* 各个方向的边框均为此属性,透明边框 */
- }
- </style>
注意:背景颜色也能蔓延到边框中,但是边框不在盒子(宽高)范围内

padding:相对于自己的内部边距
- <body>
- <div>
- 我是个大神
- </div>
- </body>
- <style>
- div{
- width: 500px;
- height: 500px;
- background: yellow;
- padding: 20px;/* div盒子内部各个方向内边距20px */
- }
- </style>
margin:相对于自己的外部边距
- <body>
- <div class="box1">我是一个方块</div>
- <div class="box2">我也是一个方块</div>
- </body>
- <style>
- div{
- width: 100px;
- height: 100px;
- }
- .box1{
- background: red;
- margin: 10px;/* box的四周外边距均为10px */
- }
- .box2{
- background: yellow;
- }
- </style>
两个盒子垂直外边距与水平外边距问题
给子盒子加外边距
最终外边距作用在子盒子身上以父盒子为外 ,前提是设置边框,不设置边框的话那么就会外边距直接作用于父盒子身上

独占一行,可以设置宽高,遵循盒子模型(内外边距,边框均可设置)
display:block;
display:list-item;
注意:p标签放文本可以,不能放块级元素
多个行内元素处于同一行,不能设置宽高,左右内外边距,边框均可设置,上下内外边距设置无效
display:inline;
兼具行内元素和块级元素特征,支持盒子模型
display:inline-block;
隐藏属性
display:none;
box-shadow属性(复合属性)

- <body>
- <div>只要学不死,就往死里学</div>
- </body>
- <style>
- div{
- width: 100px;
- height: 100px;
- background: yellow;
- margin: 0 auto;
- box-shadow: 10px 10px 10px 10px red,-10px -10px 10px blue inset;
- }
- </style>
注意:如果不加insert属性,那么默认是外阴影,加了insert属性就变内阴影,内外阴影如下


border-radius属性(复合属性)
border-radius:圆角效果;
注意:圆角效果的大小等于从矩形的顶点开始向两边延申的距离,到达指定距离的两个点之间用来形成圆角
- <body>
- <div></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: yellow;
- margin: 0 auto;
- /* 设置左下角为圆角 */
- border-bottom-left-radius: 10px;
- }
- </style>
border-radius:水平距离/竖直距离;
注意:这里的圆角效果的大小等于从矩形的顶点向水平延申的距离,以及竖直延申的距离,之后到达指定距离的两个点之间用来形成圆角,并且,这种写法只支持border-radius属性
用来自定义多个角的话写法如下
- <body>
- <div></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: yellow;
- margin: 0 auto;
- border-radius: 10px 20px 30px 40px/50px 60px 70px 80px;
- }
- </style>
含义:更改原盒子模型的计算方式通过box-sizing属性的取值进行更改
属性:box-sizing——默认值是content-box(标准盒子模型)
该属性允许您以特定的方式定义匹配某个区域的特定元素
这是由CSS2.1规定的高度行为。宽度和高度分别应用到元素的内容框。在宽度和高度之外绘制元素的内边距和边框

属性值:content-box

属性值:border-box
为元素设定的宽度和高度决定了元素的边框盒子
就是说,为元素指定的任何内边距和边框都将在已设定的宽度和高度内进行绘制。通过已设定的宽度和高度分别减去边框和内边距才得到内容的宽度和高度
- <body>
- <div class="box1"></div>
- <div class="box2"></div>
- </body>
- <style>
- /* 怪异盒子模型 */
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- padding: 30px;
- border: 10 solid black;
- box-sizing: border-box;
- }
- /* 普通盒子模型 */
- .box2{
- width: 200px;
- height: 200px;
- background: red;
- margin-top: 100px;
- padding: 30px;
- border: 10 solid black;
- box-sizing: content-box;
- }
- </style>
display:flex;
- <body>
- <div class="box">
- <div class="box1"></div>
- <div class="box2"></div>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- }
- .box2{
- width: 200px;
- height: 200px;
- background: red;
- }
- /*设置弹性盒子*/
- .box{
- display: flex;
- }
- </style>

- <body>
- <div class="box">
- <span class="box1">毁灭吧!</span>
- <span class="box2">颤抖吧!</span>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- }
- .box2{
- width: 200px;
- height: 200px;
- background: red;
- }
- /* 设置弹性盒子 */
- .box{
- display: flex;
- }
- </style>

- <body>
- <div class="box">
- <span class="box1">毁灭吧!</span>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- /* 只有1个元素的时候设置此元素水平垂直居中 */
- margin: auto;
- }
- /* 设置弹性盒子 */
- .box{
- width: 600px;
- height: 600px;
- border: 1px solid black;
- display: flex;
- }
- </style>
属性:flex-direction
属性值
- <body>
- <div class="box">
- <span class="box1">毁灭吧!</span>
- <span class="box2">颤抖吧!</span>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- }
- .box2{
- width: 200px;
- height: 200px;
- background: red;
- }
- /* 设置弹性盒子 */
- .box{
- width: 600px;
- height: 600px;
- border: 1px solid black;
- display: flex;
- /* 设置垂直拍列方式 */
- flex-direction: column;
- }
- </style>
属性名:justify-content
- <body>
- <div class="box">
- <span class="box1">毁灭吧!</span>
- <span class="box2">颤抖吧!</span>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- background: green;
- }
- .box2{
- width: 200px;
- height: 200px;
- background: red;
- }
- /* 设置弹性盒子 */
- .box{
- width: 600px;
- height: 600px;
- border: 1px solid black;
- display: flex;
- /* 调整主轴对齐方式——居中 */
- justify-content: center;
- }
- </style>
属性名:aline-items
注意:在主轴上排列始终会在主轴排列,不会换行,不管块总宽度是否大于盒子宽度,如果大于则块宽度会被挤压变小
属性:flex-shrink
折行属性:flex-wrap
作用:如果主轴显示不下就会直接折行,但是会行与行之间进行等间距排列
- <body>
- <div class="box">
- <span class="box1">毁灭吧!</span>
- <span class="box2">颤抖吧!</span>
- <span class="box2">恐惧吧!</span>
- <span class="box2">颤栗吧!</span>
- </div>
- </body>
- <style>
- span{
- width: 200px;
- height: 200px;
- border: 1px solid red;
- }
- /* 设置弹性盒子 */
- .box{
- width: 600px;
- height: 600px;
- border: 1px solid black;
- display: flex;
- /* 调整主轴对齐方式——居中 */
- justify-content: center;
- /* 主轴显示不下会折行 */
- flex-wrap: wrap;
- }
- </style>
属性名:align-content
理解:设置弹性盒子的盒子为容器,容器里面的块级元素被称为项目,这是为弹性盒子每个项目设置的属性
注意:可以脱离折行控制
属性名:align-self
- <body>
- <div class="box">
- <div class="box1">毁灭吧!</div>
- <div class="box2">颤抖吧!</div>
- </div>
- </body>
- <style>
- .box1{
- width: 200px;
- height: 200px;
- border: 1px solid red;
- align-self: center;
- }
- .box2{
- width: 200px;
- height: 200px;
- border: 1px solid red;
- }
- /* 设置弹性盒子 */
- .box{
- width: 600px;
- height: 600px;
- border: 1px solid black;
- display: flex;
- /* 调整主轴对齐方式——居中 */
- justify-content: center;
- }
- </style>
order:0;
注意:order值默认为0,order值越大,顺序越靠后,如果多个order值一样,那么就按正常顺序排列(order可以为负数)。
flex:1;
给项目加上此属性意思为占满剩余空间,如果多个项目flex属性相同,那么就会出现空间均分的情况,如果不同,则按比例换算自己占总的几份空间就占总的几份
含义:将网页划分成一个个网格,可以任意组合成不同的网络,做出各种各样的布局
容器:一个案例中最大的盒子,可以理解成父元素
项目:一个案例中,最大盒子里面的内容,可以理解成子元素
行和列:容器里面水平区域称为行,垂直区域称为列
单元格:行和列交叉的区域
含义:网格布局中的属性和flex中的布局类似,分成了两类,一类容器属性,一类项目属性
给父元素添加:display:grid;
display关于网格的取值分为两个,grid(块网格)和inline-grid(行内块网格)
注意:给行内元素加display:grid;就可以将其变成块元素
规定行属性:grid-template-row:n;
规定列属性:grid-template-column:n;
后面的取值数量代表多少行,多少列
绝对大小:npx

百分比:n%

注意:行或列百分比总和为100%
功能函数repeat()

auto-fill关键字(功能填充)配合功能函数使用

fr关键字 (列宽片段)
为了方便表示比例关系,网格布局提供了fr关键字(fraction的缩写,意味片段)如果两列的宽度分别为1fr和2fr,就表示后者是前者的两倍。

注意:以上5fr=表格列宽
- <body>
- <div class="box">
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- <div>8</div>
- <div>9</div>
- </div>
- </body>
- <style>
- .box{
- width: 600px;
- height: 600px;
- background: rgb(159, 12, 12,0.7);
- display: grid;
- /* 划分为3行3列网格 */
- grid-template-rows: repeat(auto-fill,200px);
- grid-template-columns: 1fr 1fr 1fr;
- }
- </style>
设置最小/最大值
grid-template-rows: minmax(100px,200px) 200px 300px;
minmax(100px,200px):最小100px最高200px
- <body>
- <div class="box">
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- <div>8</div>
- <div>9</div>
- </div>
- </body>
- <style>
- .box{
- width: 600px;
- height: 600px;
- background: rgb(159, 12, 12,0.7);
- display: grid;
- /* 划分为3行3列网格 */
- grid-template-rows: minmax(100px,200px) 200px 300px;
- grid-template-columns: repeat(3,200px);
- }
- </style>

grid-gap:30px 30px;
第一个值表示行间距,第二个值表示列间距

合并网格的时候使用“grid-area:网格名字;”进行合并

设置子元素区域名字
grid-area: 区域名字;
结果图

- <body>
- <div class="box">
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- <div>8</div>
- <!-- <div>9</div> -->
- </div>
- </body>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- .box{
- width: 646px;
- height: 646px;
- border: 5px solid green;
- display: grid;
- grid-template-rows: repeat(3,200px);
- grid-template-columns: 200px 200px 200px;
- grid-row-gap: 20px;
- grid-column-gap: 20px;
- grid-template-areas:'a b c'
- 'd e e'
- 'g h i';
- }
- .box div{
- /* 单元格边框 */
- border: 1px solid red;
- }
- .box div:nth-child(1){
- /* 设置第一个div子元素名字为a */
- grid-area: e;
- border: 1px solid blue;
- }
- </style>
注意:每个块占的位置只能是个矩形
设置子盒子排列方式为纵向排列
grid-auto-flow: column;


注意:单元格对齐方式默认为stretch,不加宽高才好用
- <body>
- <div class="box">
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- <div>7</div>
- <div>8</div>
- <div>9</div>
- </div>
- </body>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- .box{
- width: 600px;
- height: 600px;
- border: 5px solid green;
- display: grid;
- grid-template-rows: repeat(3,100px);
- grid-template-columns: repeat(3,100px);
- grid-auto-flow: column;
- /* 调整主元素水平对齐 */
- justify-content:center;
- /* 调整主元素竖直对齐 */
- align-content: center;
- /* 调整子元素水平对齐 */
- justify-items: center;
- /* 调整子元素竖直对齐 */
- align-items: center;
- }
- .box div{
- /* 单元格边框 */
- border: 1px solid red;
- width: 50px;
- height: 50px;
- }
- </style>

- <body>
- <div class="box">
- <div>1</div>
- <div>2</div>
- <div>3</div>
- <div>4</div>
- <div>5</div>
- <div>6</div>
- </div>
- </body>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- .box{
- width: 600px;
- height: 600px;
- border: 5px solid green;
- display: grid;
- grid-template-rows: repeat(3,200px);
- grid-template-columns: repeat(3,200px);
- }
- .box div{
- /* 单元格边框 */
- border: 1px solid red;
- }
- .box div:nth-child(1){
- /* 起始列网格线是1 */
- grid-column-start: 1;
- /* 结束列网格线是3 */
- grid-column-end: 3;
- /* 起始行网格线是1 */
- grid-row-start: 1;
- /* 结束行网格线是3 */
- grid-row-end: 3;
- /* 因此1占2行共4个单元格 */
- }
- </style>
- .box div:nth-child(1){
- /* 简写方式 */
- grid-column: 1/3;
- grid-row: 1/3;
- /* 因此1占2行共4个单元格 */
- }
- .box div:nth-child(1){
- /* 简写方式 */
- grid-column: 2/4;
- grid-row: 2/4;
- /* 通过网格线改变块的位置与大小 */
- }
设备像素比(dpr)=物理像素/CSS像素
布局视口:布局视口是指网页的宽度,一般移动端浏览器都默认设置了布局视口的宽度。
视觉视口:视觉视口是指用户正在看到网页的区域,这个区域的宽度等同于移动设备浏览器窗口的宽度。
理想视口:理想窗口是指对设备来讲最理想的视口尺寸,采用理想视觉窗口的方式,可以使网页在移动端浏览器上获得最理想的浏览和阅读的宽度
<meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
- /* 定制滚动条消失 */
- ::-webkit-scrollbar{
- display: none;
- }
column-count:规定元素被分割的列数
column-gap:规定列之间的间隔大小
column-rule:调整列边框(复合属性)
column-fill:设置或检索对象所有列的高度是否统一
column-width:设置或检索对象每列的宽度
column-span:该元素是否横跨父级元素所有列
break-inside:是否允许该盒子内部折行
- <body>
- <div>
- <h1 class="a">hello world</h1>
- <h1>hello world</h1>
- <h1>hello world</h1>
- <h1>hello world</h1>
- <h1>hello world</h1>
- </div>
- </body>
- <style>
- div{
- height: 300px;
- background: pink;
- /* div内容显示5列 */
- column-count: 5;
- /* 调整列间距 */
- column-gap: 50px;
- /* 调整列边框 */
- column-rule: 2px solid red;
- /* 列高度统一属性,会先占满列高度,可能达不到5列 */
- column-fill:auto;
- /*调整列宽 */
- column-width: 100px;
- }
- .a{
- /* 横跨所有的列 */
- column-span: all;
- text-align:center;
- }
- h1{
- /* 不允许盒子内部进行折行 */
- break-inside: avoid;
- }
- </style>
定义:按照屏幕的大小进行动态的响应,快速的反应,快速的处理让每个屏幕都能在这套网页中很好的展示,这就叫响应式布局
固定布局:以像素作为页面的基本单位,不管设备屏幕以及浏览器宽度,只设计一套尺寸
可切换的固定布局:同样以像素作为页面单位,参考主流设备尺寸,设计几套不同的宽度布局。通过识别的屏幕尺寸以及浏览器宽度,选择最合适的那套宽度布局
弹性布局:以百分比作为页面的基本单位,可以适应一定范围内所有尺寸的设备屏幕以及浏览器宽度,并能完美的利用有效空间展现最佳效果
混合布局:同弹性布局类似,可适应一定范围内所有尺寸的设备屏幕及浏览器宽度,并能完美利用有效空间展现最佳效果;只是混合像素、和百分比两种单位作为页面的基本单位
布局响应:对页面进行响应式的设计实现,需要对相同内容进行不同宽度的布局设计,有2种方式
无论基于哪种模式的设计,要兼容所有设备,布局响应时不可避免地需要对模块布局做一些变化(发生布局改变的临界点称为布局断点)
媒体查询可以让我们根据设备显示器的特性(如视口宽度、屏幕比例、设备方向:横向或纵向)为其设置CSS样式,媒体查询由媒体类型和一个或者多个检测媒体特性的条件表达式组成。媒体查询中可用于检测的媒体特性有width、height、color等。使用媒体查询,可以在不改变页面内容的情况下,为特定的一些输出设备制定显示效果。
对设备提出询问(表达书)开始,如果表达式为真,媒体查询中的CSS被应用,若为假则忽略
媒体查询结构
- @media all and(min-width:320px){
- body{
- background: red;
- }
- }
理解:对于所有的媒体,若宽度>=320px则使用如下样式

@media only screen and (min-width:1029px){}
orientation属性
竖屏时执行后面操作
@media screen and (orientation:portrait) and (max-width:720px){}
横屏时执行后面操作
@media screen and (orientation:landscape){}
注意:当宽度比高度大的时候浏览器认为是横屏,否则为竖屏

- <style>
- *{
- margin: 0;
- padding: 0;
- }
- html,body{
- height: 100%;
- }
- /* width>1000px */
- @media screen and (min-width:1000px) {
- body{
- background: blue;
- }
- }
- /* width<500 */
- @media screen and (max-width:500px) {
- body{
- background: red;
- }
- }
- /* 500px<width<1000px */
- @media screen and (max-width:1000px) and (min-width:500px) {
- body{
- background: green;
- }
- }
- </style>
字体模块:@font-face
@font-face{
font-family:<YourWebFontName>;
src:<source>[<format>][,<soure>[format>]]*;
[font-weight:<weight>];
[font-style:<style>];
}
- <body>
- <div>赵钱孙礼</div>
- </body>
- <style>
- @font-face {
- font-family: hello;
- src: url("字体样式路径");
- }
- div{
- font-family: hello;
- font-size: 50px;
- }
- </style>
目的:解决移动端等比例缩放问题
- <body>
- <div class="div1">
- <p>div1</p>
- </div>
- <div class="div2">
- <p>div2</p>
- </div>
- </body>
- <style>
- html{
- font-size: 32px;
- }
- .div1,.div2{
- border: 1px solid red;
- font-size: 16px;
- }
- .div1 p{
- font-size: 1rem;
- }
- .div2 p{
- font-size: 2em;
- }
- </style>
- <body>
- <div class="box">
- hello
- </div>
- </body>
- <style>
- .box{
- width: 3.75rem;
- height: 100px;
- background: red;
- }
- </style>
- <script>
- /* html标签的fontSize随着clientWidth动态改变 */
- document.documentElement.style.fontSize=document.documentElement.clientWidth/375 * 100 +'px'
- </script>
功能:将导航背景图片,按钮背景图片等有规则的合成一张背景图,即多张图片合为一整图,然后用background-position来实现背景图片的定位技术

两栏布局:左边固定,右边块自适应
三栏布局:两边固定,中间块自适应
- <body>
- <div class="left"></div>
- <div class="right"></div><!-- 在不浮动块的前面 -->
- <div class="center"></div><!--因为其是块级元素独占一行-->
- </body>
- <style>
- .left,.right{
- width: 200px;
- height: 100%;
- }
- .left{
- background: red;
- float: left;
- }
- .right{
- background: blue;
- float: right;
- }
- .center{
- height: 100%;
- background: pink;
- margin-left: 200px;
- margin-right: 200px;
- }
- /* 窗口自适应设定 */
- html,body{
- height: 100%;
- }
- </style>
- <body>
- <div class="left"></div>
- <div class="center"></div>
- <div class="right"></div>
- </body>
- <style>
- .left,.right{
- width: 200px;
- height: 100%;
- }
- .left{
- background: red;
- float: left;
- }
- .right{
- background: blue;
- float: right;
- }
- .center{
- width: calc(100% - 400px);
- height: 100%;
- background: pink;
- float: left;
- }
- /* 窗口自适应设定 */
- html,body{
- height: 100%;
- }
- </style>
定义:用CSS方式实现的一个颜色到另一个颜色之间的平稳的过渡
background: linear-gradient(direction,color1,color2);
background: linear-gradient(90deg,red,blue);
理解:red在下面,blue在上面,随着度数的增加顺时针旋转
注意:颜色可以写多个值,支持多颜色渐变 ,颜色后面也可以加百分比来控制渐变

- <body>
- <div class="box">
- hello world
- </div>
- </body>
- <style>
- html,body{
- width: 100%;
- height: 100%;
- }
- div{
- height: 100%;
- background: linear-gradient(to top left,red,blue);
- }
- </style>
径向渐变不同于线性渐变,线性渐变是从一个方向向另一个方向的渐变,而径向渐变是从一个点向四周的渐变

background: radial-gradient(center,shape,size,color1,color2);
center:渐变起点位置
shape:渐变的形状
size:渐变的大小,即渐变到哪里停止

background: radial-gradient(red 5%,green 20%,blue 30%);
理解:5%的前面全为红色,之后红色从5%时就开始渐变直到20%变为绿色,绿色从20%开始渐变直到30%变为蓝色,之后不需要渐变剩下区域全为蓝色
background: -webkit-radial-gradient(20% 40%,red,green,blue);
-webkit-radial-gradient解决浏览器兼容问题,从浏览器内核的角度支持一下此属性的设置
background: repeating-linear-gradient(red 5%,green 20%,blue 30%);
理解:5%的前面承接100%后面,之后红色从5%时就开始渐变直到20%变为绿色,绿色从20%开始渐变直到30%变为蓝色,之后又开始重复上述过程,直到到达100%
background: repeating-radial-gradient(red 5%,green 20%,blue 30%);
含义:CSS3的transition允许CSS的属性值在一定时间区间内平滑的过渡。这种效果可以在鼠标单击、获得焦点、被点击或对元素任何改变中触发,并圆滑的以动画效果改变CSS属性值
transition: all 5s 3s linear;
all:单一属性-transition-property:检索或设置对象中参与过渡的属性(all表示所有属性均变)
5s:单一属性-transition-duration:检索或设置对象过渡的持续时间
3s:单一属性-transition-dely:检索或设置对象延迟过渡的时间(什么时候开始过渡)
linear:单一属性-transition-timing-function:检索或设置对象中过渡的动画类型
简写:transition:all/具体属性值 运动时间s/ms 延迟时间s/ms 动画类型
注意:单一属性transition-property写属性值可以写多个,中间用空格隔开
- <body>
- <div class="box"></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: red;
- transition: all 5s 3s linear;
- }
- div:hover{
- width: 600px;
- height: 600px;
- }
- </style>

steps(n):动画执行的步数——8步
transition: all 5s 1s steps(8);
transition: all 5s 1s cubic-bezier(值1,值2,值3,值4);
其可以自定义变速动画
含义:将元素向指定方向移动
translateX(npx):水平方向移动一个对象,对象只在x轴上进行移动npx,若内部为正值,则对象右移,反之左移
translateY(npx):竖直方向移动一个对象,对象只在y轴上进行移动npx,若内部为正值,则对象下移,反之上移

含义:让元素根据中心圆点进行缩放,默认值为1.因此0.01-0.99之间任何值是一个元素缩小,任何大于1的值使元素显得更大
该函数可以接受1个值也可以接受2个值,若只有一个值时,其第二个值默认与第一个值相等
注意:该放大为中心点放大,里面的值为负数时会出现倒像
- <body>
- <div></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: blue;
- border: 1px solid red;
- transition: all 2s;
- margin: 0 auto;
- }
- div:hover{
- /* xy轴放大1.5倍 */
- transform: scale(1.5);
- }
- </style>
transform-origin: left top;
rotate(n)方法:通过指定的角度参数对元素根据对象原点指定一个2D旋转。它主要在二维空间内进行操作,接受一个角度值,用来指定旋转的幅度。如果这个值为正值,元素相对于原点中心顺时针旋转;若为负,则逆时针旋转。
rotateX(n)方法,元素围绕其x轴以给定的度数进行旋转,它主要在三维空间内进行操作
rotateY(n)方法,元素围绕其y轴以给定的度数进行旋转,它主要在三维空间内进行操作
注意rotate(n)等价于rotateZ(n)
- <body>
- <div class="box">
- <div></div>
- <div></div>
- <div></div>
- </div>
- </body>
- <style>
- .box{
- width: 600px;
- height: 600px;
- border: 2px solid gray;
- }
- .box div{
- width: 200px;
- height: 200px;
- background: red;
- border: 1px solid black;
- }
- .box div:nth-child(1){
- transform: translateX(400px);
- }
- .box div:nth-child(2){
- transform: translateX(400px) scale(0.5);
- }
- .box div:nth-child(3){
- transform: scale(0.5) translateX(400px);
- }
- /* 2,3产生不同情况的原因,盒子缩放时位移的距离也跟着缩放,进而导致结果不同 */
- </style>
- <body>
- <div class="box">
- <div></div>
- <div></div>
- <div></div>
- </div>
- </body>
- <style>
- .box{
- width: 600px;
- height: 600px;
- border: 2px solid gray;
- }
- .box div{
- width: 100px;
- height: 100px;
- background: red;
- border: 1px solid black;
- }
- .box div:nth-child(1){
- transform: translateX(400px);
- }
- .box div:nth-child(2){
- transform: translateX(400px) rotate(45deg);
- }
- .box div:nth-child(3){
- transform: rotate(45deg) translateX(400px);
- }
- /* 2,3产生不同情况的原因,盒子旋转时导致坐标轴旋转,进而位置不同 */
- </style>
注意:transform内可以写多个属性,但是掌握好先后顺序,不可以在一个块中写多个transform,因为会出现覆盖导致生效的只有最后一个
该函数能够让元素倾斜显示。它可以将一个对象以其中心位置围绕x轴和y轴按照一定的角度倾斜
- <body>
- <div>hello</div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: red;
- line-height: 200px;
- text-align: center;
- border: 1px solid black;
- font-size: 50px;
- margin: 0 auto;
- transform: skewX(30deg);
- /* 正值表示抓着右下角往右拉动,直至把上面的角拉的多出对应度数
- 负值表示抓着左下角往左拉动,直至把上面的角拉的多出对应度数*/
- }
- <body>
- <div>hello</div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: red;
- line-height: 200px;
- text-align: center;
- border: 1px solid black;
- font-size: 50px;
- margin: 0 auto;
- transform: skew(30deg,30deg);
- /* 抓着右下角往右下分别拉动 */
- }
- </style>
相同点:都是随着时间改变元素的属性值
不同点:transition需要触发一个事件(hover事件或click事件)才会随着时间改变CSS属性;而animation在不需要触发任何事件的情况下也可以显示的随着时间的变化来改变元素的属性值从而达到一个动画的效果,css3的animation就需要明确的动画属性值

animation: ppt2 2s linear 1s 10 alternate;
animation-duration: 2s;
animation-delay: 5s;
animation-timing-function: linear;
animation-iteration-count: infinite;
animation-direction: alternate;
animation-play-state: running;
- <body>
- <div></div>
- </body>
- <style>
- div{
- width: 200px;
- height: 200px;
- background: red;
- animation: ppt2 2s linear 1s 10 alternate-reverse;
- }
- /* 声明动画第一种写法 */
- @keyframes ppt1 {
- from{
- width: 200px;
- height: 200px;
- background: red;
- }
- to{
- width: 400px;
- height: 600px;
- background: blue;
- }
- }
- /* 第二种写法,可以支持多种状态 */
- @keyframes ppt2 {
- 0%{
- width: 200px;
- height: 200px;
- background: red;
- }
- 50%{
- width: 400px;
- height: 600px;
- background: blue;
- }
- 100%{
- width: 800px;
- height: 800px;
- background: pink;
- }
- }
- </style>
animation-fill-mode: forwards;
属性值
注意:动画填充模式不是无限循环时使用
animation: run 3s steps(1,start);
这里的1表示从第一帧到第二帧只需要1步,没有多余过渡
注意:
transform-style: preserve-3d;
transform:translateZ(100px);
transform: translate3d(0,0,100px);
3个参数分别为x,y,z轴移动
perspective:900px;
设置景深——视觉感受:近大远小,上面的意思是对象距离我们900px
向z轴移动100px
- <body>
- <div class="box">
- <div class="center">
- </div>
- </div>
- </body>
- <style>
- .box{
- width: 500px;
- height: 500px;
- border: 5px solid black;
- transform-style: preserve-3d;
- position: relative;
- margin: 0 auto;
- perspective:900px;
- }
- .center{
- position: absolute;
- width: 200px;
- height: 200px;
- background: red;
- left: 50%;
- top: 50%;
- margin-left: -100px;
- margin-top: -100px;
- transform: translate3d(0,0,700px);
- }
- </style>
注意:z轴上物体离我们越来越近,视觉感受就是物体越来越大
transform: rotate3d(1,0,0,90deg);
注意:前面3个值为0/1分别对应x,y,z轴,后面跟个角度,代表每个轴旋转多少度
- <body>
- <div class="box">
- <div class="center">
- </div>
- </div>
- </body>
- <style>
- .box{
- width: 500px;
- height: 500px;
- border: 5px solid gray;
- display: flex;
- transform-style:preserve-3d ;
- transition: all 2s linear;
- background: pink;
- }
- .center{
- margin: auto;
- width: 200px;
- height: 200px;
- background: red;
- transition: all 2s linear;
- }
- div .center:hover{
- transform: rotate3d(1,0,0,90deg);
- }
- div:hover{
- transform: rotateY(90deg);
- }
- </style>
transform: scale3d(1,1,10);
里面有3个值分别对应x,y,z轴放大对应的倍数
- <body>
- <div class="box">
- <div class="center">
- </div>
- </div>
- </body>
- <style>
- .box{
- width: 500px;
- height: 500px;
- border: 5px solid gray;
- display: flex;
- transform-style:preserve-3d ;
- perspective: 900px;
- }
- .center{
- margin: auto;
- width: 200px;
- height: 200px;
- background: red;
- transition: all 2s linear;
- }
- div .center:hover{
- transform: scale3d(1,1,10) rotateX(90deg);
- }
- </style>