简体   繁体   English

CSS文件何时检索background-img URL?

[英]When does css file retrieve background-img URL?

When a .css file uses a url for an image file, does the client retrieve that image when the .css file is loaded or does it wait until the specific class is assigned to an element? 当.css文件使用图像文件的url时,客户端是否会在加载.css文件时检索该图像,还是等待直到将特定类分配给元素?

For example - 例如 -

A user loads home.html with style.css referenced in the header. 用户使用标头中引用的style.css加载home.html。 The home page does not include any elements with class attach_background_image , but within style.css we have 主页不包含任何带有attach_background_image类的attach_background_image ,但是在style.css中,我们拥有

.attach_background_image{
    background-image:URL(background.png);
}

Will background.png be retrieved when home.html is hit? 当点击home.html时,会检索background.png吗? Or does the image only get requested when an element meets the css selector criteria? 还是仅当元素满足CSS选择器条件时才请求图像?

当加载类.attach_background_image时,将在页面加载时检索它

The whole CSS-file is loaded on page-load, 整个CSS文件都在页面加载时加载, so that's when the file will be retrieved. 这样就可以检索文件了。 That the CSS in the file isn't actually used anywhere doesn't matter. 文件中的CSS实际上没有在任何地方使用都没关系。

EDIT Sorry, I realised the original answer was wrong - the CSS file is loaded (all of it), but the external resources are loaded on a on-use basis, so until that class or element is present on the page, it will not run the GET-request to fetch it. 编辑对不起,我意识到原来的答案是错的- CSS文件被加载(所有),但外部资源装载于上使用的基础上,所以直到这个类或元素出现在页面上,也不会运行GET请求以获取它。

That means that if you for instance have a javascript change classes, the image will get retrieved, which of course can lead to slightly longer load-times than expected (depending on where the resource is located, and the size of the image) 这意味着,例如,如果您具有javascript更改类,则将检索图像,这当然会导致加载时间比预期的稍长(取决于资源所在的位置以及图像的大小)

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

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