可以有两个背景图片吗?例如,我想有一个图像在顶部重复(repeat-x),另一个在整个页面重复(repeat),其中一个在整个页面是后面的一个在顶部重复。

我发现我可以通过设置html和body的背景来实现两个背景图像的预期效果:

html {
    background: url(images/bg.png);
}

body {
    background: url(images/bgtop.png) repeat-x;
}

这是“好的”CSS吗?有没有更好的方法?如果我想要三个或更多的背景图像呢?

I want to show an image from an URL with a certain width and height even if it has a different size ratio. So I want to resize (maintaining the ratio) and then cut the image to the size I want. I can resize with html img property and I can cut with background-image. How can I do both? Example: This image: Has the size 800x600 pixels and I want to show like an image of 200x100 pixels With img I can resize the image 200x150px: <img style="width: 200px; height: 150px;" src="http://i.stack.imgur.com/wPh0S.jpg"> That gives me this: <img style="width: 200px; height: 150px;" src="https://i.stack.imgur.com/wPh0S.jpg"> And with background-image I can cut the image 200x100 pixels. <div style="background-image: url('https://i.stack.imgur.com/wPh0S.jpg'); width:200px; height:100px; background-position:center;">&nbsp;</div> Gives me: <div style="background-image:url('https://i.stack.imgur.com/wPh0S.jpg'); width:200px; height:100px; background-position:center;">&nbsp;</div> How can I do both? Resize the image and then cut it the size I want?

我有一个背景图像在下面的div,但图像被切断:

 <div style='text-align:center;background-image: url(/media/img_1_bg.jpg);background-repeat:no-repeat;width:450px;height:900px;' id="mainpage" align="center">

有没有办法显示背景图像而不切断它?

我正在查看一个greasemonkey用户脚本的源代码,并注意到他们的css中有以下内容:

.even { background: #fff url(data:image/gif;base64,R0lGODlhBgASALMAAOfn5+rq6uvr6+zs7O7u7vHx8fPz8/b29vj4+P39/f///wAAAAAAAAAAAAAAAAAAACwAAAAABgASAAAIMAAVCBxIsKDBgwgTDkzAsKGAhxARSJx4oKJFAxgzFtjIkYDHjwNCigxAsiSAkygDAgA7) repeat-x bottom}

我可以理解greasemonkey脚本想要在源代码中捆绑任何东西,而不是将其托管在服务器上,这是足够明显的。但由于我以前没有见过这种技术,我考虑了它的使用,它似乎很有吸引力,原因有很多:

它将减少页面加载中的HTTP请求数量,从而提高性能 如果没有CDN,那么它将减少通过与图像一起发送的cookie产生的流量 CSS文件可以被缓存 CSS文件可以被gzip压缩

考虑到IE6(例如)在缓存背景图像方面存在问题,这似乎不是最糟糕的主意……

那么,这是一个好还是坏的做法,为什么不使用它,你会使用什么工具来base64编码图像?

更新-测试结果

用图像测试:http://fragged.org/dev/map-shot.jpg - 133.6Kb 测试网址:http://fragged.org/dev/base64.html 专用CSS文件: http://fragged.org/dev/base64.css 178.1 kb GZIP编码服务器端 结果大小发送到客户端(YSLOW 元件测试):59.3Kb 保存发送到客户端浏览器的数据:74.3Kb

很好,但我想对于较小的图像来说,它就不那么有用了。

更新:Bryan McQuade,谷歌的软件工程师,致力于PageSpeed,在2013年ChromeDevSummit上表示,CSS中的data:uris被认为是一种渲染阻塞反模式,用于交付关键/最小的CSS,在他的演讲#perfmatters: Instant mobile web apps中。请访问http://developer.chrome.com/devsummit/sessions,并记住这是实际的幻灯片

我有一个JPEG文件,我使用作为搜索页面的背景图像,我使用CSS来设置它,因为我在Backbone.js上下文中工作:

background-image: url("whatever.jpg");

我想应用css3模糊过滤器只对背景,但我不确定如何样式只是一个元素。如果我试着:

-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);

就在我的CSS中的background-image下面,它为整个页面设置了样式,而不仅仅是背景。是否有一种方法只选择图像并应用过滤器?或者,有没有一种方法可以关闭页面上所有其他元素的模糊效果?

在什么情况下使用HTML IMG标记来显示图像更合适,而不是CSS背景图像,反之亦然?

这些因素可能包括可访问性、浏览器支持、动态内容或任何类型的技术限制或可用性原则。

我如何使用CSS3渐变为我的背景颜色,然后应用背景图像应用某种轻透明纹理?