为什么我的 div 没有正确居中?
我使用 $.blockUI 和 jQuery 来显示模式对话框。 HTML 看起来像:
<div id="progressDialogue" class="mp_modalpopup">
<div class="mp_container">
<div class="mp_header">
<span id="pd_header_text" class="mp_msg">Please wait..</span>
</div>
<div class="mp_body">
<img src="ajax-loader.gif" style="text-align:center" alt="loading" />
</div>
</div>
</div>
CSS 看起来像:
.mp_modalpopup
{
font-family: arial,helvetica,clean,sans-serif;
font-size: small;
padding: 2px 3px;
bottom: 50%;
right: 50%;
position: absolute;
width: 400px;
z-index:999;
}
.mp_container
{
width: 400px;
border: solid 1px #808080;
border-width: 1px 1px;
left: 50%;
position: relative;
top: 50%;
}
/* removed mp_header, mp_body, mp_msg CSS for brevity */
这将愉快地在页面中心的其他 HTML 之上呈现出“smack bang”。
但是,如果我在 .mp_modalpopup
CSS 类中设置 display:none
,然后使用 $.blockUI 使其可见,则在 IE 8 中,对话会垂直居中,但与左对齐一半的对话离开页面,在 Google Chrome 和 Firefox 中,对话根本不可见(但 blockUI 正在工作,因为页面呈灰色)。
这是 blockUI javascript:
$.blockUI.defaults.css = {};
$.blockUI({
message: $('#progressDialogue'),
overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
css: {backgroundColor: '#00f', color: '#100'}
});
为什么会发生这种情况?
I'm using $.blockUI with jQuery to show a modal dialogue. The HTML looks like:
<div id="progressDialogue" class="mp_modalpopup">
<div class="mp_container">
<div class="mp_header">
<span id="pd_header_text" class="mp_msg">Please wait..</span>
</div>
<div class="mp_body">
<img src="ajax-loader.gif" style="text-align:center" alt="loading" />
</div>
</div>
</div>
The CSS looks like:
.mp_modalpopup
{
font-family: arial,helvetica,clean,sans-serif;
font-size: small;
padding: 2px 3px;
bottom: 50%;
right: 50%;
position: absolute;
width: 400px;
z-index:999;
}
.mp_container
{
width: 400px;
border: solid 1px #808080;
border-width: 1px 1px;
left: 50%;
position: relative;
top: 50%;
}
/* removed mp_header, mp_body, mp_msg CSS for brevity */
This will happily render smack bang in the center of the page on top of other HTML.
However if I set display:none
in the .mp_modalpopup
CSS class and then use $.blockUI to make it visible, in IE 8 the dialogue centers itself vertically but aligns left with half of the dialogue off the page and in Google Chrome and Firefox the dialogue is not visible at all (but blockUI is working because the page greys out).
This is the blockUI javascript:
$.blockUI.defaults.css = {};
$.blockUI({
message: $('#progressDialogue'),
overlayCSS: { backgroundColor: '#000', opacity: 0.1 },
css: {backgroundColor: '#00f', color: '#100'}
});
Why is this happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为 blockUI 也试图将您的
删除这个:
还有这个
或者,您可以禁用
blockUI
居中,如下所示:This is happening because blockUI is also trying to center your
<div>
. If you remove all positioning from your CSS, it should work.Remove this:
And this
Alternatively, you could disable the
blockUI
centering like this:实际上我刚刚找到了一个非常简单的解决方案。
如果您阻止一个元素而不是整个页面,blockUI 将为您提供使用 centerX 和 centerY 的选项。
我屏蔽了 html 元素,仅此而已。我的 div 水平和垂直居中。当然,我不希望它垂直居中,所以我将 centerY 设置为 false,并在 CSS 中将其设置为 top: '5%',如下所示。还要确保在 blockUI css 中设置元素的宽度。
Actually I just found a very easy solution.
If you block an element instead of the entire page, blockUI gives you the options to use centerX and centerY.
I blocked the element html and that was it. My div was horizontally and vertically centered. Ofourse I didn't want it vertically centered, so I set centerY to false, and in the CSS set it to top: '5%', as below. Also make sure you set the width of the element in the blockUI css.