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!

如何解决Java开发中的HTTP请求连接被拒绝问题在进行Java开发中,经常会遇到HTTP请求连接被拒绝的问题。这种问题的出现可能是由于服务器端限制了访问权限,或是网络防火墙阻止了HTTP请求的访问。解决这个问题需要对代码和环境进行一些调整。本文将介绍几种常见的解决方法。检查网络连接和服务器状态首先,确认你的网络连接是正常的,可以尝试访问其他的网站或服务,看

PHP是一种广泛使用的编程语言,其中一个常见的应用就是发送电子邮件。在这篇文章中,我们将讨论如何使用HTTP请求发送邮件。我们将从以下几个方面来介绍这个主题:什么是HTTP请求发送邮件的基本原理使用PHP发送HTTP请求发送邮件的示例代码什么是HTTP请求HTTP请求是指发送到web服务器的请求,以获取web资源。HTTP是一种协议,用于在web浏览器和we

从头到尾:如何使用php扩展cURL进行HTTP请求引言:在Web开发中,经常需要与第三方API或其他远程服务器进行通信。而使用cURL进行HTTP请求是一种常见而强大的方式。本文将介绍如何使用php扩展cURL来执行HTTP请求,并提供一些实用的代码示例。一、准备工作首先,确保php已安装cURL扩展。可以在命令行执行php-m|grepcurl查

如何使用Nginx进行HTTP请求的压缩和解压缩Nginx是一款高性能的Web服务器和反向代理服务器,其功能强大且灵活。在处理HTTP请求时,可以使用Nginx提供的gzip和gunzip模块对请求进行压缩和解压缩,以减小数据传输量,提高请求响应速度。本文将介绍如何使用Nginx进行HTTP请求的压缩和解压缩的具体步骤,并提供相应的代码示例。配置gzip模块

Nginx如何实现HTTP请求的重试配置,需要具体代码示例Nginx是一款非常流行的开源反向代理服务器,它拥有强大的功能和灵活的配置选项,可以用来实现HTTP请求的重试配置。在网络通信中,由于各种原因,例如网络延迟、服务器负载等,有时候我们发起的HTTP请求可能会失败。为了提高应用程序的可靠性和稳定性,我们可能需要在请求失败时进行重试。下面将介绍如何使用Ng

如何使用PHP和Exif扩展来提取照片的测光模式摄影是一种以图像为媒介的艺术形式,在数字摄影时代,我们可以通过照片的Exif数据来获取有关拍摄参数的详细信息。其中,测光模式是一个重要的参数,它可以告诉我们照片是如何进行光线测量的,帮助我们更好地理解和分析照片。在PHP编程中,我们可以使用Exif扩展来提取照片的Exif数据。本文将介绍如何使用PHP和Exif

如何使用golang中的http.Client进行HTTP请求的高级操作引言:在现代开发中,HTTP请求是不可避免的一部分。golang提供了强大的标准库,其中包含了http包。http包提供了http.Client结构体,用于发送HTTP请求和接收HTTP响应。在本文中,我们将探讨如何使用http.Client进行HTTP请求的高级操作,并提供具体的代码示

标题:使用Java11中的HttpClient发送HTTP请求并处理响应引言:在现代的互联网应用程序中,与其他服务器进行HTTP通信是非常常见的任务。Java提供了一些内置的工具,可以帮助我们实现这一目标,其中最新且推荐使用的是Java11中引入的HttpClient类。本文将介绍如何使用Java11中的HttpClient发送HTTP请求并处理响应,


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
