(CSS)移动设备上背景图片无法调整大小的问题

3
  /* Responsive Full Background Image Using CSS
  * Tutorial URL: http://sixrevisions.com/css/responsive-background-image/
  */

  body {
      /* Location of the image */
      background-image: url(images/background-photo.jpg);
      /* Image is centered vertically and horizontally at all times */
      background-position: center center;
      /* Image doesn't repeat */
      background-repeat: no-repeat;
      /* Makes the image fixed in the viewport so that it doesn't move when 
     the content height is greater than the image height */
      background-attachment: fixed;
      /* This is what makes the background image rescale based on its container's size */
      background-size: cover;
      /* Pick a solid background color that will be displayed while the background image is loading */
      background-color: #464646;
      /* SHORTHAND CSS NOTATION
   * background: url(background-photo.jpg) center center cover no-repeat fixed;
   */
      min-width: 100%;
      max-width: 100%;
      min-height: 100%;
      max-height: 100%;
  }
  /* For mobile devices */

  @media only all and (max-width: 768px) {
      .logo {
          /* The file size of this background image is 93% smaller
     * to improve page load speed on mobile internet connections */
          background-image: url(images/background-photo-mobile-devices.jpg);
          height: 300px;
          width: 100%;
          background-size: contain;
          background-repeat: no-repeat;
          background-position: center;
      }
  }

背景图片在桌面浏览器上显示正常,但是在iPhone 6 Plus或其他任何移动电话上查看时,图像无法正确缩放并且部分被裁剪。有人能提供帮助吗?谢谢。

POST 或许可以帮助您区分移动页面和网页。然后您可以相应地更改您的 background-position 属性。 - Abhi
2个回答

3
有两种方法可以处理背景并使其正常工作: 您应该删除开始:

min-width: 100%;
max-width: 100%;
min-height: 100%;
max-height: 100%;

并且

height: 300px;
width: 100%;

要使长度或宽度支持对方,可以通过添加以下代码的方式实现:

background-size: 100% auto;

第二种方法是通过添加以下代码来使后部橡胶具有任何尺寸:

background-size: 100% 100%;

谢谢你


0

另外,您还可以使用或添加更多的优化:

html{
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
   background-size: cover;
}

它将基于您想要图像覆盖的标签,覆盖所有HTML或主体。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接