Home Web Front-end CSS Tutorial Media queries for CSS responsive layout

Media queries for CSS responsive layout

May 14, 2020 am 10:23 AM
css

Media queries for CSS responsive layout

In actual projects, you will always encounter the problem of responsive layout. If you have not used some responsive UI framework, then you will generally use media queries to implement responsive layout. Since I encounter it so often, I think it is necessary to summarize it a little bit.

The first thing we need to understand is that when using media queries, when the browser size is reset, the page will be re-rendered according to the width and height of the browser.

1. About meta

Before using media queries, you need to use the setting to be compatible with the display effect of mobile devices, so first understand briefly Let’s talk about the tag in html.

This tag is usually placed inside the head tag and is used to provide meta-information (meta-information) about an HTML element, such as description, keywords for search engines, and refresh frequency.

The settings for media queries are as follows:

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
Copy after login

Parameter description:

1. The name attribute provides the name in the name/value pair. name="viewport" means the setting of the window, in addition to the viewport.

2. The content attribute provides the value in the name/value pair. The value can be any valid string, and content is based on the content. It should always be used together with the http-equiv or name attribute to provide them with Definition of the values ​​associated with it. Content value description:

width = device-width: width is equal to the width of the current device;

initial-scale: initial scaling (default setting is 1.0);

minimum-scale: The minimum ratio that users are allowed to zoom to (default setting is 1.0);

maximum-scale: The maximum ratio that users are allowed to zoom to (default setting is 1.0);

user -scalable: Whether the user can manually zoom (default is no, because we don't want users to zoom in and out of the page).

For more in-depth understanding of , please refer to: http://www.w3school.com.cn/tags/tag_meta.asp

2. Media query implementation method

1. Directly determine the size of the device in the link, and then reference different external css files. For example:

<link type="text/css" href="media_1.css" media="screen and (min-width: px)">
Copy after login

When the screen is larger than 400px, the external css style file media_1.css is referenced.

2. When setting the css style, set @media directly after it. For example:

.div1{
    width: 400px;
    height: 400px;
}
/*当屏幕大于1440px的彩色屏幕样式*/
@media screen and (min-width: 1440px){
    .div1{
        width: 800px;
        height:800px;
    }
}
Copy after login

This is a media query implemented using CSS3. In fact, media query is essentially a style overlay.

3. CSS3 media query

1. Basic syntax

@media  媒体类型  关键字 and (媒体功能) {
        /* CSS 样式 */
    }
Copy after login

2. Keywords

only: Specify a specific media type, you can use to exclude browsers that don't support media queries.

not: Exclude a specified media type.

The use of keywords is often used for devices that do not support media features but support media types

3. Media type

Media type describes the general description of the device Categories, media types are optional and the all type will be implicitly used unless the not or only keywords are used.

ValueDescription
allFor all multimedia type devices
printFor printers
#screenFor computer screens, tablets, smartphones, etc.
speechFor screen readers

All browsers support the value "screen", "print" and "all" media attributes.

4. Commonly used media functions

The following only lists some media functions that may be slightly commonly used:

height defines the height of the visible area of ​​the page in the output device.

width defines the width of the visible area of ​​the page in the output device.

max-height defines the maximum visible area height of the page in the output device.

max-width defines the maximum visible area width of the page in the output device.

min-height defines the minimum visible area height of the page in the output device.

min-width defines the minimum visible area width of the page in the output device.

device-height defines the visible height of the screen of the output device.

device-width defines the visible width of the screen of the output device.

max-device-height Defines the maximum height visible on the screen of the output device.

max-device-width defines the maximum visible width of the screen of the output device.

orientation Detects whether the device is currently in landscape or portrait orientation.

4. Setting the actual common sizes

The following only lists some common screen sizes, but settings of greater than, less than, and equal to are also implemented.

    /* 屏幕尺寸大于960px时(普通彩色屏幕) */
    @media only screen and (min-width:960px){ }
    /* 屏幕尺寸小于1440px时 */
    @media only screen and (max-width:1440px){ }
    /* 屏幕尺寸大于960px而小于1920px */
    @media only screen  (min-width: 960px) and (max-width: 1920px){ }
    /* 屏幕大于2000px时(MAC) */
    @media only screen and (min-width:2000px){ }
    /*  当设备可视宽度小于480px (iphone)*/
    @media only screen and (max-device-width:480px){ }
    /* 当设备可视宽度等于768px时 (iPad) */
    @media only screen and (device-width:768px){ }
    /* 屏幕尺寸大于481px而小于1024px (iPad 竖屏) */
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) { }
    /* 屏幕尺寸大于481px而小于1024px (iPad横屏) */
    @media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) { }
Copy after login

Note: Due to the style coverage problem of CSS, during actual setting, if the same element needs to be set to several different sizes, you need to pay attention to the order issue, such as: min- When it comes to width, put the small ones on top and the big ones on the bottom. Similarly, if you use max-width, then the big ones are on top and the small ones are on the bottom.

Recommended tutorial: "CSS Tutorial"

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

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles