1)静态定位 css
position:static(默认)html
2)相对定位浏览器
position:relative(要配合top、bottom、left、right等属性来使用)ide
3)绝对定位布局
position:absolute测试
<html>
元素或其最近的“positioned”祖先元素,一个“positioned”元素是指 position 值不是 static
的元素。4)固定定位字体
position:fixed(要配合top、bottom、left、right等属性来使用)flex
5)堆叠顺序spa
z-indexcode
几种定位的区别:https://baijiahao.baidu.com/s?id=1631432397252663686&wfr=spider&for=pc
http://zh.learnlayout.com/position.html
属性 | 备注 | 值 |
flex-direction | 指定主轴的方向 | row/column,默认是row |
flex-wrap | 换行 | wrap/wrap-reverse,flex-direction和flex-wrap能够合并为flex-flow: row wrap; |
flex | 三个属性的缩写,flex-grow/flex-shrink /flex-basis | flex:0 1 auto(默认) |
align-items | 控制在交叉轴的位置 | stretch(默认)/center/flex-start/flex-end |
justify-content | 控制在主轴上的位置 | flex-start(默认)/flex-end/center/space-around(均匀分布)/space-between(不会在两端留下任何空间) |
order | flex项排序 | 数字,默认0,越大越后面 |
3.1.1 流动模型
两个典型的特征:
3.1.2 浮动模型
任何元素默认不能浮动,使用css定义浮动
div{float:left;} div{float:right;}
3.1.3 层模型
三种形式:
3.1.4 z-index
利用该属性,能够改变元素相互覆盖的顺序
3.2.1 float多个特性
3.2.2 清除浮动
3.3.1 一列布局
固定宽和高,设置margin:0 auto来水平居中
3.3.2 二列布局
最多见的就是使用float来实现,但会出现文本环绕等效果,须要及时清除浮动
3.3.3 三列布局
两侧定宽中间自适应
首先设置父级元素的宽度,能够左左右设置浮动。而后中间设置margin调整间距。 也能够都设置成左浮动,设置margin,调整间距。一样注意清除浮动的影响!
<div class="main">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
.main{ width: 100%; background: red; overflow: hidden; } .left{ background: yellow; float: left; width: 100px; } .middle{ background: rosybrown; float: left; width: cacl(100%-200px); } .right{ background: green; float: right; width: 100px%; }
或者为父级元素设置relative属性,再为子元素设置absolute属性,再分别定位,调整间距
<div class="parent" style="background-color: lightgrey;"> <div class="left" style="background-color: lightblue;"> <p>left</p> </div> <div class="center" style="background-color: pink;"> <p>center</p> <p>center</p> </div> <div class="right" style="background-color: lightgreen;"> <p>right</p> </div> </div>
<style> p{margin: 0;} .parent{position: relative;height:40px;} .left,.right,.center{position: absolute;} .left{left: 0;width:100px;} .right{right: 0;width: 100px;} .center{left: 120px; right: 120px;} </style>
3.3.4 混合布局
在一列布局的基础上,保留top和foot部分,将中间的main部分改形成两列或三列布局,小的模块能够再逐级同理划分。
<div class="top"></div> <div class="main"> <div class="left">left</div> <div class="right">right</div> </div> <div class="footer"></div>
.top{ height: 100px; background: teal; } .footer{ height: 100px; background: wheat; } .main{ width: 100%; background: red; overflow: hidden; } .left{ background: yellow; float: left; width: 50%; } .right{ background: green; float: right; width: 50%; }
3.4.1 水平居中
(1)行内元素的水平居中
若是被设置元素为文本、图片等行内元素时,在父元素中设置text-align:center实现行内元素水平居中,将子元素的display设置为inline-block,使子元素变成行内元素
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> <style> .parent{text-align: center;} .child{display: inline-block;} </style>
(2)块状元素的水平居中(定宽)
当被设置元素为定宽块级元素时用 text-align:center 就不起做用了。能够经过设置“左右margin”值为“auto”来实现居中的。
<div class="parent" style="background-color: gray;"> <div class="child" style="background-color: lightblue;">DEMO</div> </div> .child{ width: 200px; margin: 0 auto; }
(3)块状元素的水平居中(不定定宽)
在实际工做中咱们会遇到须要为“不定宽度的块级元素”设置居中,好比网页上的分页导航,由于分页的数量是不肯定的,因此咱们不能经过设置宽度来限制它的弹性。
能够直接给不定宽的块级元素设置text-align:center来实现,也能够给父元素加text-align:center 来实现居中效果。
当不定宽块级元素的宽度不要占一行时,能够设置display 为 inline 类型或inline-block(设置为 行内元素 显示或行内块元素)
<div class="container"> <ul> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> </ul> </div> .container{text-align:center;background: beige} .container ul{list-style:none;margin:0;padding:0;display:inline-block;} .container li{margin-right:8px;display:inline-block;}
3.4.2 垂直居中
和水平居中同样,这里要讲垂直居中,首先设定两个条件即父元素是盒子容器且高度已经设定
场景1:子元素是行内元素,高度是由其内容撑开的
这种状况下,须要经过设定父元素的line-height为其高度来使得子元素垂直居中
<div class="wrap line-height"> <span class="span">111111</span> </div> .wrap{ width:200px ; height: 300px; line-height: 300px; border: 2px solid #ccc; } .span{ background: red; }
场景2:子元素是块级元素可是子元素高度没有设定
在这种状况下其实是不知道子元素的高度的,没法经过计算获得padding或margin来调整,可是仍是存在一些解法。
经过给父元素设定display:table-cell;vertical-align:middle来解决
<div class="wrap"> <div class="non-height ">11111</div> </div> .wrap{ width:200px ; height: 300px; border: 2px solid #ccc; display: table-cell; vertical-align: middle; } .non-height{ background: green; }
场景3:子元素是块级元素且高度已经设定
计算子元素的margin-top或margin-bottom,计算方法为父(元素高度-子元素高度)/2
<div class="wrap "> <div class="div1">111111</div> </div> .wrap{ width:200px ; height: 300px; border: 2px solid #ccc; } .div1{ width:100px ; height: 100px; margin-top: 100px; background: darkblue; }
3.4.3 水平垂直居中
(1)水平对齐+行高
text-align + line-height实现单行文本水平垂直居中
<style> .test{ text-align: center; line-height: 100px; } </style> <div class="test" style="background-color: lightblue;width: 200px;">测试文字</div>
(2)水平+垂直对齐
<style> .parent{ display: table-cell; text-align: center; vertical-align: middle; } .child{ display: inline-block; } </style>
<div class="parent" style=" width:200px; height:100px;"> <div class="child" style="">测试文字</div> </div>
<div class="parent" style="background-color: gray; width:200px; "> <img class="child" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif" width="50%" alt="test"> </div>
<style> .parent{ text-align: center; line-height: 100px; font-size: 0; } .child{ vertical-align: middle; } </style>
(3)相对+绝对定位
使用absolute,利用绝对定位元素的盒模型特性,在偏移属性为肯定值的基础上,设置margin:auto
<style> .parent{ position: relative; } .child{ position: absolute; top: 0; left: 0; right: 0; bottom: 0; height: 50px; width: 80px; margin: auto; } </style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; "> <div class="child" style="background-color: lightblue;">测试文字</div> </div>