CSS3 Media Queries

Source: Internet
Author: User

CSS Media queries literal translation of the "Media query", we can easily understand the media device to show the different styles.

For example, you can put styles for display on large screens and special styles for mobile devices in a style document so that different devices can render different interface appearances without changing the contents of the document.

Take a look at this online demo , resize your browser window and see how it changes. The control element color change is not JS, but CSS3 Media Queries.

Let's start with a simple example:

<link rel= "stylesheet" media= "screen and (max-width:600px)" href= "Small.css"/>

The media statement above represents: When the page width is less than or equal to 600px, call the SMALL.CSS style sheet to render your Web page. First look at what the media statement contains:

1,screen: This needless to say that we all know, refers to a type of media;

2,and: is called keywords, and similar to the not,only, will be introduced later;

3,(max-width:600px): This is the media characteristics, the popular point is that the media conditions.

The previous simple example leads to two conceptual things, one being the media type and media Query, first to understand the two concepts together:

Media type

Media type is a common attribute in Css2 and is a very useful property that can be used to assign different styles to different devices through the media type, and in CSS2 we often encounter all (full), screen, Print (page print or hit Qiu preview mode), in fact, there are more than three types of media type, the total list of 10 media types.

There are also several ways to introduce media types in the page:

1, link method introduced

<link rel= "stylesheet" type= "Text/css" href= ". /css/print.css "media=" print "/>

2, the introduction of XML method

<?xml-stylesheet rel= "stylesheet" media= "screen" href= "Css/style.css"? >

3. Introduction of @import method

@import introduced in two ways, one is to call a style file through @import in the style file, and the other way is to introduce it in <style>...</style> in

Another style file is called in the style file:

