简体   繁体   中英

(CSS)Trouble having background image resize for mobile device

  /* 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;
      }
  }

The background image shows up fine on a desktop browser but when I view it on my iphone 6 plus or any other mobile phone the image doesn't scale correctly and is partially cut off. Can anyone help? Thanks.

There are two ways to work on the background and make it work well: You should delete the onset:

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

And

 height: 300px; width: 100%; 

The first way to make the length or width is supported on the other by adding the following code:

 background-size: 100% auto; 

The second way to make the rear rubber with any size by adding the following code:

 background-size: 100% 100%; 

Thank you

Also you can use or add for more optimization:

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

it will cover the all html or body base on the tag you want the image to cover.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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