Example code for using @media in CSS3 to achieve web page adaptation

Example code for using @media in CSS3 to achieve web page adaptation

Nowadays, the screen resolution of computer monitors is developing towards higher and higher, but the resolution of mobile device terminals such as mobile phones cannot be that much higher. More and more websites are starting to adapt their pages to various resolutions, displaying basic content at small resolutions, full functions at large resolutions, and even multiple versions at multiple levels.

1. @media in CSS2

Although CSS2 supports the @media attribute, it can only implement a few functions and is generally only used to define special CSS for printing.

@media sMedia { sRules }

1.1 Examples

// Set the font size for the display @media screen {
BODY {font-size:12pt; }
}
// Set the font size for the printer @media print {
@import "print.css"
BODY {font-size:8pt;}
}

2. @media in CSS3

The @media attribute has evolved into a kind of media queries in CSS3. In CSS3, query statements can be used to match various types of screens.

grammar:

@media mediatype and|not|only (media feature) {
CSS-sRules;
}
  1. mediatype media type: all, print (printer), screen (computer screen, mobile phone, etc.), speech (screen reader and other devices)
  2. media_query and | not | only media query condition operation
  3. media_feature Media features, such as maximum and minimum width.

Determine the media (object) type to achieve different displays. This feature allows CSS to act more precisely on different media types.

2.1 Examples

body{background:blue;}/*width between 500px-800px + height between 100px-400px blue*/
@media screen and (max-width:500px){body{background:green;}}/*Green when width is less than 500px*/
@media screen and (min-width:800px){body{background:red;}}/*Red when width is greater than 800px*/
@media screen and (max-height:100px){body{background:yellow;}}/*Yellow when height is less than 100px*/
@media screen and (min-height:400px){body{background:pink;}}/*Pink when height is greater than 400px*/

2.2 Media Characteristics

value describe
aspect-ratio Defines the ratio of the width to the height of the visible area of ​​the page in the output device
color Defines the number of color originals per set for the output device. If it is not a color device, the value is 0.
color-index Defines the number of entries in the output device's color lookup table. If no color lookup table is used, the value is equal to 0
device-aspect-ratio Defines the ratio of the output device's screen visible width to height.
device-height Defines the screen visible height of the output device.
device-width Defines the screen visible width of the output device.
grid Used to query whether the output device uses a raster or dot matrix.
height Defines the height of the visible area of ​​the page in the output device.
max-aspect-ratio Defines the maximum ratio of the output device's screen-viewable width to height.
max-color Defines the maximum number of color originals per set for the output device.
max-color-index Defines the maximum number of entries in the color lookup table of the output device.
max-device-aspect-ratio Defines the maximum ratio of the output device's screen-viewable width to height.
max-device-height Defines the maximum height visible on the output device's screen.
max-device-width Defines the maximum visible screen width of the output device.
max-height Defines the maximum visible height of the page in the output device.
max-monochrome Defines the maximum number of monochrome elements per pixel in a monochrome frame buffer.
max-resolution Defines the maximum resolution of the device.
max-width Defines the maximum visible width of the page in the output device.
min-aspect-ratio Defines the minimum ratio of the width to the height of the visible area of ​​a page in an output device.
min-color Defines the minimum number of color originals per set for the output device.
min-color-index Defines the minimum number of entries in the color lookup table of the output device.
min-device-aspect-ratio Defines the minimum ratio of the output device's screen-viewable width to height.
min-device-width Defines the minimum visible screen width of the output device.
min-device-height Defines the minimum visible height of the output device's screen.
min-height Defines the minimum visible height of the page in the output device.
min-monochrome Defines the minimum number of monochrome elements per pixel in a monochrome frame buffer.
min-resolution Defines the minimum resolution of the device.
min-width Defines the minimum visible width of the page in the output device.
monochrome Defines the number of monochrome elements per pixel in a monochrome frame buffer. If the device is not monochrome, the value is 0.
orientation Defines whether the visible area of ​​the page in the output device is greater than or equal to the width.
resolution Defines the resolution of the device. Such as: 96dpi, 300dpi, 118dpcm
scan Defines the scanning process for TV-type devices.
width Defines the width of the visible area of ​​the page in the output device.

Summarize

This concludes this article about using @media in CSS3 to achieve web page adaptation. For more relevant css3 media web page adaptation content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

<<:  The question of whether a tag opens a new page. The opening status of major websites is summarized

>>:  Vue implements nested routing method example

Recommend

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...

Vue implements a simple calculator

This article example shares the specific code of ...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

Using Apache ab to perform http performance testing

Mac comes with Apache environment Open Terminal a...

Solution to docker suddenly not being accessible from the external network

According to the methods of the masters, the caus...

Simple analysis of EffectList in React

Table of contents EffectList Collection EffectLis...

Solution to incomplete text display in el-tree

Table of contents Method 1: The simplest way to s...

Implementing a simple student information management system based on VUE

Table of contents 1. Main functions 2. Implementa...

js canvas implements verification code and obtains verification code function

This article example shares the specific code of ...

CentOS 7.6 installation of MySQL 5.7 GA version tutorial diagram

Table of contents Environment Preparation Environ...

MySQL encryption and decryption examples

MySQL encryption and decryption examples Data enc...