search
HomeWeb Front-endCSS TutorialOverview of new features of CSS3: How to use CSS3 to implement media queries

Overview of new features of CSS3: How to use CSS3 to implement media queries

Sep 08, 2023 am 08:14 AM
cssMedia inquiriescharacteristic

Overview of new features of CSS3: How to use CSS3 to implement media queries

Overview of the new features of CSS3: How to use CSS3 to implement media queries

With the popularity of mobile devices, responsive design of web pages has become more and more important. CSS3 provides a series of powerful features for front-end developers, one of the most important features is media queries (Media Queries). By using media queries, we can apply different styles and layouts to web pages on different devices.

This article will introduce the media queries in the new features of CSS3 and how to use it to achieve perfect responsive design. let's start!

1. The basic concept of media query

Media query is a new feature of CSS3, which allows us to apply different styles according to the characteristics of the device. Through media queries, we can set different CSS styles for different media types (such as screen, printer, etc.) and media characteristics (such as width, height, device orientation, etc.).

The syntax of media queries is very concise and clear. Its basic structure is as follows:

@media (media-feature-rule) {

/* 在这里写入适应该条件的CSS样式 */

}

Where (media-feature-rule) is what we want Media feature rules used, such as screen width, device orientation, etc.

2. Common features of media queries

CSS3 media queries provide some commonly used feature rules. The following are some common media query features:

  1. Screen width : Use max-width and min-width to define the maximum and minimum width of the screen.
  2. Device orientation: Use the orientation attribute to detect the orientation of the device, such as landscape (horizontal) and portrait (portrait).
  3. Screen resolution: Use min-resolution and max-resolution to set the minimum and maximum resolution of the screen.
  4. Touch devices: Use pointer and hover to detect whether the device supports touch.
  5. Printer: Use print to detect whether it is a printer.

3. Use media queries to implement responsive design

Below, we will combine actual code examples to show how to use media queries to implement responsive design.

/* 默认样式 */
body {
    background-color: #fff;
    color: #000;
    font-size: 16px;
}

/* 在大屏幕上应用的样式 */
@media (min-width: 768px) { 
    body {
        font-size: 24px;
    }
}

/* 在小屏幕上应用的样式 */
@media (max-width: 767px) { 
    body {
        font-size: 12px;
    }
}

In the above code, we have defined different font sizes for different screen sizes. When the screen width is greater than or equal to 768px, the large screen style will be applied and the font size is 24px; when the screen width is less than 767px, the small screen style will be applied and the font size is 12px. In this way, we can apply different styles to the web page according to different device sizes to adapt to different screens.

If you are developing web pages for mobile devices, you will most likely need to use media queries to set mobile styles. The following is a sample code that shows how to hide an element on the mobile terminal:

/* 默认样式 */
.element {
    display: block;
}

/* 在小屏幕上隐藏该元素 */
@media (max-width: 767px) { 
    .element {
        display: none;
    }
}

In the above example, when the width of the screen is less than 767px, the display attribute of the element .element will be set to none, thus Hide this element. In this way, we can dynamically adjust the web page layout and style according to different screen sizes.

Summary:

This article introduces the media queries in the new features of CSS3 and how to use it to implement responsive design. Media queries are an important feature of CSS3, which allow us to apply different styles and layouts to web pages based on the characteristics of the device. Whether on the big screen or mobile devices, media queries help us achieve a better user experience. I hope this article will help you understand the basic concepts and usage of media queries!

The above is the detailed content of Overview of new features of CSS3: How to use CSS3 to implement media queries. 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
A Little Reminder That Pseudo Elements are Children, Kinda.A Little Reminder That Pseudo Elements are Children, Kinda.Apr 19, 2025 am 11:39 AM

Here's a container with some child elements:

Menus with 'Dynamic Hit Areas'Menus with 'Dynamic Hit Areas'Apr 19, 2025 am 11:37 AM

Flyout menus! The second you need to implement a menu that uses a hover event to display more menu items, you're in tricky territory. For one, they should

Improving Video Accessibility with WebVTTImproving Video Accessibility with WebVTTApr 19, 2025 am 11:27 AM

"The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect."- Tim Berners-Lee

Weekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteWeekly Platform News: CSS ::marker pseudo-element, pre-rendering web components, adding Webmention to your siteApr 19, 2025 am 11:25 AM

In this week's roundup: datepickers are giving keyboard users headaches, a new web component compiler that helps fight FOUC, we finally get our hands on styling list item markers, and four steps to getting webmentions on your site.

Making width and flexible items play nice togetherMaking width and flexible items play nice togetherApr 19, 2025 am 11:23 AM

The short answer: flex-shrink and flex-basis are probably what you’re lookin’ for.

Position Sticky and Table HeadersPosition Sticky and Table HeadersApr 19, 2025 am 11:21 AM

You can't position: sticky; a

Weekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryWeekly Platform News: HTML Inspection in Search Console, Global Scope of Scripts, Babel env Adds defaults QueryApr 19, 2025 am 11:18 AM

In this week's look around the world of web platform news, Google Search Console makes it easier to view crawled markup, we learn that custom properties

IndieWeb and WebmentionsIndieWeb and WebmentionsApr 19, 2025 am 11:16 AM

The IndieWeb is a thing! They've got a conference coming up and everything. The New Yorker is even writing about it:

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

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)