【问题标题】:CSS Background fit every resolutionCSS 背景适合每个分辨率
【发布时间】:2013-12-02 02:40:48
【问题描述】:

好的,所以我是新手。我希望我的背景(即图像)适合 Web 浏览器的每个分辨率,无论浏览器分辨率如何。我发现了这个技巧:

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

但它所做的只是使背景适合显示器的完整尺寸,而不是浏览器。因此,如果显示器为 1366x768 并且浏览器已最大化,那么背景将正确显示为“满的”。但是,如果我再调整浏览器,背景图像将无法正确显示。

那我需要怎么做,所以背景图片是用浏览器调整的?因此,如果浏览器为 1300x700,则背景图像为 1300x700,但如果浏览器为 900x600,则背景图像为 900x600。同样,我是新手,所以请提供一些示例。

【问题讨论】:

  • background-size: 100% 100%;
  • 试过用contain代替cover
  • cover 这个关键字指定背景图片在保证两个尺寸都大于或等于背景定位对应尺寸的情况下要尽可能的缩小区域。 contain 这个关键字指定背景图片在保证两个尺寸都小于或等于背景定位区域对应尺寸的情况下,应该尽可能的放大。所以尝试使用 contain 而不是 cover
  • 如果我是正确的,包含将背景限制为图像的分辨率。所以如果我的图像是 1330x900,但我的浏览器是 2400x1500,包含限制图像为 1330x900?我尝试了包含,这就是正在发生的事情。我的代码: html{ background-image:url("../1330img.png");背景重复:不重复; -webkit-背景大小:包含; -moz-背景大小:包含; -o-背景大小:包含;背景尺寸:包含;

标签: css browser background resolution adjustment


【解决方案1】:

实际上,您可以通过插入所需的具有高度和宽度属性的背景图像并在其上设置 100% 宽度来解决此问题。然后您可以将您的内容放在该图像之上。

<div class="container">
    <img class="background-image" src="whatever.png" width="NATURAL WIDTH" height="NATURAL HEIGHT">
    <div class="content">
        This stuff will be on top of the image.
    </div>
</div>

<style>
    /* This element will == the height of the image */
    .container {
        position: relative;
    }

    /* This element is the background image */
    .background-image {
        width: 100%;
        height: auto;
        position: static;
    }

    /* This element contains the content which will be placed on top of the background image */
    .content {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 1;
    }
</style>

【讨论】:

    【解决方案2】:

    cover 该关键字指定背景图片在保证两个尺寸都大于或等于背景定位区域对应尺寸的情况下,应尽可能缩小。

    contain 该关键字指定背景图片在保证两个尺寸都小于或等于背景定位区域对应尺寸的情况下,应该尽可能的放大。所以尝试使用包含而不是覆盖

    100% 这将 100% 缩放到高度和宽度,而不会进行任何裁剪。

    html { 
      background: url(images/bg.jpg) no-repeat center center fixed; 
      -webkit-background-size: contain;
      -moz-background-size: contain;
      -o-background-size: contain;
      background-size: contain;
    }
    

    这可能会对您有所帮助:http://www.w3schools.com/cssref/playit.asp?filename=playcss_background-size&preval=contain

    还有这个https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

    【讨论】:

    • 如果我是正确的,包含将背景限制为图像的分辨率。所以如果我的图像是 1330x900,但我的浏览器是 2400x1500,包含限制图像为 1330x900?我尝试了包含,这就是正在发生的事情。我的代码: html{ background-image:url("../1330img.png");背景重复:不重复; -webkit-背景大小:包含; -moz-背景大小:包含; -o-背景大小:包含;背景大小:包含; .
    • 您可以尝试将@media 用于不同的浏览器尺寸,也可以使用 background-size: 100%;
    • 覆盖最大长度后,将背景图像裁剪为高度或宽度。包含将图像按比例缩放,以便完整图像可见
    • 您不应该只使用 background-size: 100% 而是可以为不同的媒体尺寸加载不同尺寸的图像,然后使用 100%。
    • 谢谢 Sazzad,包含关键字帮助我根据屏幕尺寸重新调整高分辨率图像的大小。非常感谢您的回答。你节省了我宝贵的时间:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多