Home  >  Article  >  Web Front-end  >  Share the learning summary of Media Queries in CSS3

Share the learning summary of Media Queries in CSS3

高洛峰
高洛峰Original
2017-03-10 09:41:491645browse

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
分享CSS3中的Media Queries的学习总结

分享CSS3中的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

   
   
   
   


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) {   
  ...   
}


What should I do? Online However, many people have proposed solutions. The simplest method is:

IE8 and previous browsers do not support CSS3 media queries. You can add css3-mediaqueries.js to the page to solve this problem.


It turns out that it is quite simple, but the result is very disappointing. No matter how I tried it in the project, it didn’t work. I hope colleagues who have tried it can give me some advice. , I have no choice but to use another slightly more complicated method, which I also learned from the Internet. There are two issues to consider here. One is that only IE8 and its lower versions can do this, and the other is that only the browser can be reduced to a certain This processing is done only after the size range has been determined. The method is as follows:

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("");   
           document.write("");   

       }   
   }    
}   
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!

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 [email protected]