12 @import url("css/reset.css"screen;@import url("css/print.css"print;

  

Called in <style>...</style> in

12345    <style type="text/css">   @import url("css/style.css"all;   </style> 

  

4, @media introduced

This type of introduction is the same as @import, and there are two ways:

Used in style files:

12345 @media screen{     选择器{    属性:属性值;     }   }

  

Called in <style>...</style> in

123456789 <HEAD> &NBSP;&NBSP; <style type= " Text/css " > &NBSP;&NBSP; screen { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; selector { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Code class= "CSS Plain" "Properties: Property values; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; } &NBSP;&NBSP; } &NBSP;&NBSP; </STYLE>

  

The above several methods have their own pros and cons, in the practical application I suggest the use of the first and fourth, because these two methods are in the project is commonly used in the production of methods, for their specific differences, I do not say, want to know the people can go to find degrees Niang or G dad, they can help you solve.

  

Media features (medium Query)

As mentioned earlier, media query is an enhanced version of media type CSS3, in fact, you can think of media query as a media type (judging condition) +CSS (conditional style rules), commonly used features are listed in 13. Refer to: Media features for more details. To get a better understanding of media Query, we go back to the previous instance:

<link rel= "stylesheet" media= "screen and (max-width:600px)" href= "Small.css"/>

The syntax for converting to CSS is:

12345 @media screenand (max-width600px) {    选择器 {      属性:属性值;    }  }

  

In fact, the style in the Small.css file is placed in the @media Srceen and (max-width;600px) {...} Curly braces. In the statement structure above the statement, you can see that the media query and CSS Properties collection are similar, the main differences are:

1, Media query accepts only a single logical expression as its value, or no value;

2. CSS properties are used to declare how the page information is represented, and media query is an expression that determines whether the output device satisfies a certain condition;

3. Media query Most of these accept the Min/max prefix, which is used to represent their logical relationship, and to indicate a condition that is greater than or equal to or less than or equal to a value

4, CSS properties require that property values must be, Media query can have no value, because its expression returns only true or False two

Common Media Query is shown in the following table:

Compatible Browsers:

Let's take a look at the specific use of media queries

One, max width max

<link rel= "stylesheet" media= "screen and (max-width:600px)" href= "Small.css" type= "Text/css"/>

The above indicates that when the screen is less than or equal to 600px, the Web page is rendered in the Small.css style.

Second, min width min

<link rel= "stylesheet" media= "screen and (min-width:900px)" href= "Big.css" type= "text/css"  />

The above indicates that when the screen is greater than or equal to 900px, the Web page is rendered in the Big.css style.

Three, multiple media queries use

<link rel= "stylesheet" media= "screen and (min-width:600px) and (max-width:900px)" href= "Style.css" type= "text/css"/ >

Media query can combine multiple media queries, in other words, a media query can contain between 0 and more expressions, and the expression can contain between 0 and more keywords, as well as a media Type. As it says above, when the screen is between 600px-900px, the STYLE.CSS style is used to render the Web page.

Iv. output width of device screen

<link rel= "stylesheet" media= "screen and (max-device-width:480px)" href= "Iphone.css" type= "Text/css"/>

The above code refers to the IPHONE.CSS style for the maximum device width of 480px, such as the display on the iphone, where Max-device-width refers to the actual resolution of the device, that is, the visual area resolution

Wu, IPhone4

<link rel= "stylesheet" media= "only screens and (-webkit-min-device-pixel-ratio:2)" type= "Text/css" href= " Iphone4.css "/>

The above style is written specifically for IPhone4 mobile devices.

VI. IPad

<link rel= "stylesheet" media= "All and (orientation:portrait)" href= "Portrait.css" type= "Text/css"/>   < Link rel= "stylesheet" media= "All and (Orientation:landscape)" href= "Landscape.css"  type= "Text/css"/>

In the case of large numbers, the mobile device on the ipad is the same as on the iphone, but the difference is that the ipad declares a different direction, such as the above example, in Portrait (portrait) Use PORTRAIT.CSS to render the page, and use LANDSCAPE.CSS to render the page in landscape (landscape).

Vii. Android

/*240px width */<link rel= "stylesheet" media= "only screens and (max-device-width:240px)" href= "Android240.css" type= " Text/css "/>/*360px width */<link rel=" stylesheet "media=" only screen and (min-device-width:241px) and ( max-device-width:360px) "href=" Android360.css "type=" text/css "/>/*480px width */<link rel=" stylesheet "media=" Only screen and (min-device-width:361px) and (max-device-width:480px) "href=" Android480.css "type=" Text/css "/>

We can use media query to provide specific styles for Android phones at different resolutions, so that the screen resolution can be resolved differently for Android phone page refactoring issues.

Eight, not keyword

<link rel= "stylesheet" media= "not print and (max-width:1200px)" href= "Print.css" type= "Text/css"/>

The NOT keyword is used to exclude a certain type of media, in other words, to exclude devices that match an expression.

Nine, only keyword

<link rel= "stylesheet" media= "only screens and (max-device-width:240px)" href= "Android240.css" type= "Text/css"/>

Only used to set a specific media type that can be used to exclude browsers that do not support media queries. In fact, only many times it is used to hide style sheets for devices that do not support media query but support media type. The main features are: devices that support media Queries, the normal invocation style, when only does not exist, and for devices that do not support media features (media Queries) but that support the medium type, which will not read the style, Because its first reads only instead of screen, and the browser does not support media qqueries, the style will not be adopted, whether or not it supports only.

X. Other

If media Type is not explicitly specified in media query, it defaults to all, such as:

<link rel= "stylesheet" media= "(min-width:701px) and (max-width:900px)" href= "Medium.css" type= "Text/css"/>

There is also a comma (,) that is used to denote a juxtaposition or representation or, as follows

<link rel= "stylesheet" type= "Text/css" href= "Style.css" media= "handheld and (max-width:480px), screen and (min-width : 960px) "/>

The style.css style in the code above is used on a handheld device with a width less than or equal to 480px, or on a device with a screen width greater than or equal to 960px.

The section on the use of Media query is described here, and in the end the overall regulation of its function, the personal think is a word:Media queries can use different styles under different conditions, use the page to achieve different rendering effect. the next section will introduce several instances of media queries, and if interested friends remember to watch this site update.

  

Browsers that do not support CSS3

For now different products also require compatibility does not support CSS3 browser, can only write JS to make a judgment

123 <!--[if lt IE 9]><script src=“"></script><![endif]-->

 

Horizontal Screen & Vertical screen
123456789 /*横屏时*/@media all and (orientation : landscape) {    /**/}/*竖屏时*/@media all and (orientation : portrait){    /**/}

  

CSS3 Media Queries

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.