这类css合理布局平常用的较为多,也是招聘面试题常出的1个题,在网上1搜1大丢,但是還是想自身总结1下。
这类方式较为多,本文只总结在其中的几种,便于加深印象。
实际效果图都为这个:
方式1:position加margin
XML/HTML Code拷贝內容到剪贴板
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { width: 200px; height: 200px; background: yellow; position: relative;
- } .wrap .center { width: 100px; height: 100px; background: green; margin: auto; position: absolute; left: 0; rightright: 0; top: 0; bottombottom: 0;
- }
适配性:流行访问器均适用,IE6不适用
方式2:diaplay:table-cell
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap{ width: 200px; height: 200px; background: yellow; display: table-cell; vertical-align: middle; text-align: center;
- } .center{ display: inline-block; vertical-align: middle; width: 100px; height: 100px; background: green;
- }
-
适配性:因为display:table-cell的缘故,IE67兼容问题
方式3:position加 transform
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { position: relative; background: yellow; width: 200px; height: 200px;} .center { position: absolute; background: green; top:50%; left:50%; -webkit-transform:translate(⑸0%,⑸0%); transform:translate(⑸0%,⑸0%); width: 100px; height: 100px;
- }
-
适配性:ie9下列不适用 transform,手机上端主要表现的较为好。
方式4:flex;align-items: center;justify-content: center
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify-content: center;
- } .center { background: green; width: 100px; height: 100px;
- }
-
挪动端首选
方式5:display:flex;margin:auto
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; display: flex;
- } .center { background: green; width: 100px; height: 100px; margin: auto;
- }
-
挪动端首选
方式6:纯position
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; position: relative;
- } .center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;
-
- } .center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;
- }
-
适配性:可用于全部访问器
方式6中的方式1测算公式以下:
子元素(conter)的left值测算公式:left=(父元素的宽 - 子元素的宽 ) / 2=(200⑴00) / 2=50px;
子元素(conter)的top值测算公式:top=(父元素的高 - 子元素的高 ) / 2=(200⑴00) / 2=50px;
方式2测算公式:
left值固定不动为50%;
子元素的margin-left= -(子元素的宽/2)=⑴00/2= ⑸0px;
top值也1样,固定不动为50%
子元素的margin-top= -(子元素的高/2)=⑴00/2= ⑸0px;
方式7:适配低版本号访问器,不固定不动宽高
XML/HTML Code拷贝內容到剪贴板
-
- <div class="table">
- <div class="tableCell">
- <div class="content">不固定不动宽高,自融入</div>
- </div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .table { height: 200px; width: 200px; display: table; position: relative; float:left; background: yellow;
- } .tableCell { display: table-cell; vertical-align: middle; text-align: center;
- *position: absolute; padding: 10px;
- *top: 50%;
- *left: 50%;
- } .content {
- *position:relative;
- *top: ⑸0%;
- *left: ⑸0%; background: green;
- }
-
临时总结上面的7种,这类方式太多,实际上要是习惯性了在其中的1两种也就够用了。
总结
假如是挪动端,那末用方式4和方式5是较为便捷的。并且适用不固定不动宽高的状况,快、准、狠
也便是用 flex; align-items: center; justify-content: center;
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; display: flex; align-items: center; justify-content: center;
- } .center { background: green; width: 100px; height: 100px;
- }
-
或display:flex;margin:auto;
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; display: flex;
- } .center { background: green; width: 100px; height: 100px; margin: auto;
- }
-
假如是PC端,要考虑到适配性的话。方式6是非常好滴,也便是纯position。
XML/HTML Code拷贝內容到剪贴板
-
- <div class="wrap">
- <div class="center"></div>
- </div>
-
CSS Code拷贝內容到剪贴板
- .wrap { background: yellow; width: 200px; height: 200px; position: relative;
- } .center { background: green; position: absolute; width: 100px; height: 100px; left: 50px; top: 50px;
-
- } .center { background: green; position: absolute; width: 100px; height: 100px; left: 50%; top: 50%; margin-left:-50px; margin-top:-50px;
- }
-
假如PC端正中间的元素高宽比不固定不动,那末就用方式7便可,编码就不拷贝了。
这类css元素竖直的假如真的要总结起来,应当有10几210几种。但是也没必要所有把握吧,要是大约掌握1些,用起来沒有不良反应就行。
以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。