关于left:50%的应用技巧

让一个单独div居中有多种方法:

1.margin:0px auto;

2.设置position:fixed或者relative;left:50%;这是水平偏右,再将margin-left设置为-n;(n一般为div长度的一半);这样就可以啦

如果有两个层,使用这种方法可以使他在居中位置,然后通过margin属性设置位置,参考代码:

<style type="text/css">
*{margin:0px;padding:0px;}
.box{background-color:#99FF33;800px;height:800px;margin:0px auto;}
.floor{100px;height:500px;background-color:#CCFFCC;position:fixed;top:200px;left:50%;margin-left:400px;}
</style>
<body>
<div class="box">hha fnaiouhs</div>
<div class="floor">tttt</div>
</body>
</html>

原文地址:https://www.cnblogs.com/jia-hui/p/5502907.html