简体   繁体   English

调整背景图片的大小

[英]Background image resizing

I'am trying to design a website with a background image that should stretch in height and width accordingly when the browser window is resizing. 我正在尝试设计一个带有背景图像的网站,当浏览器窗口调整大小时,背景图像应在高度和宽度上相应地拉伸。 It is complicated to explain what exact behaviour I would like to achieve, for this reason here is the website that does it perfectly: 解释我要实现的确切行为是很复杂的,因此,这是一个完美的网站:

http://de.wix.com/website-template/view/html/709?originUrl=http%3A%2F%2Fde.wix.com%26number-of-page%3D1%26position-in-page%3D4 http://de.wix.com/website-template/view/html/709?originUrl=http%3A%2F%2Fde.wix.com%26number-of-page%3D1%26position-in-page%3D4

I used background-size set to everything possible as well as background-width set to 100%, but I could not achieve such a behaviour. 我将background-size设置为所有可能的值,并且将background-width设置为100%,但是我无法实现这种行为。

Have you tried using the below CSS? 您是否尝试过使用以下CSS? This fills the page background with the image. 这将用图像填充页面背景。

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

Without any HTML or CSS of your own, it's impossible for us to truly help you, but generally this behavior can be achieved with the following property: background-size: cover 没有您自己的任何HTML或CSS,我们将无法真正为您提供帮助,但是通常可以通过以下属性来实现此行为: background-size:cover

background-image: url('URLGOESHERE');
    height: ???;
    background-size: cover;

As long as you specify an explicit height (pixels) and do not specify a width, this property should cause the background-image to resize while maintaining aspect ratio. 只要您指定一个显式的高度(像素)并且指定宽度,此属性将导致在保持纵横比的同时调整背景图像的大小。

JSFiddle Example. JSFiddle示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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