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

CSS Naming: BEM, scoped CSS, CSS modules and CSS-in-JS explained

The scope of css is global. As the project gets b...

jQuery implements breathing carousel

This article shares the specific code of jQuery t...

JS implements random generation of verification code

This article example shares the specific code of ...

Let's deeply understand the event object in js

We know that the commonly used events in JS are: ...

How to view version information in Linux

How to view version information under Linux, incl...

How to submit the value of a disabled form field in a form Example code

If a form field in a form is set to disabled, the ...

Tips for implementing list loop scrolling based on jQuery (super simple)

I saw a good idea and recorded it. I have used jQ...

JavaScript deshaking and throttling examples

Table of contents Stabilization Throttling: Anti-...

Sample code for separating the front-end and back-end using FastApi+Vue+LayUI

Table of contents Preface Project Design rear end...

Summary of situations where MySQL indexes will not be used

Types of Indexes in MySQL Generally, they can be ...

Detailed explanation of the basic usage of the Linux debugger GDB

Table of contents 1. Overview 2. gdb debugging 2....

The order of event execution in the node event loop

Table of contents Event Loop Browser environment ...

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first f...