多次指定相同的 CSS 背景

Same CSS background specified multiple times

如果您在不同的 CSS 选择器中指定相同的图像,它会单独下载还是稍后 url 将使用第一个?

.a {
   background: url('image1.png');
}

...

.b {
   background: url('image1.png');
}

实际上在所有的浏览器中,我们都有浏览器缓存。一旦加载,将不会再次加载。如果文件大小和名称相同,则无法再次加载。您可以使用 firebug 或 developertools 进行跟踪。

Try it — when looking into caching issues, a tool like Firebug for Firefox or the Developer Tools within Chrome are very beneficial. If you open the 'Net' panel in either and reload a page, you will see what HTTP status code was sent for each item. 304 (Not modified) means that the item was retrieved locally from the cache.

As dthorpe says above, cache headers are important here. As well as making sure that 'no-cache' hasn't been set, if you have access to your server configuration you should be pro-active — if you know a resource isn't going to change you should make sure to set either an 'Expires' header (which tells browsers a date after which the cached copy should be considered stale) or a 'Cache-Control: max-age' header (which gives a number of days/hours rather than a set date).

You can set different time-scales for different mime-types/folders too, which allows you to get clients data to refresh HTML content often, but images and stylesheets rarely.

Here's a nice intro video/article by Google that's worth checking out.

突出显示的是此 stackoverfow 问题的正确答案。如果您尝试了解 Web 浏览器的工作原理,您将会很感兴趣。