Media Queries in CSS3 are often used to create front-end responsive design pages. Here we share a learning summary of Media Queries in CSS3, including solutions to compatibility issues in IE8. Friends in need can refer to the following
1. Attributes supported by Media Queries
##2. Keywords
and - Combining multiple media queriesnot - to exclude a certain specified media type only - to specify a specific media type
3. Reference style examples
<link rel="stylesheet" media="all" href="style.css" /> <link rel="stylesheet" media="screen and (min-width:980px)" href="desktop.css" /> <link rel="stylesheet" media="screen and (min-width:768px) and (max-width:980px)" href="pad.css" /> <link rel="stylesheet" media="screen and (min-width:480) and (max-width: 768px)" href="phone.css" /> <link rel="stylesheet" media="screen and (max-width:320px)" href="small.css" />
4. Inline style example
@media screen and (min-width: 980px) { //css code } @screen and (min-width:768px) and (max-width:980px) { //css code } @screen and (min-width:480) and (max-width: 768px) { //css code } @screen and (max-width:320px) { //css code } @media screen and (max-device-width: 480px) { //max-device-width等于480px } @media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) { //设备宽高比 } @media all and (orientation:portrait) { //竖屏 } @media all and (orientation:landscape) { //横屏 }
5. Solving the compatibility problem of I8
The root of the problem:The media is used in the CSS file of the project to automatically adjust the layout according to the size of the window. However, IE8 and below browsers do not support it. CSS3 media queries, that is, the css code in @media screen and (max-width: 900px) is not executed.
@media screen and (max-width: 900px) { ... }
IE8 and previous browsers do not support CSS3 media queries. You can add css3-mediaqueries.js to the page to solve this problem.
<!--[if lt IE 9]> <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> <![endif]-->
Principle: Use jquery. If you don’t understand it, just know how to use it. Then change the corresponding CSS file in the html as needed.
Solution:
Add the following after the common js file for all pages Code:
/*Adjust the layout in IE8 and lower than IE8 when the browser is narrow*/ function processLowerIENavigate() { var isIE = document.all ? 1 : 0; if (isIE == 1) { if(navigator.userAgent.indexOf("MSIE7.0") > 0 || navigator.userAgent.indexOf("MSIE 8.0") > 0) { // var doc=document; var link=document.createElement("link"); link.setAttribute("rel", "stylesheet"); link.setAttribute("type", "text/css"); link.setAttribute("id", "size-stylesheet"); link.setAttribute("href", ""); var heads = document.getElementsByTagName("head"); if(heads.length) heads[0].appendChild(link); else document.documentElement.appendChild(link); document.write("<script type='text/javascript' src='jquery.min.js'></script>"); document.write("<script type='text/javascript' src='media_screen.js'></script>"); } } } var lowerIE8 = processLowerIENavigate(); media_screen.js文件内容如下,直接从网上copy: function adjustStyle(width) { width = parseInt(width); if (width < 902) { //alert("<900"); //alert(width); $("#size-stylesheet").attr("href", "navigateLowerIE8.css"); } else { // alert(">900"); //alert(width); $("#size-stylesheet").attr("href", ""); } } $(function() { adjustStyle($(this).width()); $(window).resize(function() { adjustStyle($(this).width()); }); });
##navigateLowerIE8.css file is the page layout of IE8 and its lower version browser when it is reduced. OK, everything is indeed done.
The above is the detailed content of Share the learning summary of Media Queries in CSS3. For more information, please follow other related articles on the PHP Chinese website!