Flex是Flexible Box的縮寫”彈性布局”是目前主流的布局方式,適用PC+移動自適應(yīng)布局,使垂直居中更容易實(shí)現(xiàn);justify-content屬性,align-items屬性來控制水平排序與垂直居中
運(yùn)行結(jié)果:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
- <title>通高科技</title>
- <style>
- .a{
- /* background-color: green; */
- display: flex;/* 使用彈性布局 */
- align-items:center;/* 垂直居中 */
- justify-content:center;/* 水平居中 */
- }
- .div{
- width: 100px;
- height: 100px;
- background-color: aqua;
- margin-left: 5px;
- }
- .div1{
- height: 50px;;
- }
- </style>
- </head>
- <body>
- <div class="a">
- <p>根據(jù)窗口自動水平居中</p>
- <div class="div">1</div>
- <div class="div">2</div>
- <div class="div div1">3垂直居中</div>
- <div class="div">4</div>
- <div class="div">5</div>
- </div>
- </body>
- </html>
display: block 與display:inline-block 區(qū)別在元素之后不添加換行符,因此該元素可以位于其他元素旁邊。
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
- <title>通高科技</title>
- <style>
- .a{
- display: inline;
- width: 100px;
- height: 100px;
- padding: 5px;
- border: 1px solid blue;
- background-color: yellow;
- }
- .b{
- display: inline-block;
- width: 100px;
- height: 100px;
- padding: 5px;
- border: 1px solid blue;
- background-color: yellow;
- }
- .c{
- display: block;
- width: 100px;
- height: 100px;
- padding: 5px;
- border: 1px solid blue;
- background-color: yellow;
- }
- </style>
- </head>
- <body>
- <p>display: inline-block和display: inline區(qū)別;<span class="a">inline設(shè)置屬性高度</span></p>
- <p>display: inline-block和display: inline區(qū)別;<span class="b">inline-block設(shè)置屬性高度</span></p>
- <p>display: inline-block和display: inline區(qū)別;<span class="c">block設(shè)置屬性高度</span></p>
- </body>
- </html>
運(yùn)行結(jié)果:
display: inline 行元素是p、span、a標(biāo)簽的默認(rèn)標(biāo)簽使用不多,display: inline-block和display: inline對比 display: inline-block允許在元素上設(shè)置寬度和高度。
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=yes">
- <title>通高科技</title>
- <style>
- .a{
- display: inline;
- width: 100px;
- height: 100px;
- padding: 5px;
- border: 1px solid blue;
- background-color: yellow;
- }
- .b{
- display: inline-block;
- width: 100px;
- height: 100px;
- padding: 5px;
- border: 1px solid blue;
- background-color: yellow;
- }
- </style>
- </head>
- <body>
- <p>display: inline-block和display: inline區(qū)別;<span class="a">inline設(shè)置屬性高度</span></p>
- <p>display: inline-block和display: inline區(qū)別;<span class="b">inline-block設(shè)置屬性高度</span></p>
- </body>
- </html>
運(yùn)行結(jié)果: