div{position:fixed;margin:auto;left:0;right:0;top:0;bottom:0;width:200px;height:150px;}如果只需要左右居中,那么把 bottom:0; 或者 top:0; 删掉即可<style type="text/css">.dialog{ position: fixed; _position:absolute; z-index:1; top: 50%; left: 50%; margin: -141px 0 0 -201px; width: 400px; height:280px; border:1px solid #CCC; line-height: 280px; text-align:center; font-size: 14px; background-color:#F4F4F4; overflow:hidden;} </style><div class="dialog">我是在窗口正中央的,呵呵!</div>设置的技巧全部在这里:.dialog{ position: fixed; _position:absolute; /* hack for IE6 */ z-index:1; top: 50%; left: 50%; margin: -141px 0 0 -201px; width: 400px; height:280px; border:1px solid #CCC; line-height: 280px; text-align:center; font-size: 14px; background-color:#F4F4F4; overflow:hidden;}设置 position: fixed; _position:absolute;
以上内容是小编给大家分享的CSS中position属性之fixed实现div居中的全部叙述,希望大家喜欢。