媒体查询语法
1 2 3
| @media media-type and (media-feature) { }
|
媒体类型
- all:所有设备
- screen:屏幕
- print:打印机
- speech:语音合成器
媒体特性
宽度
1 2 3 4 5 6 7 8 9 10 11
| @media (min-width: 768px) { }
@media (max-width: 767px) { }
@media (width: 768px) { }
|
高度
1 2 3
| @media (min-height: 600px) { }
|
方向
1 2 3 4 5 6 7
| @media (orientation: landscape) { }
@media (orientation: portrait) { }
|
像素密度
1 2 3
| @media (-webkit-min-device-pixel-ratio: 2) { }
|
逻辑运算符
and
1 2 3
| @media screen and (min-width: 768px) and (max-width: 1024px) { }
|
or (逗号)
1 2 3
| @media (min-width: 768px), (orientation: landscape) { }
|
not
1 2 3
| @media not screen and (color) { }
|
在 HTML 中使用
1
| <link rel="stylesheet" media="screen and (min-width: 768px)" href="desktop.css">
|
总结
媒体查询是响应式设计的核心。灵活使用各种特性,适配不同设备。