简体   繁体   English

CSS 背景图像在移动设备上全屏 android 设备不完美

[英]CSS Background Image full screen on mobile android devices not perfect

wow.哇。 How hard can it be to create a full screen background image with HTML and CSS?使用 HTML 和 CSS 创建全屏背景图像有多难? Seems to be quite hard.好像还蛮难的。 At least, the internet if full of answers like this css perfect full screen image background and this https://css-tricks.com/perfect-full-page-background-image/ .至少,如果互联网上充满了css 完美全屏图像背景https://css-tricks.com/perfect-full-page-background-image/ 之类的答案。

In fact, the demo of the second link works also on mobile android devices: https://css-tricks.com/examples/FullPageBackgroundImage/progressive.php事实上,第二个链接的演示也适用于移动 android 设备: https://css-tricks.com/examples/FullPageBackgroundImage/progressive.ZE1BFD762321E409CEE4AC0B6E841963

But I still have no luck.但我仍然没有运气。 I copied the style我复制了样式

html {
    background: url(/images/snowback.png) no-repeat center center fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

To my stylesheet https://aoc-2019.netlify.com/css/retro.css and still, the background image is not perfect: https://aoc-2019.netlify.com To my stylesheet https://aoc-2019.netlify.com/css/retro.css and still, the background image is not perfect: https://aoc-2019.netlify.com

It first seems to be perfect, but the Android browser hides the adress bar when you scroll around a bit and then the background will not be resized.它首先看起来很完美,但是当您滚动一点时,Android 浏览器会隐藏地址栏,然后背景不会调整大小。 This leads to a region on hte bottom of the page which is not covered by the background image.这会导致页面底部的区域未被背景图像覆盖。

And as I said, the demo above works quite well.正如我所说,上面的演示效果很好。

What is the difference between my page and the demo?我的页面和演示有什么区别? Any idea?任何想法?

The differences I can easily spot are...我可以轻松发现的差异是...

CSS-Tricks version: html and body have no height or min-height. CSS-Tricks 版本:html 和 body 没有高度或最小高度。

Your version: html and body both set too 100% for height and min-height.您的版本:html 和 body 的高度和最小高度都设置为 100%。

CSS-Tricks version: Body set to overflow-x: hidden; CSS-Tricks 版本:正文设置为overflow-x: hidden;

Your version: No overflow rule on body.您的版本:正文没有溢出规则。

I suggest you replicate what they did precisely, with no added content, make sure it works as expected.我建议你准确地复制他们所做的,不添加任何内容,确保它按预期工作。 Then recreate it within your site.然后在您的站点中重新创建它。

If it turns out that the 100% height and min-height are the problem, and yet you need those for a sticky footer or something.如果事实证明 100% 的高度和最小高度是问题所在,但您需要这些作为粘性页脚或其他东西。 Then you may be able to use a secondary wrapper div to facilitate your sticky footer.然后,您可以使用辅助包装 div 来促进您的粘性页脚。 Like put the height/min-height rules on an fullwrapper div, then within that use a pagewrapper as normal with the sticky footer.就像将高度/最小高度规则放在一个完整的包装器 div 上,然后在其中使用带有粘性页脚的页面包装器。

There are several differences on how the page is structured.页面的结构有几个不同之处。 Long story short, the main thing that is preventing it from working as you expected is the assignment of height: 100% to your HTML element.长话短说,阻止它按预期工作的主要因素是 height: 100% 分配给您的 HTML 元素。 As you can read here directly from Google's developers website , assigning height: 100% gives to an element the height available with the bar displayed and won't resize once it's hidden.正如您可以直接从 Google 的开发者网站上阅读的那样,分配 height: 100% 为元素提供了显示栏时可用的高度,并且一旦隐藏就不会调整大小。 If you don't want to revise and change your code and need a fast solution, just add height 110% to the HTML and you'll get the same effect as in the compared link.如果您不想修改和更改代码并需要快速解决方案,只需将高度 110% 添加到 HTML 即可获得与比较链接中相同的效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM