本文主要包含css,居中等相关知识,佚名 希望在学习及工作中可以帮助到您
在前端面试中,大都会问你div居中的方法:
文笔不好,就随便寥寥几句话概括了,
不过以后文笔肯定会变得更好一些的。
今天我们就细数一下几种方法:
1,使用position:absolute,设置left、top、margin-left、margin-top的属性
- .one{
- position:absolute;
- width:200px;
- height:200px;
- top:50%;
- left:50%;
- margin-top:-100px;
- margin-left:-100px;
- background:red;
- }
这种方法基本浏览器都能够兼容,不足之处就是需要固定宽高。
2,使用position:fixed,同样设置left、top、margin-left、margin-top的属性
- .two{
- position:fixed;
- width:180px;
- height:180px;
- top:50%;
- left:50%;
- margin-top:-90px;
- margin-left:-90px;
- background:orange;
- }
大家都知道的position:fixed,IE是不支持这个属性的
3,利用position:fixed属性,margin:auto这个必须不要忘记了。
- .three{
- position:fixed;
- width:160px;
- height:160px;
- top:0;
- rightright:0;
- bottombottom:0;
- left:0;
- margin:auto;
- background:pink;
- }
4,利用position:absolute属性,设置top/bottom/right/left
- .four{
- position:absolute;
- width:140px;
- height:140px;
- top:0;
- right