我有一个网站(g-floors.eu),我想使背景(在css中,我已经为内容定义了bg-image)也响应。不幸的是,我真的不知道如何做到这一点,除了我能想到的一件事,但这是一个相当好的变通办法。创建多个图像,然后使用css屏幕大小来改变图像,但我想知道是否有更实用的方法来实现这一点。

基本上我想要实现的是图像(水印'G')自动调整大小,而不显示更少的图像。当然,如果可能的话

链接:g-floors.eu

到目前为止的代码(内容部分)

#content {
  background-image: url('../images/bg.png');
  background-repeat: no-repeat;
  position: relative;
  width: 85%;
  height: 610px;
  margin-left: auto;
  margin-right: auto;
}

CSS:

background-size: 100%;

这应该能奏效!:)


如果你想让相同的图像根据浏览器窗口的大小缩放:

background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;

不要设置宽度、高度或边距。

编辑: 前一行关于不设置宽度、高度和边距的问题是指OP最初关于窗口大小缩放的问题。在其他用例中,如果需要,您可能需要设置宽度/高度/边距。


你可以用这个。我已经测试过了,它的工作100%正确:

background-image:url('../images/bg.png'); 
background-repeat:no-repeat;
background-size:100%;
background-position:center;

您可以在这个屏幕大小模拟器测试您的网站的响应性: http://www.infobyip.com/testwebsiteresolution.php

每次你做改变时,清除你的缓存,我更喜欢使用Firefox来测试它。

如果你想使用来自其他网站/URL的图像,而不是像这样: 背景图片:url(. . /图片/ bg.png); //该结构将使用来自您自己托管服务器的映像。 然后像这样使用: background-image: url(http://173.254.28.15/~brettedm/wp-content/uploads/Brett-Edmonds-Photography-14.jpg);

享受:)


自适应的平方比与jQuery

var Height = $(window).height();
var Width = $(window).width();
var HW = Width/Height;

if(HW<1){
      $(".background").css("background-size","auto 100%");
    }
    else if(HW>1){
      $(".background").css("background-size","100% auto");
    }

我使用

#content {
  width: 100%;
  height: 500px;
  background-size: cover;
  background-position: center top;
}

效果非常好。


通过这段代码,你的背景图像到中心,并固定它的大小,无论你的div大小改变,适合小,大,正常大小,最好的,我用它在我的项目中,我的背景大小或div大小可以改变

background-repeat:no-repeat;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
background-position:center;

这里是sass mixin响应背景图像,我使用。它适用于任何块元素。当然,同样可以在纯CSS中工作,你只需要手动计算填充。

@mixin responsive-bg-image($image-width, $image-height) {
  background-size: 100%;
  height: 0;
  padding-bottom: percentage($image-height / $image-width);
  display: block;
}


.my-element {
  background: url("images/my-image.png") no-repeat;

  // substitute for your image dimensions
  @include responsive-bg-image(204, 81);
}

比如http://jsfiddle.net/XbEdW/1/


试试这个:

background-image: url(_images/bg.png);
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: cover;

我认为,最好的办法是:

body {
    font-family: Arial,Verdana,sans-serif;
    background:url("/images/image.jpg") no-repeat fixed bottom right transparent;
}

这样就不需要再做什么了,而且很简单。

至少,它对我有用。

我希望这能有所帮助。


响应网站添加填充到底部图像高度/宽度x 100 = padding-bottom %:

http://www.outsidethebracket.com/responsive-web-design-fluid-background-images/


更复杂的方法:

http://voormedia.com/blog/2012/11/responsive-background-images-with-fixed-or-fluid-aspect-ratios


尝试调整背景大小eq Firefox Ctrl + M看到神奇的脚本,我认为最好的一个:

http://www.minimit.com/demos/fullscreen-backgrounds-with-centered-content


background:url("img/content-bg.jpg") no-repeat;
background-position:center; 
background-size:cover;

or

background-size:100%;

这是我得到的最好的方法。

#content   {
    background-image:url('smiley.gif');
    background-repeat:no-repeat;
    background-size:cover;
}

查看w3schools

更多可用选项

background-size: auto|length|cover|contain|initial|inherit;

#container {
    background-image: url("../images/layout/bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    height: 100vh;
    margin: 3px auto 0;
    position: relative;
}

    <style> 
         * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

#res_img {
  background: url("https://s15.postimg.org/ve2qzi01n/image_slider_1.jpg");
  width: 100%;
  height: 500px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-position: center;
  border: 1px solid red;
}

@media screen and (min-width:300px) and (max-width:500px) {
  #res_img {
    width: 100%;
    height: 200px;
  }
}

    </style>

<div id="res_img">
</div>

只需两行代码,它就能工作。

#content {
  background-image: url('../images/bg.png');
  background-size: cover;
}

这个很简单=)

body {
    background-image: url(http://domains.com/photo.jpeg);
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
}

看一下jsFiddle演示


如果你想让整个图像显示不受纵横比影响,那么试试这个:

background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:100% 100%;
background-position:center;

这将显示整个图像,无论屏幕大小。


尝试使用background-size,但使用两个参数,一个用于宽度,另一个用于高度

background-image:url('../images/bg.png'); 
background-repeat:no-repeat;
background-size: 100% 100%; // Here the first argument will be the width 
// and the second will be the height.
background-position:center;

background: url(/static/media/group3x.6bb50026.jpg);
background-size: contain;
background-repeat: no-repeat;
background-position: top;

位置属性可用于根据您的需要对齐顶部底部和中心,背景大小可用于中心裁剪(覆盖)或完整图像(包含或100%)