<style> * { margin: 0; padding: 0; } .layout { margin-top: 10px; } .layout div{ min-height: 100px; } </style> <body> <!--1.浮动的方式来实现布局--> <section class="layout float"> <style> .layout.float .left { float: left; width: 300px; background-color: #48adff; } .layout.float .main { background-color: #ff4344; } </style> <article class="left-main"> <div class="left"></div> <div class="main"> <h1>浮动两栏布局</h1> <p>两栏布局的中间部分</p> <p>两栏布局的中间部分</p> </div> </article> </section> <!--2.定位的方式来实现布局--> <section class="layout absolute"> <style> .layout.absolute .left-main { width: 100%; } .layout.absolute .left { left : 0; width: 300px; background-color: #48adff; position: absolute; } .layout.absolute .main { /*默认是以正常文档流的方式来展示的*/ background-color: #ff4344; margin-left: 300px; right: 0; } </style> <article class="left-main"> <div class="left"></div> <div class="main"> <h1>绝对定位两栏布局</h1> <p>两栏布局的中间部分</p> <p>两栏布局的中间部分</p> </div> </article> </section> <!--3.flex布局的实现--> <section class="layout flex"> <style> .layout .left-main { display: flex; } .layout .left { width: 300px; background-color: #48adff; } .layout .main { flex: 1; background-color: #ff4344; } </style> <article class="left-main"> <div class="left"></div> <div class="main"> <h1>flex两栏布局</h1> <p>两栏布局的中间部分</p> <p>两栏布局的中间部分</p> </div> </article> </section> <!--4.table布局的实现--> <section class="layout table"> <style> .layout .left-main { display: table; width: 100%; } .layout .left { display : table-cell; width: 300px; background-color: #48adff; } .layout .main { background-color: #ff255f; } </style> <article class="left-main"> <div class="left"></div> <div class="main"> <h1>table两栏布局</h1> <p>两栏布局的中间部分</p> <p>两栏布局的中间部分</p> </div> </article> </section> <!--5.grid布局--> <section class="layout grid"> <style> .layout.grid .left-main { display: grid; } .layout.grid .left-main { grid-template-rows : 100px; /*按照顺序指定盒子的宽度*/ grid-template-columns : 300px auto; } .layout.grid .left { background-color: #48adff; } .layout.grid .main { background-color: #ff4344; } </style> <article class="left-main"> <div class="left"></div> <div class="main"> <h1>grid两栏布局</h1> <p>两栏布局的中间部分</p> <p>两栏布局的中间部分</p> </div> </article> </section> </body>
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>实现三栏水平布局之圣杯布局</title> <style type="text/css"> /*基本样式*/ .left, .right, .main { min-height: 300px; } .left { width: 200px; background-color:thistle; } .main { background-color: #999; } .right { width: 300px; background-color: violet; } /* 圣杯布局关键代码 */ .left, .main, .right { float: left; position: relative; } .main { width: 100%; } .container { padding-left: 200px; padding-right: 300px; } .left { margin-left: -100%; left: -200px; } .right { margin-left: -300px; right: -300px; } </style> </head> <body> <div class="container"> <div class="main">main</div> <div class="left">left</div> <div class="right">right</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>双飞翼布局</title> <style> .left, .right, .main { min-height: 200px; } .left { width: 200px; background-color: thistle; } .main { background: #999; } .right { width: 300px; background-color: violet; } /* 双飞翼布局重点 */ .left, .main, .right { float: left; } .main { width: 100%; } .main-inner { margin-left: 200px; margin-right: 300px; } .left { margin-left: -100%; } .right { margin-left: -300px; } </style> </head> <body> <div class="main"><div class="main-inner">中心区</div></div> <div class="left">left</div> <div class="right">right</div> </body> </html>
<style type="text/css"> p{ font-size:11pt; color:#363636; text-indent:2em; } .parent{ width:500px; height:150px; margin-top:20px; margin-left:20px; border:solid 1px #555555; background:#aaaaaa; } .parent div{ width:100px; height:80px; float:left; background:#708090; border:dashed 1px #008B8B; font-size:12pt; font-weight:bold; color:#104E8B; } </style> </head> <body> <!--相对定位!--> <h2>relative</h2> <p>相对定位是一个很是容易掌握的概念。若是对一个元素进行相对定位,它将出如今它所在的位置上。而后,能够经过设置垂直或水平位置,让这个元素“相对于”它的起点进行移动。</p> <div class="parent"> <div>child 1</div> <div style="position:relative;left:20px;top:20px;">child 2</div> <div>child 3</div> </div> <!--绝对定位!--> <h2>absolute</h2> <p>绝对定位的元素的位置相对于最近的已定位祖先元素,若是元素没有已定位的祖先元素,那么它的位置相对于最初的包含块。 对于定位的主要问题是要记住每种定位的意义。</p> <p>绝对定位是“相对于”最近的已定位祖先元素,若是不存在已定位的祖先元素,那么“相对于”最初的包含块。因此若是要设定元素与其父元素的绝对位置定位就必须设定父元素的定位。</p> <p>注释:根据用户代理的不一样,最初的包含块多是画布或 HTML 元素。</p> <div class="parent" style="position:relative;"<!--若是该处不定位,那么child5框的定位是相对于最初的包含块!-->> <div>child 4</div> <div style="position:absolute;left:20px;top:20px;">child 5</div> <div>child 6</div> </div> <!--相对定位!--> <h2>fixed</h2> <p>元素框的表现相似于将 position 设置为 absolute,不过其包含块是视窗自己。</p> <div class="parent"> <div>child 7</div> <div style="position:fixed;right:20px;top:20px;">child 8</div> <div>child 9</div> </div> <!--相对定位!--> <h2>static</h2> <p>元素框正常生成。块级元素生成一个矩形框,做为文档流的一部分,行内元素则会建立一个或多个行框,置于其父元素中。</p> <div class="parent"> <div>child 10</div> <div style="position:static;right:20px;top:20px;">child 11</div> <div>child 12</div> </div> </body>
<style> .father-green { width:500px; height:300px; background-color:green; } .son-red { width:200px; height:100px; background-color:red; display:inline-block; } .subson-yellow { height:50px; width:200px; background-color: yellow; } .son-purple { width: 200px; height:100px; background-color:purple; display:inline-block; margin-left:-50px; } .mather-pink { width: 300px; height:100px; background-color:pink; } .daughter-blue { width:100px; height:50px; background-color:blue; margin-top:-20px; } </style> <body> <div class="father-green"> <div class="son-red"> <div class="subson-yellow"> 我是孙子辈的我是孙子辈的我是孙子辈的 </div> </div> <div class="son-purple"> 我是第二个子元素 </div> </div> <div class="mather-pink"><div class="daughter-blue">daughter-blue</div> </div> </body>
[!NOTE]css
- 层叠上下文能够包含在其余层叠上下文中,而且一块儿组建了一个有层级的层叠上下文
- 每一个层叠上下文彻底独立于它的兄弟元素,当处理层叠时只考虑子元素,这里相似于BFC
- 每一个层叠上下文是自包含的:当元素的内容发生层叠后,整个该元素将会在父级叠上下文中按顺序进行层叠
[!NOTE]
总结:层叠上下文(border/background)< 负z-index < block块状盒子 < 浮动的盒子 < inline/inline-block水平盒子 < z-index:auto 或者 z-index:0 < 正z-index(定位并设定了正的z-index值,z-index值越大 层级越高)html
<style> *{ margin: 0; padding: 0; } ul{ list-style: none; } li{ display: inline-block; width: 100px; height: 100px; background: red; } </style> <ul> <li>111</li> <li>222</li> <li>333</li> <li>444</li> <li>555</li> </ul>
BFC:浮动元素和绝对定位元素,非块级盒子的块级容器(例如 inline-blocks, table-cells, 和 table-captions),以及overflow值不为“visiable”的块级盒子,都会为他们的内容建立新的BFC(块级格式上下文)。它是指一个独立的块级渲染区域,只有Block-level BOX参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与区域外部无关web
<div class="main left">.main{float:left;}</div> <div class="side left">.side{float:right;}</div> <div style="clear:both;"></div> </div> <div class="footer">.footer</div>
<div class="wrap" id="float3" style="overflow:hidden; *zoom:1;"> <h2>3)父元素设置 overflow </h2> <div class="main left">.main{float:left;}</div> <div class="side left">.side{float:right;}</div> </div> <div class="footer">.footer</div>
<style type="text/css"> .clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } .clearfix { display: inline-block; *zoom:1; } /* for IE/Mac */ </style> <!--[if IE]> <style type="text/css"> .clearfix {zoom: 1;/* triggers hasLayout */ display: block;/* resets display for IE/Win */} </style> <![endif]-->
.clearfix:before,.clearfix:after{ content:""; display:table; } .clearfix:after{ clear:both; } .clearfix{ *zoom:1; }
<meta name="viewport" content="width=device-width,initial-scale=1">
rem是什么(CSS3新增),初始值:1rem=16px?
rem(font size of the root element)是指相对于根元素的字体大小的单位。简单的说它就是一个相对单位浏览器
[!NOTE]
rem(1rem = 16px) / viewport(固定宽度) / media query(屏幕大小自适应)app
隐藏(移动端隐藏元素) 折行(横排变纵排) 自适应(留下自适应的空间)(media query)ide
定宽布局(版心)布局
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .wrapper { overflow: hidden; width: 1000px; height: 500px; background: #999; } .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; /* 1vh = 1% * 视口高度 */ margin: 50vh auto; transform: translateY(-50%); } </style> <body> <div class="wrapper"> <div class="center"> 基于视口的垂直居中<br /> 不要求原生有固定的宽高。<br /> 可是这种居中是在整个页面窗口内居中,不是基于父元素<br /> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; position: absolute; top: 50%; left: 50%; margin-left: -9rem; margin-top: -5rem; } </style> <body> <div class="center"> 要求原生有固定的宽高。<br/> position: absolute;<br/> top和left 为 50%;<br/> margin上为高的一半<br/> margin左为宽的一半<br/> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; position: absolute; top: calc(50% - 5em); left: calc(50% - 9em); } </style> <body> <div class="center"> 要求原生有固定的宽高。<br/> position: absolute;<br/> top 为 calc(50% 剪 一半高) left 为 calc(50% 剪 一半宽) </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style> <body> <div class="center"> 不要求原生有固定的宽高。<br/> position: absolute;<br/> top和left 为 50%;<br/> transform: translate(-50%, -50%); </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .wrapper { width: 1000px; height: 600px; background: #999; display: flex; } .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; margin: auto; } </style> <body> <div class="wrapper"> <div class="center"> 使用flex居中<br/> 父元素 display: flex; <br/> 居中块: margin: auto; </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>垂直居中</title> </head> <style> .wrapper { width: 1000px; height: 600px; background: #999; display: flex; /* 盒子横轴的对齐方式 */ justify-content: center; /* 盒子纵轴的对齐方式 */ align-items: center; } .center { width: 18em; height: 10em; text-align: center; background-color: orange; color: #fff; } </style> <body> <div class="wrapper"> <div class="center"> 使用flex居中<br/> 父元素 display: flex; <br/> justify-content: center;<br/> align-items: center;<br/> </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>水平元素居中</title> </head> <style> .wrapper { text-align: center; height: 1000px; } .center { display: inline-block; width: 500px; height: 200px; background: orange; } </style> <body> <div class="wrapper"> <div class="center">若是须要居中的元素为常规流中 inline / inline-block 元素,为父元素设置 text-align: center;</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>水平元素居中</title> </head> <style> .wrapper { width: 100%; height: 500px; text-align: center; /* 3 */ } .center { width: 500px; text-align: left; margin: 0 auto; background-color: orange; } </style> <body> <div class="wrapper"> <div class="center"> 父元素上设置 text-align: center;<br /> 居中元素上margin 为 auto。 </div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>水平元素居中</title> </head> <style> .wrapper { width: 80%; height: 500px; background: #888; position: relative; } .center { width: 500px; position: absolute; left: 50%; margin-left: -250px; background-color: orange; } </style> <body> <div class="wrapper"> <div class="center">若是元素positon: absolute; 那么 0)设置父元素postion: relative 1)为元素设置宽度,2)偏移量设置为 50%,3)偏移方向外边距设置为元素宽度一半乘以-1</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta http-equiv="X-UA-Compatible" content="ie=edge" /> <title>水平元素居中</title> </head> <style> .wrapper { width: 80%; height: 500px; background: #888; } .center { width: 500px; position: relative; left: 50%; margin-left: -250px; background-color: orange; } </style> <body> <div class="wrapper"> <div class="center">若是元素positon: relative。 那么 1)为元素设置宽度,2)偏移量设置为 50%,3)偏移方向外边距设置为元素宽度一半乘以-1</div> </div> </body> </html>
top和left 为 50%, margin的left和top为自身宽高一半post
.center { position: absolute; top: 50%; left: 50%; margin-left: -9rem; margin-top: -5rem; }
top和lefe为父元素一半剪自身一半字体
.center { position: absolute; top: calc(50% - 5em); left: calc(50% - 9em); }
使用CSS3 的 transform
将位置在中心点平移自身宽高一半flex
.center { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
.wrapper { display: flex; } .center { margin: auto; }
父元素指定子元素居中。
.wrapper { display: flex; align-items: center; justify-content: center; }
不要求原生有固定的宽高,可是这种居中是在整个页面窗口内居中,不是基于父元素
.center{ margin: 50vh auto; transform: translateY(-50%); }