Share the learning summary of Media Queries in CSS3
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP templating often gets a bad rap for facilitating subpar code — but that doesn't have to be the case. Let’s look at how PHP projects can enforce a basic

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.
