CSS3: MediaQueries-majiangl Media Queries is one of the most revolutionary features of CSS3, which enables our website to adapt to a variety of devices. Specifically, developers can use Media Queries to identify the device environment, and then apply different CSS rules based on different device environments, so that they can be implemented under different devices, use different la S and skins.
Syntax
There are three ways to apply Media Queries:
1. Use media attributes when introducing css files
2. Use the @ import command in the css file
@import url('file.css') logic media and (expression);
3. Use the @ media command in the css file
@media logic media and (expression) { rules }
When the device environment complies with the logic media and (expression) description, the corresponding css will take effect or be applied. In the first case, the entire file.css will be effective. In the second case, the entire file.css will take effect. In the third case, all css in braces will take effect.
LogicThe value can be not, only.
MediaMedia type. screen and print are the most common media types, which respectively indicate the screen environment and the print environment. The default value is all.
ExpressionIt is the judgment of device features, such as height and width.
/* In the print environment, print.css takes effect */
/* Notprint.css applies to non-printing environments */@ import url('notprint.css ') not print;/* only when the window size is at least 1000px in the screen environment, the corresponding css takes effect */@ media only screen and (min-width: 1000px) {# example {background-color: red ;}/ * multiple media queries, separate */@ media screen, print {# example {background-color: red ;}}
Media Features
The expression above is the Media Features expression used to describe the Features of a device. The syntax is @ media (feature: value) {rules }. Feature indicates the feature name and value indicates its value. For example, see the preceding section.
Below are all the Features provided by Media Queries. I have sorted them by practicality.
Width and Height
Indicates the width and height of the webpage rendering window, that is, the size of the browser display area.
@media (width: 600px) { rules }
The feature value supports both the max-and min-prefixes, for example:
@media (max-width: 480px) { rules }@media (min-width: 640px) { rules }
Pixel Ratio
Pixel Ratio or resolution. Here, Pixel Ratio is actually Device Piexel Ratio (DPR), indicating the Ratio of physical Pixel to logical Pixel.
For iPhone 5S, the physical resolution is 640*1136, but its CSS resolution is 320*568. That is to say, its logical resolution is 320*568, And DPR is 2, that is, two horizontal physical pixels and two vertical physical pixels constitute a logical pixel.
: DPR 1 (left), DPR 2 (middle), DPR 3 (right)
In most cases, developers do not need to care about the physical resolution of the device. Both text and vector graphs can perform well, but non-vector images may suffer severe distortion under high-split screens. A common image, on a device with a DPR of 2, is equivalent to a picture that is zoomed twice, and the user can obviously feel blurred.
In this case, Media Queries comes in handy. You can use this syntax to determine the device's DPR @ media and (resolution: value) {rules }.
Value units: dots per inch (DPI), dots per centimeter (DPCM), or more intuitive to us, dots per pixel (DPPX ).
@media (resolution: 1.5dppx) { rules }@media (max-resolution: number) { rules }@media (min-resolution: number) { rules }
Therefore, when DPR = 2, we can use a 2x resolution image:
1 p { background-image: url('image.png'); }2 @media (resolution: 2dppx) {3 p { background-image: url('image-2x.png'); }4 }
Compatibility
Chrome, Firefox, and IE 10 + support resolution Media Feature, but IE does not support DPPX units. To be compatible with IE, DPI can be used as the unit (standard screen DPI is 96), for example:
@media (resolution: 1.5dppx), (resolution: 144dpi) { rules }
In addition, Safari does not support resolution. Use-webkit-device-pixel-ratio instead:
@media (resolution: 1.5dppx), (resolution: 144dpi), (-webkit-device-pixel-ratio: 2) { rules }
Device Width and Height
These two attributes detect the width and height of the device screen, and the practical value is not high. The width and height here refer to the physical pixels of the device.
@media (device-width: number) { rules }@media (device-height: number) { rules }@media (max-device-width: number) { rules }@media (max-device-height: number) { rules }@media (min-device-width: number) { rules }@media (min-device-height: number) { rules }
Orientation
Device orientation: landscape or portrait. When the width is greater than the height, the device is in landscape mode; otherwise, the device is in portrait mode. This function is useful for handheld devices.
@media (orientation: landscape) { rules }@media (orientation: portrait) { rules }
Aspect Ratio
Aspect Ratio, such as 16/9, 16/10, and so on. Aspect-ratio indicates the aspect ratio of the browser, while device-aspect-ratio indicates the aspect ratio of the device.
@media (aspect-ratio: horizontal/vertical) { rules }@media (device-aspect-ratio: horizontal/vertical) { rules }@media (min-aspect-ratio: horizontal/vertical) { rules }@media (min-device-aspect-ratio: horizontal/vertical) { rules }@media (max-aspect-ratio: horizontal/vertical) { rules }@media (max-device-aspect-ratio: horizontal/vertical) { rules }
@media (min-device-aspect-ratio: 16/9) {...}
Note that the iPhone returns the same aspect ratio in landscape and portrait modes, while other devices return different aspect ratios in different Orientation modes.