CSS篇
垂直居中
方式一
<!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; 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> <style> .center { width: 18em; height: 10em; position: absolute; background: orange; text-align: center; top: 50%; left: 50%; margin-left: -9em; margin-top: -5em; /* 或者 transform: translate(-9em, -5em); */ } </style> </head> <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> <style> .wrapper { text-align: center; height: 200px; background: orange; } .center { display: inline-block; width: 100px; height: 100px; } </style> </head> <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>
布局
绝对定位
<!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> html, body { margin: 0; padding: 0; } * { margin: 0; padding: 0; } aside { position: absolute; width: 300px; min-height: 100px; } left { left: 0; background-color: red; } .right { right: 0; background-color: blue; } .center { position: absolute; left: 300px; right: 300px; background-color: orange; } </style> </head> <body> <aside class="left"></aside> <aside class="right"></aside> <main class="center"> <h1>绝对定位解决方案</h1> <p>左右区域分别postion:absolute,固定到左右两边</p> <p>中间区域postion:absolute;left:300px; right: 300px</p> <p>给总的宽度加一个min-width,不然缩小窗口会有毛病</p> </main> </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> html, body { margin: 0; padding: 0; } .wrapper { display: table; width: 100%; } .left, .main, .right { min-height: 100px; display: table-cell; } .left { width: 100px; background-color: red; } .main { background-color: orange; } .right { width: 200px; background-color: blue; } </style> </head> <body> <div class="wrapper"> <aside class="left"></aside> <main class="main"> <h1>表格布局</h1> <p>父元素display:table并且宽度为100%</p> <p>每一个子元素display:table-cell</p> <p>左右两侧添加宽度,中间不加宽度</p> </main> <aside class="right"></aside> </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> html, body { padding: 0; margin: 0; } .left, .right, .main { height: 200px; } .left { float: left; width: 100px; background-color: blue; } .main { background-color: seagreen; width: 100%; } .right { float: right; width: 100px; background-color: red; } </style> </head> <body> <aside class="left"></aside> <aside class="right"></aside> <main class="center"> <h1>浮动解决方案</h1> <p>方法:left和right都写在center前面,并且分别左右浮动</p> <p>中间的这个div因为是块级元素,所以在水平方向按照他的包容快自动撑开</p> <p>简单,但是中心部分过长下面会溢出,然后文字就会跑到两边去。</p> </main> </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> html, body { padding: 0; margin: 0; } .wrapper { display: grid; width: 100%; grid-template-columns: 100px 1fr 100px; } .left { background-color: red; } .center { background-color: orange; } .right { background-color: blue; } </style> </head> <body> <div class="wrapper"> <aside class="left"></aside> <main class="center"> <h1>网格布局</h1> <p>父元素display:grid</p> </main> <aside class="right"></aside> </div> </body> </html>
三栏-flex布局
<!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>flex布局</title> <style> html, body { padding: 0; margin: 0; } .wrapper { display: flex; height: 200px; } .left { width: 100px; background-color: seagreen; } .right { width: 200px; background-color: sienna; } .main { flex: 1; background-color: springgreen; } </style> </head> <body> <div class="wrapper"> <aside class="left"></aside> <main class="main"> <h1>flex布局</h1> </main> <aside class="right"></aside> </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> html, body { padding: 0; margin: 0; } .left { width: 100px; background-color: springgreen; } .right { width: 200px; background-color: steelblue; } .main { width: 100%; background-color: red; } /* 关键代码 */ .left, .right, .main { float: left; position: relative; height: 200px; } .left { margin-left: -100%; left: -100px; } .container { padding-left: 100px; padding-right: 200px; } .right { margin-left: -200px; right: -200px; } </style> </head> <body> <div class="container"> <div class="main"></div> <div class="left"></div> <div class="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> html, body { padding: 0; margin: 0; } .left, .right, .main { min-height: 200px; } .left { width: 200px; background-color: thistle; } .main { background: #999; width: 100%; } .right { width: 300px; background-color: violet; } /*关键代码*/ .left, .right, .main { float: left; } .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>