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
<img src="/static/imghwm/default1.png" data-src="haorooms.jpg" class="lazy" style="max-width:90%" / alt="How to use CSS to control front-end image HTTP requests" >
http request is as follows:
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
<img src="/static/imghwm/default1.png" data-src="haorooms.jpg" class="lazy" / alt="How to use CSS to control front-end image HTTP requests" > <img src="/static/imghwm/default1.png" data-src="haorooms.jpg" class="lazy" / alt="How to use CSS to control front-end image HTTP requests" >
http request is as follows:
Conclusion: All browsers only generate One request.
3. Repeat background
<style type="text/css"> .test1 { background: url(haorooms.jpg) } .test2 { background: url(haorooms.jpg) } </style> <p class="test1">test1</p> <p class="test2">test2</p>
http request is as follows:
Conclusion: All browsers only generate One request.
4. Background of non-existent element
<style type="text/css"> .test1 { background: url(haorooms.jpg) } .test2 { background: url(http2.jpg) } /* 页面中没有class为test2的元素 */ </style> <p class="test1">test1</p>
http request is as follows:
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
<style type="text/css"> .test1 { background: url(haorooms.jpg); display: none; } .test2 { background: url(http2.jpg); visibility: hidden; } </style> <p class="test1">test1</p>
http request is as follows:
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
<style type="text/css"> .test1 { background: url(haorooms.jpg); } .test1 { background: url(http2.jpg); } </style> <p class="test1">test1</p>
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:
<style type="text/css"> .test1 { background-image:url("haorooms.jpg"),url("http2.jpg"); } </style> <p class="test1">test1</p>
Then the http request is as follows:
7. Background loading of hover
<style type="text/css"> a.test1 { background: url(haorooms.jpg); } a.test1:hover { background: url(http2.jpg); } </style> <a href="#" class="test1">test1</a>http request is as follows:
8. Pictures in innerHTML in JS
<script type="text/javascript"> var el = document.createElement('p'); el.innerHTML = '<img src="/static/imghwm/default1.png" data-src="haorooms.jpg" class="lazy" / alt="How to use CSS to control front-end image HTTP requests" >'; //document.body.appendChild(el); </script>http request is as follows:
9. Image preloading
The most commonly used is the JS solution:
<script type="text/javascript"> new Image().src = 'haorooms.jpg'; new Image().src = 'http2.jpg'; </script>In an environment without JS support, hidden elements can be used to preload:
<img src="/static/imghwm/default1.png" data-src="haoroomscom.jpg" class="lazy" style="max-width:90%" / alt="How to use CSS to control front-end image HTTP requests" >
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!
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!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use
