css让容器水平垂直居中的7种方式
副标题[/!--empirenews.page--]
方法一:position加margin
XML/HTML Code复制内容到剪贴板
<div class="wrap"> <div class="center"></div> </div>
CSS Code复制内容到剪贴板
/**css**/ .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不支持 方法二:diaplay:table-cell
XML/HTML Code复制内容到剪贴板
<!-- html --> <div class="wrap"> <div class="center"></div> </div>
CSS Code复制内容到剪贴板
/*css*/ .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不兼容 方法三:position加 transform
XML/HTML Code复制内容到剪贴板
<!-- html --> <div class="wrap"> <div class="center"></div> </div>
CSS Code复制内容到剪贴板
/* css */ .wrap { position: relative; background: yellow; width: 200px; height: 200px;} .center { position: absolute; background: green; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); transform:translate(-50%,-50%); width: 100px; height: 100px; }
兼容性:ie9以下不支持 transform,手机端表现的比较好。 方法四:flex;align-items: center;justify-content: center
XML/HTML Code复制内容到剪贴板
<!-- html --> <div class="wrap"> (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |