search
HomeWeb Front-endCSS TutorialDetailed explanation of CSS media query properties: @media and min-width/max-width

CSS 媒体查询属性详解:@media 和 min-width/max-width

Detailed explanation of CSS media query properties: @media and min-width/max-width

In modern web development, devices have various screen sizes and resolutions Diverse. In order to achieve a better user experience, we often need to adjust the style and layout of web pages according to different devices. CSS media query properties are a powerful tool that can help us dynamically apply different styles based on the characteristics of the device. This article will introduce the @media rules and the min-width and max-width attributes in detail, and give specific code examples.

@media rules are keywords used for media queries in CSS. By using @media rules, we can apply different styles based on different media types and conditions. The media type can be screen, print, or speech (speech synthesis), etc. The conditions can be the width, height, resolution, etc. of the device.

In media queries, commonly used condition attributes are min-width and max-width. min-width represents the minimum width of the device, while max-width represents the maximum width of the device. With these two properties, we can implement a simple responsive layout. Here is an example:

@media screen and (min-width: 768px) and (max-width: 1024px) {
  body {
    background-color: lightblue;
  }
  h1 {
    font-size: 24px;
  }
  p {
    font-size: 18px;
  }
}

The above code means that when the width of the device is between 768px and 1024px, the defined style is applied. In this example, the body's background color will change to light blue, and the font sizes of h1 and p will be adjusted accordingly.

In addition to the min-width and max-width attributes, other conditional attributes can also be used to implement more complex media queries. For example, we can use min-device-width and max-device-width to query based on the actual width of the device.

@media screen and (min-device-width: 320px) and (max-device-width: 480px) {
  /* 样式定义 */
}

Another commonly used conditional attribute is orientation, which is used to determine whether the orientation of the device is landscape or portrait.

@media screen and (orientation: landscape) {
  /* 横向样式定义 */
}

@media screen and (orientation: portrait) {
  /* 纵向样式定义 */
}

Media query attributes can be nested to achieve more fine-grained style adjustments. For example, we can nest another media query within a media query and combine multiple conditional attributes to meet specific layout needs.

@media screen and (min-width: 768px) and (max-width: 1024px) {
  body {
    background-color: lightblue;
  }
  
  @media (orientation: landscape) {
    h1 {
      font-size: 24px;
      margin-top: 20px;
    }
    p {
      font-size: 18px;
    }
  }
  
  @media (orientation: portrait) {
    h1 {
      font-size: 18px;
      margin-top: 10px;
    }
    p {
      font-size: 14px;
    }
  }
}

In the example, when the device width is between 768px and 1024px, different styles are applied according to the device orientation.

To summarize, CSS media query properties are a flexible and powerful tool that can dynamically adjust web page style and layout based on device characteristics. By using @media rules and conditional attributes such as min-width/max-width, we can easily implement responsive layout and improve user experience. In actual development, we can select appropriate media query attributes according to specific needs and use them in conjunction with nesting to achieve fine style adjustments. I hope the above content is helpful to your understanding of media query properties.

The above is the detailed content of Detailed explanation of CSS media query properties: @media and min-width/max-width. 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
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools