Media queries and HTTP requests

Source: Internet
Author: User
Tags datetime min query reflection version

Jason Grigsby published an article in which CSS media query for Mobile is fool ' Gold ' spits out media queries, to the effect that using media queries on mobile devices can cause a lot of wasted resources-- The browser requests a lot of resources such as images that are not in use, and then writes some test cases to test some of the available methods. Then Tim Kadlec wrote "Media Query & Asset downloading Results", using JS to automatically test the use case of Jason Grigsby.

This article is mainly organized from Tim's article. Let's see if we can waste resources and find the best solution.

Just look at the results.

Test One: IMG tags

Run Tests

This test attempts to hide the picture by using Display:none for the parent element of the IMG tag. HTML and CSS code are as follows:

1
2
3
     
1
2
3
@media all and (max-width:600px) {
	#test1 {Display:none}}
}

Test results

If there is a way to hide a picture that should be 100% avoided, that's display:none. It's basically no use. It seems that opera mobile and Opera Mini will not download pictures, and other browsers will download them. Opera can better control the download of resources, for users can not see the content, it will not be downloaded beforehand.

Browser Request Picture
Android 2.1+ Request
Blackberry (6.0+) Request
Chrome (4.1) + Request
Chrome Mobile Request
Fennec (10.0+) Request
Firefox (3.6+) Request
Ie Request
IOS (4.26+) Request
Kindle (3.0) Request
Opera (11.6+) Request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Rockmelt Request
Safari (4+) Request

Conclusion

It's simple: don't use it that way.

Test Two: background picture Display:none

Run Tests

In this case, the DIV is set to background-image. If the screen width is less than 600px,div, it is set to Display:none. HTML and CSS code are as follows:

1
     
1
2
3
4
5
6 7 8
#test2 {
	background-image:url (' images/test2.png ');
	width:200px;
	height:75px;
}
@media all and (max-width:600px) {
	#test2 {Display:none}}
}

Test results

The result is the same as the test: all browsers will download pictures except Opera Mini and opera Mobile and Firefox.

Browser Request Picture
Android 2.1+ Request
Blackberry (6.0+) Request
Chrome (4.1) + Request
Chrome Mobile Request
Fennec (10.0+) Request
Firefox (3.6+) Do not request
Ie Request
IOS (4.26+) Request
Kindle (3.0) Request
Opera (11.6+) Request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Rockmelt Request
Safari (4+) Request
Silk Request

Conclusion

Also: don't do that. However, like the rest of the tests, there are other ways to hide background images while avoiding unwanted requests.

Test Three: The parent element of the background picture is set to Display:none

Run Tests

In this test, you set the background picture for a div tag, and then set the Display:none for its parent element (also a div) when the browser width is less than 600px. HTML and CSS code are as follows:

1
2
3
     
1
2
3
4
5
6 7 8 9 10
#test3 Div {
	background-image:url (' images/test3.png ');
	width:200px;
	height:75px;
}
@media all and (max-width:600px) {
	#test3 {
		display:none
	}}
}

Test results

On the surface, this test seems to be less obvious than Test two, but the conclusion is that this method is more reliable ...

Browser Request Picture
Android 2.1+ Do not request
Blackberry (6.0+) Do not request
Chrome (16+) Do not request
Chrome Mobile Do not request
Fennec (10.0+) Request
Firefox (3.6+) Do not request
IE 9+ Do not request
IOS (4.26+) Do not request
Kindle (3.0) Do not request
Opera (11.6+) Do not request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Safari (4+) Do not request

Conclusion

This is a good method. In addition to immature Fennec, other browsers do not request pictures that need not be displayed.

test four: background picture cascade

Run Tests

In this test, a DIV is set with a background image. If the browser width is less than 600px, the div is given to another background picture. This test is used to detect whether two pictures will be requested or only requested. HTML and CSS code are as follows:

1
     
1
2
3
4
5
6 7 8 9 10
#test4 {
	background-image:url (' images/test4-desktop.png ');
	width:200px;
	height:75px;
}
@media all and (max-width:600px) {
	#test4 {
		background-image:url (' images/test4-mobile.png ');
	}
}

Test results

Better than setting display:none, the result of this method is a bit messy:

Browser Simultaneous Request
Android 2.1-3.0? Request
Android 4.0 Do not request
Blackberry 6.0 Request
Blackberry 7.0 Do not request
Chrome (16+) Do not request
Chrome Mobile Do not request
Fennec (10.0+) Request
Firefox (3.6+) Do not request
IE 9+ Do not request
IOS (4.26+) Do not request
Kindle (3.0) Request
Opera (11.6+) Do not request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Safari 4.0 Request
Safari 5.0+ Do not request

Conclusion

I will avoid using this method. While the environment is improving, the Android 2.x version, which dominates the Android market, still downloads two of images at the same time as Fennec and the Kindle. Among the three, especially because of Android (fragmentation), I would recommend looking for alternatives.

Test Five: Large background picture is set Min-width

Run Tests

In this test, a DIV element is set to a background picture when the browser is wider than 601px, and then set to a different background picture when the browser width is less than 600px. HTML and CSS code are as follows:

1
     
1
2
3
4
5
6 7 8 9 (14)
@media all and (min-width:601px) {
	#test5 {
		background-image:url (' images/test5-desktop.png ');
		width:200px;
		height:75px
	}
}
@media all and (max-width:600px) {
	#test5 {
		background-image:url (' images/test5-mobile.png ');
		width:200px;
		height:75px
	}
}

Test results

This scheme is a little bit better:

Browser Simultaneous Request
Android 2.1+ Do not request
Blackberry (6.0+) Do not request
Chrome (16+) Do not request
Chrome Mobile Do not request
Fennec (10.0+) Request
Firefox (3.6+) Do not request
IE 9+ Do not request
IOS (4.26+) Do not request
Kindle (3.0) Do not request
Opera (11.6+) Do not request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Safari (4+) Do not request

Conclusion

This time, more browsers are playing together. But Fennec as always. Android 2.x is a bit weird. It requests two pictures at the same time--but only when the screen width is greater than 600px matches to Min-width. This behavior appears to have been improved in the Android 3.0 version. It's a weird thing, and I wonder why it does that. Actually, there's good news. Jason Grigsby says his test results for this case are not the same as mine. So I ran the test on some Android 2.x machines. The conclusion is that my initial test results were not very correct, and the Android 2.x was doing well and I had a problem with the platform I was testing initially. This is not only good news for developers, but also for me to restore confidence in human beings ...

But this is still not enough, you will need to provide an alternative to the IE8 browsers, those versions of browsers do not support media query, so no pictures will be displayed. Of course, this problem can be easily compatible with conditional annotations.

Test VI: Background picture display:none (max-device-width)

Run Tests

This test is similar to test two, but uses max-device-width to replace Max-width. HTML and CSS code are as follows:

1
     
1
2
3
4
5
6 7 8 9 10
#test6 {
	background-image:url (' images/test6.png ');
	width:200px;
	height:75px;
}
@media all and (max-device-width:600px) {
	#test6 {
		display:none
	}}
}

Conclusion

Well, no time to waste, the test results are basically the same as the test two.

test Seven: Cascade Overlay High Resolution

Run Tests

The final test, designed for the new ipad, uses the Retina screen so that it uses a higher-resolution image.

In this case, a div is given to a background image. Then, by using the Min-device-pixel-ratio property, if the scale is greater than 1.5, a new background image will be used.

HTML and CSS code are as follows:

1
     
1
2
3
4
5
6 7 8 9 (15)
#test7 {
	background-image:url (' images/test7-lowres.png ');
	width:200px;
	height:75px;
}
@media only screens and (-webkit-min-device-pixel-ratio:1.5), only screen
and (-moz-min-device-pixel-ratio:1.5), C22/>only screen and (-O-MIN-DEVICE-PIXEL-RATIO:3/2), only screen and
(min-device-pixel-ratio:1.5) {
	#test7 {
		background-image:url (' images/test7-highres.png ');
		width:200px;
		height:75px
	}
}

Test results

Browser Simultaneous Request
Android 2.1-3.0? Request
Android 4.0 Do not request
Blackberry 6.0 Do not request
Blackberry 7.0 Do not request
Chrome (16+) Do not request
Chrome Mobile Do not request
Fennec (10.0+) Do not request
Firefox (3.6+) Do not request
IE 9+ Do not request
IOS (4.26+) Do not request
Kindle (3.0) Do not request
Opera (11.6+) Do not request
Opera Mini (6.5+) Do not request
Opera Mobile (11.5) Do not request
Safari 4.0+ Do not request

Conclusion

In order to be safe, this scheme can be tested a little more. It seems that this method is available in most cases. Unfortunately, it looks like Android 2.x will also download two images if the device pixel is greater than or equal to 1.5 (or any other value you set in media query). So, in this case, if you're using a high-resolution Android 2.x device, it's going to be a bit hard.

The good news is, I haven't heard of Android 2 in my current position. X's device has a screen ratio of more than 1.5. So if your project is for iOS devices that use the retina screen, you can set the Min-device-pixel-ratio to 2 or higher, which is a little bit safer ...

Recommended
    • If you want to hide a picture of the content, Display:none is invalid, so I recommend using JavaScript scheme or server-side implementation;
    • If you want to hide a background picture, the best way is to hide its parent element. If you're not comfortable doing this, cover it with a cascading style (like the fifth above), and then set the Background-image:none;
    • If you want to switch more than one picture, define them all in media query.
reflection on the design of response type

The biggest use of media inquiries now is responsive design, which is also because of the recent reflection on the efficiency of responsive design. With these test results, when implementing a Web site that responds to design, it is best to deal with mobile devices before upgrading to a high-resolution device. Then use the image and other external resources selector, be sure to write to the media query.

Feedback

If you think there are any errors in these test results, welcome in the comments, and then these test cases, Tim, are open source on the GitHub, and the interesting words can be fork ...



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.