我想提出一个position: fixed;
弹出框以屏幕为中心,具备动态宽度和高度。 我使用margin: 5% auto;
为了这。 没有position: fixed;
它水平居中,但不垂直。 添加position: fixed;
,它甚至不是水平居中的。 css
这是完整的集合: html
.jqbox_innerhtml { position: fixed; width: 500px; height: 200px; margin: 5% auto; padding: 10px; border: 5px solid #ccc; background-color: #fff; }
<div class="jqbox_innerhtml"> This should be inside a horizontally and vertically centered box. </div>
如何使用CSS将此框置于屏幕中心? jquery
添加一个容器,如: 浏览器
div { position: fixed; bottom: 0; left: 0; width: 100%; text-align: center; }
而后把你的盒子放进这个div就能够了。 ide
惟一简单的解决方案是使用table align = center,如: this
<table align=center><tr><td> <div> ... </div> </td></tr></table>
我没法相信全世界的人们都在浪费这些丰富的时间去解决像居中这样的基本问题。 css解决方案不适用于全部浏览器,jquery解决方案是一种软件计算解决方案,而且因为其余缘由而不是一种选择。 spa
我反复浪费了太多时间以免使用桌子,但经验告诉我要中止对抗它。 使用表格来居中div。 在全部浏览器中始终有效! 不再用担忧了。 code
一个可能的答案 : htm
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>CSS Center Background Demo</title> <style type="text/css"> body { margin: 0; padding: 0; } div.centred_background_stage_1 { position: fixed; z-index:(-1 ); top: 45%; left: 50%; } div.centred_background_stage_2 { position: relative; left: -50%; top: -208px; /* % does not work. According to the http://reeddesign.co.uk/test/points-pixels.html 6pt is about 8px In the case of this demo the background text consists of three lines with font size 80pt. 3 lines (with space between the lines) times 80pt is about ~3*(1.3)*80pt*(8px/6pt)~ 416px 50% from the 416px = 208px */ text-align: left; vertical-align: top; } #bells_and_wistles_for_the_demo { font-family: monospace; font-size: 80pt; font-weight: bold; color: #E0E0E0; } div.centred_background_foreground { z-index: 1; position: relative; } </style> </head> <body> <div class="centred_background_stage_1"> <div class="centred_background_stage_2"> <div id="bells_and_wistles_for_the_demo"> World<br/> Wide<br/> Web </div> </div> </div> <div class="centred_background_foreground"> This is a demo for <br/> <a href="http://stackoverflow.com/questions/2005954/center-element-with-positionfixed"> http://stackoverflow.com/questions/2005954/center-element-with-positionfixed </a> <br/><br/> <a href="http://www.starwreck.com/" style="border: 0px;"> <img src="./star_wreck_in_the_perkinnintg.jpg" style="opacity:0.1;"/> </a> <br/> </div> </body> </html>
您基本上须要将top
和left
设置为50%
以使div的左上角居中。 您还须要将margin-top
和margin-left
设置为div的高度和宽度的负半部分,以将中心移向div的中间。 three
所以,提供了一个<!DOCTYPE html>
(标准模式),这应该作到:
position: fixed; width: 500px; height: 200px; top: 50%; left: 50%; margin-top: -100px; /* Negative half of height. */ margin-left: -250px; /* Negative half of width. */
或者,若是您不关心垂直居中和旧浏览器(如IE6 / 7),那么您也能够将left: 0
和right: 0
到具备auto
margin-left
margin-right
margin-left
的元素,以便具备固定宽度的固定定位元件知道其左右偏移开始的位置。 在你的状况下:
position: fixed; width: 500px; height: 200px; margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */ left: 0; right: 0;
一样,若是您关心IE,这仅适用于IE8 +,而且这仅在水平而非垂直居中。
或者只是将left: 0
和right: 0
到原始CSS,这使得它的行为相似于常规的非固定元素,而且一般的自动边距技术有效:
.jqbox_innerhtml { position: fixed; width:500px; height:200px; background-color:#FFF; padding:10px; border:5px solid #CCC; z-index:200; margin: 5% auto; left: 0; right: 0; }
请注意,您须要使用有效的(X)HTML DOCTYPE
才能在IE中正确运行(不管如何你固然应该......)