Home Web Front-end CSS Tutorial CSS uses @media media queries for responsive design. What are media queries?

CSS uses @media media queries for responsive design. What are media queries?

Sep 08, 2018 am 11:58 AM

With the development of the Internet, various mobile devices such as smartphones and tablets can be seen everywhere. So how can our website be properly laid out on various mobile devices? Writing a set of code for each device is cumbersome and requires a lot of work. Then smart programmers will use a set of codes to make the website display reasonably on devices of different sizes. Therefore, the responsive design model was born, the core of which is "media query".

1. What is media query

Media query allows us to determine the characteristics of the device display (such as viewport width, screen ratio, device orientation: landscape or portrait) To set CSS styles for it, a media query consists of a media type and one or more conditional expressions that detect media characteristics. Media properties that can be detected in media queries are width , height , and color (etc.). Using media queries, you can customize the display effect for specific output devices without changing the page content.

Using the @media query, you can define different styles for different media types. @media can set different styles for different screen sizes, especially if you need to set up a responsive page, @media is very useful. When you reset the browser size, the page will also be re-rendered based on the browser's width and height.

2. Use of @media media query

a. Add the following code to the html document to be compatible with the display effect of mobile devices

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

width=device-width: The width is equal to the width of the current device
initial-scale=1: The initial scaling ratio (default is 1)
maximum-scale=1: Allow the user to zoom to the maximum Scale (default is 1)
user-scalable=no: Users cannot manually scale

b. Write responsive media queries in CSS files

Basic syntax: @media media type and (Media Features) {Style}

Example 1:

@media screen and (max-width:480px){
body {
background:red
  }
}
Copy after login

means: When the screen is less than or equal to 480px, the background color in the page turns red.

Example 2:

@media screen and (min-width:900px){
body{
font-size:20px
    }
}
Copy after login

means: when the screen is greater than or equal to 900px, the font size on the page becomes 20px.

Example 3:

@media screen and (min-width:600px) and (max-width:900px){
  body {background-color:#f5f5f5;}
}
Copy after login

means: when the screen is between 600px~900px, the background color of the body is "#f5f5f5".

Summary: The above introduces what media query @media is, and gives examples of how to use media queries.

The above is the detailed content of CSS uses @media media queries for responsive design. What are media queries?. 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

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
Tutorial on using CSS to implement responsive image automatic carousel effect Tutorial on using CSS to implement responsive image automatic carousel effect Nov 21, 2023 am 08:37 AM

With the popularity of mobile devices, web design needs to take into account factors such as device resolution and screen size of different terminals to achieve a good user experience. When implementing responsive design of a website, it is often necessary to use the image carousel effect to display the content of multiple images in a limited visual window, and at the same time, it can also enhance the visual effect of the website. This article will introduce how to use CSS to achieve a responsive image automatic carousel effect, and provide code examples and analysis. Implementation ideas The implementation of responsive image carousel can be implemented through CSS flex layout. exist

How to create a responsive tag cloud using HTML, CSS and jQuery How to create a responsive tag cloud using HTML, CSS and jQuery Oct 27, 2023 am 10:46 AM

How to use HTML, CSS and jQuery to create a responsive tag cloud. A tag cloud is a common web element used to display various keywords or tags. It usually displays the importance of keywords in different font sizes or colors. In this article, we will introduce how to use HTML, CSS and jQuery to create a responsive tag cloud, and give specific code examples. Creating the HTML Structure First, we need to create the basic structure of the tag cloud in HTML. You can use an unordered list to represent tags

Tutorial on implementing responsive sliding menu using CSS Tutorial on implementing responsive sliding menu using CSS Nov 21, 2023 am 08:08 AM

A tutorial on using CSS to implement a responsive sliding menu requires specific code examples. In modern web design, responsive design has become an essential skill. To accommodate different devices and screen sizes, we need to add a responsive menu to the website. Today, we will use CSS to implement a responsive sliding menu and provide you with specific code examples. First, let's take a look at the implementation. We will create a navigation bar that automatically collapses when the screen width is smaller than a certain threshold and expands by clicking the menu button.

How to use React to develop a responsive backend management system How to use React to develop a responsive backend management system Sep 28, 2023 pm 04:55 PM

How to use React to develop a responsive backend management system. With the rapid development of the Internet, more and more companies and organizations need an efficient, flexible, and easy-to-manage backend management system to handle daily operations. As one of the most popular JavaScript libraries currently, React provides a concise, efficient and maintainable way to build user interfaces. This article will introduce how to use React to develop a responsive backend management system and give specific code examples. Create a React project first

How to make a responsive music playlist using HTML, CSS and jQuery How to make a responsive music playlist using HTML, CSS and jQuery Oct 25, 2023 am 09:25 AM

How to make a responsive music playlist using HTML, CSS and jQuery In modern society, music has become an indispensable part of people's lives. In order to facilitate users to enjoy their favorite music anytime and anywhere, it is very necessary to create a responsive music playlist. In this article, we will introduce how to use HTML, CSS and jQuery to make a music playlist with responsive design, and provide detailed code examples. Step 1: HTML structure design First, we need to design

How to create a responsive carousel layout using HTML and CSS How to create a responsive carousel layout using HTML and CSS Oct 20, 2023 pm 04:24 PM

How to create a responsive carousel layout using HTML and CSS Carousels are a common element in modern web design. It can attract the user's attention, display multiple contents or images, and switch automatically. In this article, we will introduce how to create a responsive carousel layout using HTML and CSS. First, we need to create a basic HTML structure and add the required CSS styles. The following is a simple HTML structure: &lt;!DOCTYPEhtml&g

How to create a responsive scrolling notification bar using HTML, CSS and jQuery How to create a responsive scrolling notification bar using HTML, CSS and jQuery Oct 26, 2023 pm 12:12 PM

How to use HTML, CSS and jQuery to create a responsive scrolling notification bar. With the popularity of mobile devices and the increase in user requirements for website access experience, designing a responsive scrolling notification bar has become more and more important. Responsive design ensures that the website displays properly on different devices and that users can easily view notification content. This article will introduce how to use HTML, CSS and jQuery to create a responsive scrolling notification bar, and provide specific code examples. First we need to create the HTM

Tips for Responsive Website Development with Webman Tips for Responsive Website Development with Webman Aug 14, 2023 pm 12:27 PM

Tips for Responsive Website Development with Webman In today’s digital age, people are increasingly relying on mobile devices to access the Internet. In order to provide a better user experience and adapt to different screen sizes, responsive website development has become an important trend. As a powerful framework, Webman provides us with many tools and technologies to realize the development of responsive websites. In this article, we will share some tips for using Webman for responsive website development, including how to set up media queries,

See all articles