CSS—媒体查询器

1,020 阅读1分钟

@media

媒体查询器。针对不同的屏幕尺寸设置不同的样式

☆当屏幕尺寸大于 320px 时,不包含指定值

@media only screen and (min-width:320px){
	div{ color:red }
}

当屏幕尺寸小于 640px 时,不包含指定值

@media only screen and (max-width:640px){
	div{ color:red }
}

当屏幕尺寸大于 320px 且小于 640px 时,不包含指定值

@media only screen and (min-width:320px) and (max-width:640px){
	div{ color:red }
}

输出设备中的页面可见区域高度、输出设备中的页面可见区域宽度

@media only screen and (width:320px) and (height:640px) {
	div{ color:red }
}

设备像素比率为 1.0 时(用来适配特定屏幕长宽比的设备)(1.0、1.3、1.5、2.0、3.0)

@media only screen and (device-aspect-ratio:1.0){
	div{ color:red }
}

设备宽度在区间指定值时应用,不包含指定值

@media only screen and (min-device-width:320px) and (max-device-width:480px){
	div{ color:red }
}

指在输出设备上的像素密度,设备分辨率为 144dpi 时

@media print and (min-resolution: 144dpi){
	div{ color:red }
}

设备上物理像素和设备独立像素,设备像素比率,仅用于 Chorme 浏览器

@media screen and (-webkit-min-device-pixel-ratio:1.0){
	div{ color:red }
}

设备的屏幕可见宽度、设备像素比率

@media (device-width:720px) and (-webkit-min-device-pixel-ratio:2){
    div{ color:red }
}

设备的屏幕可见高度、设备像素比率

@media (device-height:568px) and (-webkit-min-device-pixel-ratio:2){
	div{ color:red }
}

横屏

@media all and (orientation:landscape) {
	div{ color:red }    
}

竖屏

@media all and (orientation:portrait){
	div{ color:red }
}

三种计算方式

①only

用于电脑屏幕,平板电脑,智能手机等

@media only screen and{ }

②not

非屏幕

@media not screen and(){ }

③and

所有设备

@media all and(){ }