是否可以用CSS设置背景图像的大小?
我想做的事情是:
background: url('bg.gif') top repeat-y;
background-size: 490px;
但这样做似乎是完全错误的……
是否可以用CSS设置背景图像的大小?
我想做的事情是:
background: url('bg.gif') top repeat-y;
background-size: 490px;
但这样做似乎是完全错误的……
不可能的。背景总是尽可能的大,但是你可以用background-repeat来阻止它重复自己。
background-repeat: no-repeat;
其次,背景不会进入框的边距区域,所以如果你想让背景只在框的实际内容上,你可以使用边距而不是填充。
第三,你可以控制背景图像的起始位置。默认情况下,它是一个框的左上角,但你可以通过background-position来控制它,就像这样:
background-position: center top;
或者
background-position: 20px -14px;
CSS精灵经常使用负定位。
如果你的用户只使用Opera 9.5+, Safari 3+, Internet Explorer 9+和Firefox 3.6+,那么答案是肯定的。否则,没有。
background-size属性是css3的一部分,但它不能在大多数浏览器上工作。
为了您的目的,只需将实际图像放大。
CSS2
如果需要放大图像,则必须在图像编辑器中编辑图像本身。
如果你使用img标签,你可以改变大小,但如果你需要图像作为其他内容的背景,这不会给你想要的结果(它不会像你想要的那样重复自己)…
CSS3释放力量
在CSS3中使用background-size可以做到这一点。
所有现代浏览器都支持这一点,所以除非您需要支持旧的浏览器,否则这就是实现它的方法。 支持的浏览器: 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;
}
我特别喜欢封面和包含的值,它给了我们新的控制能力,这是我们以前没有的。
轮
你也可以使用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;
}
这将调整图像宽度,使其符合背景定位区域的整个数字。
额外的注意 如果你需要的大小是静态像素大小,它仍然是聪明的物理调整实际图像的大小。这既是为了提高调整大小的质量(考虑到您的图像软件比浏览器做得更好),也是为了在原始图像比要显示的图像大时节省带宽。
我为按钮使用了背景图像,但即使我设置了宽度和高度,它也只显示了与文本相同大小的图像。相反,我用 字符(非换行空格)。基本上,我在需要的时候插入很多按钮,直到所有的按钮背景都出现。我可能有这样的代码:
样式表中:
#v2menu-home {
background-image:url(../v2-siteimages/button.png);
background-repeat:no-repeat;
}
在HTML文档中:
<div id="v2menu">
<a id="v2menu-home" href="/index.php">home </a>
</div><!-- v2menu -->
只有css3支持,
background-size: 200px 50px;
但是我会编辑图像本身,这样用户就需要加载更少的内容,而且它可能比没有反锯齿的缩小图像看起来更好。
只需有嵌套的div跨浏览器兼容
<div>
<div style="background: url('bg.gif') top repeat-y;min-width:490px;">
Put content here
</div>
</div>
你可以使用两个<div>元素:
一个是容器(它是您最初想要的容器) 背景图像出现在)。 第二个包含在。您将其大小设置为 背景图像(或您希望显示的大小)。
然后将包含的div设置为绝对位置。这样它就不会干扰包含div中的项目的正常流动。
它使你能够有效地使用精灵图像。
为了支持@tetra给出的答案,我想指出,如果图像是SVG,那么调整实际图像的大小是不必要的。
由于SVG文件只是XML,您可以指定希望它在XML中显示的大小。
但是,如果您在不同的地方使用相同的SVG图像,并且需要使用不同的大小,那么使用background-size非常有用。SVG文件本来就比栅格图像小,用CSS调整大小非常有用,而且我知道不会有任何性能损失,当然质量也几乎没有损失。
这里有一个简单的例子:
<div class="hasBackgroundImage">content</div>
.hasBackgroundImage
{
background: transparent url('/image/background.svg') no-repeat 10px 5px;
background-size: 1.4em;
}
(注:这适用于我在OS X 10.7与Firefox 8, Safari 5.1和Chrome 16.0.912.63)
不要太难,如果你不怕更深入一点的话:)
有一个被遗忘的论点:
background-size: contain;
这不会像封面那样拉伸你的背景图像。它会一直拉伸,直到较长的一边达到外容器的宽度或高度,从而保存图像。
编辑:还有-webkit-background-size和-moz-background-size。
IE9+、Firefox 4+、Opera、Chrome和Safari 5+支持background-size属性。
-资料来源:W3 Schools
你完全可以用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;
}
这将把背景图像的大小调整为主体元素宽度的100%,然后根据较小的分辨率调整主体元素的大小,相应地调整背景的大小。
示例如下:http://www.sccc.premiumdw.com/examples/resize-background-images-with-css/
例如: 背景图像将始终适合容器大小(宽度100%,高度100px)。 跨浏览器的CSS:
.app-header {
background: url("themes/default/images/background.jpg") no-repeat;
-moz-background-size: 100% 100px;
-webkit-background-size: 100% 100px;
-o-background-size: 100% 100px;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src = "themes/default/images/background.jpg", sizingMethod = 'scale');
background-size: 100% 100px;
}
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;
你写过
background: url('bg.gif') top repeat-y;
background-size: 490px;
但是你只能看到背景,这取决于容器的大小。
如果你有一个带有background url和background-size的空容器,你将看不到bg.gif。
如果你将容器的大小设置为
background: url('bg.gif') top repeat-y;
background-size: 490px;
height: 490px;
width: 490px;
结合上面编写的代码,您将能够看到bg.gif文件。
在CSS3中使用background-size可以做到这一点
.backgroungImage {
background: url('../imageS/background.jpg') no-repeat;
background-size: 32px 32px;
}
如果你想在相同的background属性中设置background-size,你可以使用use:
background:url(my-bg.png) no-repeat top center / 50px 50px;