CSS3 multimedia...LOGIN

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

##ValueDescriptionallFor all multimedia type devicesprintFor printersscreen Used for computer screens, tablets, smartphones, etc. speechFor 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:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
body {
    background-color: pink;
}
@media screen and (min-width: 480px) {
    body {
        background-color: lightgreen;
    }
}
</style>
</head>
<body>
<h1>重置浏览器窗口查看效果!</h1>
<p>如果媒体类型屏幕的可视窗口宽度小于 480 px ,背景颜色将改变。</p>
</body>
</html>

Run the program and try it


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

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8"> 
<title>php中文网(php.cn)</title> 
<style>
.wrapper {overflow:auto;}
#main {margin-left: 4px;}
#leftsidebar {float: none;width: auto;}
#menulist {margin:0;padding:0;}
.menuitem {
    background:#CDF0F6;
    border:1px solid #d4d4d4;
    border-radius:4px;
    list-style-type:none;
    margin:4px;
    padding:2px;
}
@media screen and (min-width: 480px) {
    #leftsidebar {width:200px;float:left;}
    #main {margin-left:216px;}
}
</style>
</head>
<body>
<div class="wrapper">
  <div id="leftsidebar">
    <ul id="menulist">
      <li class="menuitem">Menu-item 1</li>
      <li class="menuitem">Menu-item 2</li>
      <li class="menuitem">Menu-item 3</li>
      <li class="menuitem">Menu-item 4</li>
      <li class="menuitem">Menu-item 5</li>
   </ul>
  </div>
  <div id="main">
    <h1>重置浏览器窗口查看效果!</h1>
    <p>在屏幕可视窗口尺寸小于 480 像素时将菜单浮动到页面左侧。</p>
  </div>
</div>
</body>
</html>

Run the program and try it



<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .wrapper {overflow:auto;} #main {margin-left: 4px;} #leftsidebar {float: none;width: auto;} #menulist {margin:0;padding:0;} .menuitem { background:#CDF0F6; border:1px solid #d4d4d4; border-radius:4px; list-style-type:none; margin:4px; padding:2px; } @media screen and (min-width: 480px) { #leftsidebar {width:200px;float:left;} #main {margin-left:216px;} } </style> </head> <body> <div class="wrapper"> <div id="leftsidebar"> <ul id="menulist"> <li class="menuitem">Menu-item 1</li> <li class="menuitem">Menu-item 2</li> <li class="menuitem">Menu-item 3</li> <li class="menuitem">Menu-item 4</li> <li class="menuitem">Menu-item 5</li> </ul> </div> <div id="main"> <h1>重置浏览器窗口查看效果!</h1> <p>在屏幕可视窗口尺寸小于 480 像素时将菜单浮动到页面左侧。</p> </div> </div> </body> </html>
submitReset Code
ChapterCourseware