媒体查询@media

媒体查询使得我们可以根据设备特性以及一些特性范围来决定为页面赋予什么样的样式。

基本语法

@media (max-width: 1200px) {
    
     ... }

@media all (max-width: 1200px) {
    
     ... }

@media not screen (max-width: 1200px) {
    
     ... }

@media only screen (max-width: 1200px) and (min-width: 786px) {
    
     ... }

@media only screen (max-width: 1200px), (min-width: 786px) {
    
     ... }

@media only screen (max-width: 1200px), handheld and (min-width: 786px) {
    
     ... }

媒体设备

  • all 所有设备
  • handheld 手持设备
  • print 打印机
  • screen 屏幕
  • tv 电视设备

媒体特征

大多数媒体属性可以带有min-max-前缀,用于表达最低...或者最高...,常见特性如下:

  • aspect-ratio 宽高比

  • device-aspect-ratio 设备宽高比

  • device-height 设备高度

  • device-width 设备宽度

  • height 高度

  • width 宽度

  • resolution 分辨率(像素密度)

  • orientation 方向

    • landscape (横屏) | portrait (竖屏)

案例如下:

@media all and (orientation: portrait) {
    
     ... }

@media screen and (device-aspect-ratio: 16/9), screen and (device-aspect-ratio: 16/10) {
    
     ... }

@media screen and (min-aspect-ratio: 1/1) {
    
     ... }

@media print and (min-resolution: 300dpi) {
    
     ... }

媒体查询形式

<!-- link元素中的CSS媒体查询 -->
<link rel="stylesheet" media="(max-width: 800px)" href="example.css" />

<!-- 样式表中的CSS媒体查询 -->
<style>
@media (max-width: 600px) {
    
    
  .facet_sidebar {
    
    
    display: none;
  }
}
</style>

猜你喜欢

转载自blog.csdn.net/Sandersonia/article/details/132664345