CSS媒体查询 width VS device-width

本文详细解释了CSS中device-width与width的区别,device-width是指设备本身的宽度即屏幕分辨率,而width更适用于创建响应式网页。文章列举了多种流行设备的屏幕分辨率及CSS媒体查询示例,帮助开发者更好地为不同设备定制样式。

In CSS media the difference between width and device-width can be a bit muddled, so lets expound on that a bit. device-width refers to the width of the device itself, in other words, the screen resolution of the device. Lets say your screen's resolution is 1440 x 900. This means the screen is 1440 pixels across, so it has a device-width of 1440px. Most mobile phones have a device-width of 480px or lower, including the popular iPhone 4 (with device-width: 320px), despite it technically having a 640 x 960 resolution. This is due to iPhone 4's retina display, which crams two device pixels into each CSS pixel on the screen. This is true for the Ipad 3 as well; its reported device-width is 768px just like its predecessors, even though its actual screen resolution is 1536px x 2048px. In general width is more versatile when it comes to creating responsive webpages, though device-width is useful when you wish to specifically target mobile devices (and not desktops with a small browser window for example), as rarely do desktops have screen resolutions below a certain number such as 320px x 480px.

The below shows the screen resolution and CSS media device dimensions of some of the popular devices out there:

CSS Media Dimensions
Deviceresolution (px)device-width/ device-height (px)
iPhone320 x 480320 x 480, in both portrait and landscape mode
iPhone 4640 x 960320 x 480, in both portrait and landscape mode. device-pixel-ratio is 2
iPhone 5, 5s640 x 1136320 x 568, in both portrait and landscape mode. device-pixel-ratio is 2
iPhone 6750 x 1334375 x 667, in both portrait and landscape mode. device-pixel-ratio is 2
iPhone 6 plus1242 x 2208414 x 736, in both portrait and landscape mode. device-pixel-ratio is 3
iPad 1 and 2768 x 1024768 x 1024, in both portrait and landscape mode
iPad 31536 x 2048768 x 1024, in both portrait and landscape mode

CSS pixel density is 2

Samsung Galaxy S I and II480 x 800320 x 533, in portrait mode

CSS pixel density is 1.5

Samsung Galaxy S III720 x 1280360? x 640?, in portrait mode
HTC Evo 3D540 x 960360 x 640, portrait mode

CSS pixel density is 1.5

Amazon Kindle Fire1024 x 6001024 x 600, landscape mode

Just to complicate things even more, in iPhone and iPad devices, the device-width always corresponds to the width of the device in portrait mode, regardless of whether the device is in that mode or landscape instead. With other devices, its device-width changes depending on its orientation.

* For a more complete list of devices and their screen resolutions, visit this page.

Lets see some more CSS media queries now that capture different devices and screen dimensions:

/* #### Mobile Phones Portrait #### */
@media screen and (max-device-width: 480px) and (orientation: portrait){
  /* some CSS here */
}

/* #### Mobile Phones Landscape #### */
@media screen and (max-device-width: 640px) and (orientation: landscape){
  /* some CSS here */
}

/* #### Mobile Phones Portrait or Landscape #### */
@media screen and (max-device-width: 640px){
  /* some CSS here */
}

/* #### iPhone 4+ Portrait or Landscape #### */
@media screen and (min-device-width: 320px) and (-webkit-min-device-pixel-ratio: 2){
  /* some CSS here */
}

/* #### iPhone 5 Portrait or Landscape #### */
@media (device-height: 568px) and (device-width: 320px) and (-webkit-min-device-pixel-ratio: 2){
  /* some CSS here */
}

/* #### iPhone 6 and 6 plus Portrait or Landscape #### */
@media (min-device-height: 667px) and (min-device-width: 375px) and (-webkit-min-device-pixel-ratio: 3){
  /* some CSS here */
}

/* #### Tablets Portrait or Landscape #### */
@media screen and (min-device-width: 768px) and (max-device-width: 1024px){
  /* some CSS here */
}

/* #### Desktops #### */
@media screen and (min-width: 1024px){
  /* some CSS here */
}

转载:http://www.javascriptkit.com/dhtmltutors/cssmediaqueries2.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值