居中背景图像偏离 1 像素

发布于 2024-07-06 00:23:15 字数 388 浏览 6 评论 0原文

我的网页位于 960px 宽的 DIV 中,我使用以下代码将该 DIV 居中于页面中间:

html,body{background: url(images/INF_pageBg.gif) center top repeat-y #777777;text-align:center;}
#container{background-color:#ffffff;width:960px;text-align:left;margin:0 auto 0 auto;}

我需要 html/body 的背景图像平铺到页面中间,确实如此,但是,如果浏览器中的可视窗格的像素宽度为奇数,则居中背景和居中 DIV 不会对齐在一起。

这种事只发生在FF。

有人知道解决方法吗?

My web page sits in a DIV that is 960px wide, I center this DIV in the middle of the page by using the code:

html,body{background: url(images/INF_pageBg.gif) center top repeat-y #777777;text-align:center;}
#container{background-color:#ffffff;width:960px;text-align:left;margin:0 auto 0 auto;}

I need the background image of the html/body to tile down the middle of the page, which it does, however if the viewable pane in the browser is an odd number of pixels width then the centered background and centered DIV don't align together.

This is only happening in FF.

Does anybody know of a workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦与时光遇 2024-07-13 00:23:15

(最)常见的问题是背景图像的编号为奇数,而容器的编号为偶数。
我用我最好的英语写了一篇文章,其中还解释了浏览器如何定位您的图片:在这里查看

The (most) common problem is that your background image has an odd number while your container is an even number.
I have wrote an article in my best English about where I also explain how the browser positioned your picture: check it out here.

伴梦长久 2024-07-13 00:23:15

我发现通过将背景图像设置为奇数像素宽,Firefox 的问题就消失了。

设置内边距:0px 0px 0px 1px; 修复了 IE 的问题。

卡洛·卡波卡萨,Travian Games

I found that by making the background image on odd number of pixels wide, the problem goes away for Firefox.

Setting padding:0px 0px 0px 1px; fixes the problem for IE.

Carlo Capocasa, Travian Games

指尖上得阳光 2024-07-13 00:23:15

是的,这是已知问题。 不幸的是,您只能修复 div 和图像宽度,或使用脚本动态更改 stye.backgroundPosition 属性。 另一个技巧是将表达式放入 CSS 类定义中。

Yeah, it's known issue. Unfortunately you only can fix div and image width, or use script to dynamically change stye.backgroundPosition property. Another trick is to put expression to the CSS class definition.

伏妖词 2024-07-13 00:23:15

创建一个具有相同背景图像的包装 div 怎么样?

body{ background: url(your-image.jpg) no-repeat center top; }
#wrapper{ background: url(your-image.jpg) no-repeat center top; margin: 0 auto; width: 984px; }

包装纸的编号为偶数,背景在任何屏幕尺寸上都将保持相同的位置。

What about creating a wrapper div with the same background-image.

body{ background: url(your-image.jpg) no-repeat center top; }
#wrapper{ background: url(your-image.jpg) no-repeat center top; margin: 0 auto; width: 984px; }

The wrapper has an even number, the background will keep the same position on any screen size.

影子是时光的心 2024-07-13 00:23:15

我有同样的问题。

要使背景居中,您需要背景图像比视口更宽。 尝试使用宽度2500px的背景。 它将强制浏览器将可见图像部分居中。

请让我知道这对你有没有用。

I had the same problem.

To get the background centered, you need to have a background-image wider than the viewport. Try to use a background 2500px wide. It will force the browser to center the part of image that is viewable.

Let me know if it works for you.

养猫人 2024-07-13 00:23:15

我可以用 jQuery 解决这个问题:

$(document).ready(function(){
 $('body').css({
  'margin-left': $(document).width()%2
 });
});

I was able to resolve this with jQuery:

$(document).ready(function(){
 $('body').css({
  'margin-left': $(document).width()%2
 });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文