search
HomeWeb Front-endCSS TutorialDetailed introduction of CSS3 media queries for responsive layout

Use our CSS3 to achieve Responsive layout

Just having a flexible box is not enough
CSS3 also extends the mediaattribute, adding A module function
Media QueriesMedia QueriesFunction

Introduction of media queries

With the rapid popularity of mobile terminals
More and more users use smart phones , tablets, etc.
All taking into account the needs of users
We need to ensure that users have a good experience browsing the page on various devices
So we need media queries
It allows us to provide Different devices or devices with different conditions set different styles
However, if the mobile terminal has very important needs
It is better to develop a special page for the mobile terminal
Let’s take a look at introducing media queries Method

Media query selectively loads files

First let’s take a look at the link method
This is the approach in CSS2
Use the link tag and media attribute to introduce different style sheets ( If the conditions are met)

<link rel="stylesheet" media="screen and (max-width: 600px)" href="demo.css" />

Two concepts are introduced here
One is Media Type(Media Type), and the screen
here isMedia Features (Media Query), the max-width here: 600px

Media Query is an enhanced version of CSS3’s Media Type
In fact, Media Query can be regarded as Media Type (judgment condition) + CSS (qualified Style rules)

The translation of this code is
If the media medium is a screen type and the window width is ≤600px, introduce the demo.css style file

Media query to selectively add styles

What we do in CSS3 is to use @media rules to add different styles in CSS files
Compared with link, it can reduce page requests and have better results

@media screen and (max-width: 600px) {    .demo1 {        ......
    }
    .demo2 {
        ......
    }
}

Introduction of other media queries

There are many, many media queries, we don’t need to know so much
The only two commonly used ones are the above two
@import can also use media queries

@import url(demo.css) screen;

Use media queries

If we want to specify multiple media properties
Just use the and keyword directly
The specification of the media properties part requires the use of brackets

media="screen and (min-width: 601px) and (max-width: 800px)"

The style is applied to the screen between 601px~800px


Media queries do not have or specify alternative media capabilities, but we can use a comma separated list

media="screen and (min-width: 601px), print and (min-width: 6in)"

Styles are applied on screens 601px+ or printing devices that use paper at least 6 inches wide


Media queries can specify negative conditions
We need to use the keyword not at the front of the media statement
Note that it cannot be declared separately before a single condition, not will negate the entire media statement

media="not screen and (min-width: 600px) and (max-width: 800px)"

Style is applied to screens that are not between 600px~800px


In addition to the not keyword, there is also an only
It is used to hide media queries from early browsers
Similar to not, it must be declared at the beginning of the media declaration statement
For example, this statement

media="screen and (min-width: 601px) and (max-width: 800px)"

Early browsers can only understand it asmedia="screen"
Since it does not understand media properties, it will apply style rules to all screen devices
but uses the only keyword

media="only screen and (min-width: 601px) and (max-width: 800px)"

Early browsers will understand this as media="only "
But the only media type does not exist, so it will not apply any styles
Implements the function of hiding media queries from early browsers

Media types and media properties

Media Type

Although there are many keywords for media types, most of them have been abandoned (who uses them)
The commonly used ones are all, screen, and then print

##allAll Media equipmentscreenColor screen: computer, tablet, smartphone...printPrinter, Print PreviewspeechOccurring device: screen reader..aural(Deprecated) Speech and braille(Deprecated) Braille Tactile Feedback Device for the Blindembossed (obsolete) handheld (Obsolete) Portable device: small phone..projection (Obsolete) Projection device: slideshow..tty (Deprecated) Media using a fixed-density letter grid: teletypewriters and terminals..tv (obsolete) TV type equipment: TV, Internet TV..

Media properties

There are many media properties, and many of them are not used

Media Type Description
AudioSynthesizer
Page Braille printer for the blind
##min- device-widthThe minimum visible width of the screen of the output devicemin-device-heightThe minimum visible height of the screen of the output deviceThe minimum visible area height of the page in the output devicemin-monochromeThe minimum number of monochrome originals contained per pixel in a monochrome frame buffermin-resolutionThe minimum resolution of the devicemin-widthThe minimum visible area width of the page in the output devicemonochromeIn a single color The number of monochrome originals per pixel in the frame buffer. If it is not a monochrome device, the value is equal to 0orientationWhether the height of the visible area of ​​the page in the output device is greater than or equal to the widthresolutionThe resolution of the device. Such as: 96dpi, scanScanning process of TV equipmentwidthThe width of the visible area of ​​the page in the output device
Media type Description
aspect-ratio The ratio of the width to height of the visible area of ​​the page in the output device
color The number of color originals in each group of output devices. If it is not a color device, the value is equal to 0
color-index 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 The ratio of the visible width of the screen to the height of the output device
device-height The screen visible height of the output device
device-width Output device The visible width of the screen
grid Query whether the output device uses raster or lattice
height The height of the visible area of ​​the page in the output device
max-aspect-ratio The maximum ratio of the visible width and height of the screen of the output device
max-color The maximum number of color originals in each group of the output device
max-color-index Output The maximum number of entries in the device's color lookup table
max-device-aspect-ratio The maximum ratio of the screen visible width to height of the output device
max-device-height The maximum height visible on the screen of the output device
max-device-width The maximum visible width of the screen of the output device
max-height The maximum visible area height of the page in the output device
max-monochrome The maximum number of monochrome originals contained per pixel in a monochrome frame buffer
max-resolution Maximum device resolution
max-width Maximum visible area width of the page in the output device
min-aspect-ratio Minimum ratio of width to height of the visible area of ​​the page in the output device
min-color The minimum number of color originals in each group of the output device
min-color-index The minimum entry in the color lookup table of the output device Number
min-device-aspect-ratio The minimum ratio of the visible width to height of the output device screen
min-height
300dpi, 118dpcm

Note here to distinguish device-width/height and width/height
device -width/height is the width of the device (not the width of the browser)
width/height is the size of our browser window
Use
documentElement.clientWidth/clientHeight is viewport The value of

The above is the detailed content of Detailed introduction of CSS3 media queries for responsive layout. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Modern Scroll Shadows Using Scroll-Driven AnimationsModern Scroll Shadows Using Scroll-Driven AnimationsMay 07, 2025 am 10:34 AM

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Revisiting Image MapsRevisiting Image MapsMay 07, 2025 am 09:40 AM

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

State of Devs: A Survey for Every DeveloperState of Devs: A Survey for Every DeveloperMay 07, 2025 am 09:30 AM

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more. 

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!