css3 - 背景图自定义比例缩小
PHPz
PHPz 2017-04-17 11:57:54
0
7
951

本来新手一枚,在开发中遇到背景图不能安自定义比例缩放,用了background-size:cover,图片要么右半部分不完整,要么下半部分不完整,要怎么做才能铺满全屏并且全部显示出来

PHPz
PHPz

学习是最好的投资!

reply all(7)
PHPzhong
body {
    background-size:cover;
    text-align:center;
    /*此部分支持chrome,应该也支持firefox*/
    background:rgb(246,248,249);
    background:url(../img/bg.jpg) no-repeat center fixed transparent;
    background-attachment:fixed;
    background-size:100% 100%;
    /*以下是IE部分,使用滤镜*/
    filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../img/bg.jpg',sizingMethod='scale');
    background-repeat:no-repeat;
    background-position:100%,100%;
}
阿神
background-attachment: fixed;
background-size: cover;
background-repeat: no-repeat;
background-image: url(./img/desk4.jpg);
background-position: 50% 50%;

左手右手慢动作

You mean your image is exactly the size of the screen?

Unless the aspect ratio of your image is the same as the screen ratio, the image will be deformed, either squashed or thinned, otherwise it will be what you said

大家讲道理

The cover is full and the image may exceed it. Contain means that the image is completely inserted, and there may be some white space.
I have also encountered this adaptation requirement. The current idea is to img width height100% low-level simulation background. If you don’t have time to try it yet, you can try it first.

刘奇

Both cover and contain are scaled proportionally. Otherwise, use img 100% or background-size percentage. To make the background full-screen ratio, the ratio must be correct

刘奇
  1. The right or bottom part is incomplete, guessing that background-position does not have center;

  2. cover is filled with elements (with cropping), contain is the largest background (leaving black borders), both of which maintain the aspect ratio;

  3. If you don’t want the aspect ratio, you can try 100% 100%, which is deformed but can fill the elements without cropping;

  4. You can adjust the element size if necessary, just use js to do it dynamically.

Peter_Zhu

The first method: Center the background image through positioning, and then adjust the background size to cover, so that the image will not be deformed and covered;

p {
    background-size: cover;
    background-position: center center;
}

The second method: achieve the full effect by forcing the width and height of the background image to 100%, but the image is easily deformed (not recommended).

p {
    background-size: 100% 100%;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template