是否可以使用CSS设置背景图片的尺寸?
我想要做像这样的事情:
background: url('bg.gif') top repeat-y;
background-size: 490px;
但是似乎这样做是完全错误的...
是否可以使用CSS设置背景图片的尺寸?
我想要做像这样的事情:
background: url('bg.gif') top repeat-y;
background-size: 490px;
但是似乎这样做是完全错误的...
如果需要将图像变大,必须在图像编辑器中编辑图像本身。
如果使用img标签,可以更改大小,但如果您需要将图像用作其他内容的背景(且不会像您想要的那样重复),那么这不会给您所需的结果...
使用background-size
在CSS3中可以实现这一点。
所有现代浏览器都支持此功能,因此除非需要支持旧浏览器,否则应该采用此方法。
支持的浏览器:
Mozilla Firefox 4.0+(Gecko 2.0+),Microsoft Internet Explorer 9.0+,Opera 10.0+,Safari 4.1+(webkit 532)和Chrome 3.0+。
.stretch{
/* Will stretch to specified width/height */
background-size: 200px 150px;
}
.stretch-content{
/* Will stretch to width/height of element */
background-size: 100% 100%;
}
.resize-width{
/* width: 150px, height: auto to retain aspect ratio */
background-size: 150px Auto;
}
.resize-height{
/* height: 150px, width: auto to retain aspect ratio */
background-size: Auto 150px;
}
.resize-fill-and-clip{
/* Resize to fill and retain aspect ratio.
Will cause clipping if aspect ratio of box is different from image. */
background-size: cover;
}
.resize-best-fit{
/* Resize to best fit and retain aspect ratio.
Will cause gap if aspect ratio of box is different from image. */
background-size: contain;
}
特别是我喜欢cover
和contain
值,它们给了我们以前没有的控制力。
您还可以使用background-size:round
,它与repeat组合在一起具有意义:
.resize-best-fit-in-repeat{
/* Resize to best fit in a whole number of times in x-direction */
background-size: round auto; /* Height: auto is to keep aspect ratio */
background-repeat: repeat;
}
这将调整图像宽度,使其在背景定位区域内完整显示。
补充说明
如果你需要的尺寸是固定像素大小,仍然明智地将实际图像进行物理缩放。一方面可以提高调整大小的质量(因为您的图像软件会比浏览器更好地完成此项工作),另一方面可以节省带宽,如果原始图像大于所需显示的大小。
只有CSS 3支持该功能,
background-size: 200px 50px;
但我会编辑图片本身,这样用户需要加载的量就会减少,并且它可能看起来比没有抗锯齿的缩小图像更好。
background: url('resized_image.png')\9;
。 - skobaljic不可能。 背景图像会尽可能地扩展以填满整个背景,但你可以使用background-repeat
属性来阻止它重复显示。
background-repeat: no-repeat;
第二,背景不会进入框的边距区域,因此如果你想要背景只存在于框的实际内容中,你可以使用margin代替padding。
第三,你可以控制背景图像的起始位置。默认情况下是框的左上角,但你可以使用background-position
来控制,像这样:
background-position: center top;
或者可能
background-position: 20px -14px;
负定位在CSS精灵中经常被使用。
有一个被遗忘的参数:
background-size: contain;
使用这种方法不会像使用cover
一样拉伸您的background-image
。它将一直拉伸直到更长的边缘达到外部容器的宽度或高度,从而保留图像。
编辑:还有-webkit-background-size
和-moz-background-size
。
background-size
属性在IE9+、Firefox 4+、Opera、Chrome和Safari 5+中受支持。
<div class="hasBackgroundImage">content</div>
.hasBackgroundImage
{
background: transparent url('/image/background.svg') no-repeat 10px 5px;
background-size: 1.4em;
}
你完全可以通过CSS3实现:
body {
background-image: url(images/bg-body.png);
background-size: 100%; /* size the background image at 100% like any responsive img tag */
background-position: top center;
background-repeat:no-repeat;
}
将background-size: 200px 50px改为100% 100%,它将根据内容标签(如ul li或div)的需求进行缩放...已尝试。
put the below code in the body of you css file
background-image: URL('../images/wave-green-plain-colour.jpg') ;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
width:100px;
为了跨浏览器兼容性,只需使用嵌套的div即可
<div>
<div style="background: url('bg.gif') top repeat-y;min-width:490px;">
在此处放置内容
</div>
</div>