CSS3 multimedia queries
CSS3 Multimedia Query
CSS2 Multimedia Type
@media rules are introduced in CSS2, and different style rules can be customized for different media types.
For example: You can set different style rules for different media types (including monitors, portable devices, TVs, etc.).
But these multimedia types are not friendly enough to support on many devices.
CSS3 Multimedia Query
CSS3 multimedia query inherits all the ideas of CSS2 multimedia types: instead of looking for the type of device, CSS3 based on the settings Adaptive display.
Media queries can be used to detect many things, such as:
The width and height of the viewport (window) The width and height of the device Orientation (smartphone landscape, portrait). Resolution
Currently, many devices such as Apple phones, Android phones, and tablets use multimedia queries.
Multimedia query syntax
Multimedia query consists of a variety of media and can contain one or more expressions. The expressions are based on Returns true or false whether the condition is true.
@media not|only mediatype and (expressions) {
CSS-Code;
}
If the specified multimedia type matches the device type, the query result returns true, and the document will display the specified style effect on the matching device.
Unless you use the not or only operators, all styles will adapt to display on all devices.
not: not is used to exclude certain specific devices, such as @media not print (non-printing devices).
only: Used to define a special media type. For mobile devices that support Media Queries, if the only keyword exists, the mobile device's web browser will ignore the only keyword and directly apply the style file based on the following expression. For devices that do not support Media Queries but are capable of reading Media Type web browsers, this style file will be ignored when the only keyword is encountered.
all: All devices, this should be seen often.
You can also use different style files on different media:
CSS3 Multimedia Type
Description | |
---|---|
For all multimedia type devices | |
For printers | |
Used for computer screens, tablets, smartphones, etc. | |
For screen readers |
Multimedia queries Simple example
Use multimedia query to replace the original style with the corresponding style on the specified device. In the following example, modify the background color on a device with a screen viewable window size greater than 480 pixels:Run the program and try itphp中文网(php.cn) 重置浏览器窗口查看效果!
如果媒体类型屏幕的可视窗口宽度小于 480 px ,背景颜色将改变。
The following example is available on the screen. Float the menu to the left side of the page when the view window size is greater than 480 pixels:
Example
Run the program and try itphp中文网(php.cn) 重置浏览器窗口查看效果!
在屏幕可视窗口尺寸小于 480 像素时将菜单浮动到页面左侧。