Home  >  Article  >  Web Front-end  >  How to use CSS to control front-end image HTTP requests

How to use CSS to control front-end image HTTP requests

不言
不言Original
2018-06-20 11:10:221740browse

There are many ways to load Web front-end images, and the HTTP requests they generate are also different. Here we will list various examples of CSS controlling HTTP requests for front-end images. Friends in need can refer to the following

There are many situations for http requests for images. So under what circumstances will a request not occur? Below I will list them one by one using cases, hoping it will be helpful for you to have a deeper understanding of http image requests.

1. Hidden pictures

http request is as follows:

2016616105558967.jpg (1067×402)

Conclusion: Only Opera does not generate requests. Note: When hiding an image using visibility: hidden, a request will also be generated under Opera.

2. Duplicate images


http request is as follows:

2016616105640229.jpg (1264×371)

Conclusion: All browsers only generate One request.

3. Repeat background

   

test1

test2

http request is as follows:

2016616105713640.jpg (1049×382)

Conclusion: All browsers only generate One request.

4. Background of non-existent element

   

test1

http request is as follows:

2016616105755887.jpg (1125×326)

Conclusion: Background only The request is only made when the applied element exists on the page. This makes sense for CSS frameworks.

5. Hiding the background of elements

   

test1

http request is as follows:

2016616105836960.jpg (1228×324)

Conclusion: Opera and Firefox are for The background of an element hidden with display: none will not generate an HTTP request. A background image will only be requested if these elements are not display: none.

6. Multiple backgrounds

   

test1

The http request of the above code will only request the image http2.jpg. The reason is that the class of test1 changes the above It’s overwritten, so we only request the next picture!

If you use css3 to write multiple background images:

   

test1

Then the http request is as follows:

2016616105913578.jpg (1370×346)

##The webkit engine browser handles background images All requested because it supports multiple background images in CSS3.

7. Background loading of hover

   
test1

http request is as follows:

2016616105947450.jpg (1203×343)

Conclusion: When hover is triggered, Only then will the background in the hover state be requested. If not triggered, only the default background image is requested.

8. Pictures in innerHTML in JS

http request is as follows:


2016616110110394.jpg (1257×329)

Conclusion: Only Opera will not immediately Request pictures.

Note: Opera will only send the request when it is added to the DOM tree.

9. Image preloading
The most commonly used is the JS solution:

In an environment without JS support, hidden elements can be used to preload:

Summary
1. Opera will not generate requests for hidden pictures and backgrounds of hidden elements.
2. Firefox will not generate requests for the background of hidden elements.
3. Opera will not generate a request for img elements that have not been inserted into the DOM tree.
4. Safari and Chrome based on webkit engine support multiple background images.
5. In other scenarios, all mainstream browsers remain the same.
I hope the above image http request will be helpful to you, everyone can leave messages to communicate with each other!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the adaptive method of css to achieve fixed width on the right and width on the left

Use html and css to implement Cornell note template

The above is the detailed content of How to use CSS to control front-end image HTTP requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn