网站正文背景在 iPhone Safari 中呈现右白边距

发布于 2024-11-03 11:30:03 字数 1400 浏览 1 评论 0原文

我在这个网站上遇到了我在其他地方没有见过的渲染错误。该网站在所有现代浏览器中呈现并验证良好,尽管我无法弄清楚为什么它不显示完整的背景图像(请参见下面的屏幕截图)。我正在使用 Yahoo CSS Reset,背景图像在正文中声明如下:

background: url("back.jpg") #033049;

您还可以访问该网站: http:// xaviesteve.com/

在此处输入图像描述

在此处输入图像描述

如果我需要提供更多详细信息,请告诉我。 任何帮助/提示表示赞赏,谢谢。

编辑

我发现互联网上很少有人报告此问题:

  1. 另一个SO问题:当背景图像应延伸到页面的整个长度时,页面右侧会显示空白 建议应用overflow-x:hidden 但它会裁剪网站。

  2. 在 iPad 论坛中:http://www.ipadforums.net/ipad-development/9954-mobile-safari-doenst-show-background-image-when-page-slided-left.html 无回复

解决方案

我一直在调查并尝试不同的方法来解决此问题,发现将背景图像添加到 标记可以解决该问题。希望这能为其他开发人员节省一些时间。

之前

body {background:url('images/back.jpg');}

之后

html, body {background:url('images/back.jpg');}

I have a rendering error in this website which I haven't seen anywhere else. The website renders in all modern browsers and validates fine although I can't figure out why is it not displaying the full background image (see screenshots below). I am using Yahoo CSS Reset and the background image is declared in the body like this:

background: url("back.jpg") #033049;

You can also visit the website: http://xaviesteve.com/

enter image description here

enter image description here

Let me know if I should provide any more details.
Any help/hint is appreciated, thank you.

EDIT

I have found very few people reporting this issue around the Internet:

  1. Another SO question: White space showing up on right side of page when background image should extend full length of page Suggested applying overflow-x:hidden but it crops the website.

  2. In an iPad forum: http://www.ipadforums.net/ipad-development/9954-mobile-safari-doenst-show-background-image-when-page-slided-left.html No replies

SOLUTION

I've been investigating and trying different ways to solve this and found that adding the background image to the <html> tag fixed the problem. Hope this saves some time to other devs.

Before

body {background:url('images/back.jpg');}

After

html, body {background:url('images/back.jpg');}

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

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

发布评论

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

评论(5

百合的盛世恋 2024-11-10 11:30:03

将样式移至 html 元素效果很好,但还有其他方法可以解决此问题。

这里发生的事情最初是根据视口调整 body 元素的大小。如果视口只有 X 像素宽,则您的正文也只有 X 像素宽,即使包含的内容更宽。要解决此问题,请为您的 body(或您要附加背景样式的任何内容)指定一个非基于百分比的 widthmin-width > 适合您的内容。

实际上,通过缩小浏览器窗口并向右滚动,您在桌面浏览器上也会遇到同样的问题。这个问题在 iPhone/iPad 上更为明显,因为默认情况下,Mobile Safari 会将视口设置为 980 像素,然后缩小,直到所有内容适合屏幕。

另一种解决方案(我不建议这样做,因为它仅适用于 Mobile Safari)是您自己设置视口宽度:

<meta name = "viewport" content = "width = 1080">

更多信息位于 Apple 开发人员文档

Moving the styling to the html element works fine, but there are other ways of fixing this.

What's going on here is initially the body element is sized according to the viewport. If the viewport is only X pixels wide, your body will only be X pixels wide, even if the contained content is wider. To fix this, give your body (or whatever you're attaching the background stylings to) a non-percentage based width or a min-width to fit your content.

You actually get the same issue on desktop browsers by narrowing the browser window and scrolling to right. The problem is more noticeable on the iPhone/iPad because by default, Mobile Safari will set the viewport to 980px, and then zoom out until all your content fits on screen.

An alternate solution, which I wouldn't recommend because it only works for Mobile Safari is setting the viewport width yourself using:

<meta name = "viewport" content = "width = 1080">

More info at Apple's Developer Docs.

杀手六號 2024-11-10 11:30:03

刚刚遇到这个问题,并通过设置所有使用 width:100% 的内容来修复它;还可以将 min-width 设置为大于内容 div 的宽度。
例如:

.content_bg{width:100%; min-width:1080px;}

我还使用媒体查询在移动设备上修复了它:
对于 iPhone 和 iPad:

/*ipad portrait*/    
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
      body{width:1080px;}
    }

/*ipad landscape*/    
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
      body{width:1080px;}
    }

/* iPhone [portrait + landscape] */
@media only screen and (max-device-width: 480px) {
        body{width:1080px;}
    }

Just ran into this problem and fixed it by setting all content that uses width:100%; to also have min-width set larger than the width of your content divs.
FOr example:

.content_bg{width:100%; min-width:1080px;}

I also fixed it on mobile devices using media queries:
for iPhone and iPad:

/*ipad portrait*/    
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
      body{width:1080px;}
    }

/*ipad landscape*/    
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
      body{width:1080px;}
    }

/* iPhone [portrait + landscape] */
@media only screen and (max-device-width: 480px) {
        body{width:1080px;}
    }
饮惑 2024-11-10 11:30:03

这些解决方案都没有解决我的问题,但我发现了一个相当简单的解决方案。
只需将 bg 容器的 background-size 设置为与图像尺寸相同,如下所示:

body {
    background-image: url(bg.jpg); /* image dimensions: 1920 x 3840 */
    background-size: 1920px 3840px;
}

虽然它可能看起来有点多余,而且不如让您的网站响应良好,但它工作得很好。

None of those solutions solved my problem, but I found out a rather simple one.
Just set the background-size of your bg container equivalent to the image dimensions, like this:

body {
    background-image: url(bg.jpg); /* image dimensions: 1920 x 3840 */
    background-size: 1920px 3840px;
}

Although it may seem a bit redundant and not nearly as good as making your site responsive, it works fine.

半世晨晓 2024-11-10 11:30:03

将背景大小设置为 100%,将背景位置设置为左上角。它将正常工作,如下所示:

background-color: #336699;
background-image: url('whatever.jpg');
background-repeat: no-repeat;
background-position: top left;
background-attachment: fixed;
-webkit-background-size: 100%;
-moz-background-size: 100%;
-o-background-size: 100%;
background-size: 100%;`enter code here`

Set background-size to 100% and background-position to top-left. It will works fine as follows:

background-color: #336699;
background-image: url('whatever.jpg');
background-repeat: no-repeat;
background-position: top left;
background-attachment: fixed;
-webkit-background-size: 100%;
-moz-background-size: 100%;
-o-background-size: 100%;
background-size: 100%;`enter code here`
我纯我任性 2024-11-10 11:30:03

我知道这个问题已经在前一段时间得到了解答,但是我找到的所有修复程序都不适合我,但我管理了自己的解决方案,该解决方案应该适用于我想象的大多数人。

这是我的代码:

<代码>
html{
背景: url("../images/blahblah.jpg") 重复-y;
最小宽度:100%;
背景大小:包含;
}

希望它对某人有帮助!

I know this is already answered some time ago, but none of the fixes I could find worked for me, but I managed my own solution which should work for most people I imagine.

Here's my code:


html {
background: url("../images/blahblah.jpg") repeat-y;
min-width: 100%;
background-size: contain;
}

Hopefully it helps someone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文