苹果新设备对应的CSS媒体查询是什么?我需要设置body
的background-color
来更改X的安全区域背景颜色。
苹果新设备对应的CSS媒体查询是什么?我需要设置body
的background-color
来更改X的安全区域背景颜色。
@media only screen
and (device-width : 375px)
and (device-height : 812px)
and (-webkit-device-pixel-ratio : 3) { }
@media only screen
and (device-width : 375px)
and (device-height : 667px)
and (-webkit-device-pixel-ratio : 2) { }
@media only screen
and (device-width : 414px)
and (device-height : 736px)
and (-webkit-device-pixel-ratio : 3) { }
iPhone 6+/6s+/7+/8+都是一样的尺寸,而iPhone 7/8也是如此。
寻找特定方向?
竖屏
添加以下规则:
and (orientation : portrait)
景观
添加以下规则:
and (orientation : landscape)
参考资料:
以下是适用于iPhone的一些媒体查询。此处是参考链接:https://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
/* iphone 3 */
@media only screen and (min-device-width: 320px) and (max-device-height: 480px) and (-webkit-device-pixel-ratio: 1) { }
/* iphone 4 */
@media only screen and (min-device-width: 320px) and (max-device-height: 480px) and (-webkit-device-pixel-ratio: 2) { }
/* iphone 5 */
@media only screen and (min-device-width: 320px) and (max-device-height: 568px) and (-webkit-device-pixel-ratio: 2) { }
/* iphone 6, 6s, 7, 8 */
@media only screen and (min-device-width: 375px) and (max-device-height: 667px) and (-webkit-device-pixel-ratio: 2) { }
/* iphone 6+, 6s+, 7+, 8+ */
@media only screen and (min-device-width: 414px) and (max-device-height: 736px) and (-webkit-device-pixel-ratio: 3) { }
/* iphone X , XS, 11 Pro, 12 Mini */
@media only screen and (min-device-width: 375px) and (max-device-height: 812px) and (-webkit-device-pixel-ratio: 3) { }
/* iphone 12, 12 Pro */
@media only screen and (min-device-width: 390px) and (max-device-height: 844px) and (-webkit-device-pixel-ratio: 3) { }
/* iphone XR, 11 */
@media only screen and (min-device-width: 414px) and (max-device-height: 896px) and (-webkit-device-pixel-ratio: 2) { }
/* iphone XS Max, 11 Pro Max */
@media only screen and (min-device-width: 414px) and (max-device-height: 896px) and (-webkit-device-pixel-ratio: 3) { }
/* iphone 12 Pro Max */
@media only screen and (min-device-width: 428px) and (max-device-height: 926px) and (-webkit-device-pixel-ratio: 3) { }
我注意到这里的答案都在使用:device-width
、device-height
、min-device-width
、min-device-height
以及max-device-width
、max-device-height
。
请不要再使用它们,因为它们已被废弃。请参考MDN。相反,请使用常规的min-width
、max-width
等属性。为了增加保障,您可以将最小值和最大值设置为相同的像素数。例如:
iPhone X
@media only screen
and (width : 375px)
and (height : 812px)
and (orientation : portrait)
and (-webkit-device-pixel-ratio : 3) { }
以下是一些关于这个主题的有用链接:
看起来,使用env()方法为iPhone X/8添加填充的最准确(和无缝)方法似乎是...
padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
以下是相关内容的链接:
以获得更好的格式。 - Alan Scarpameta[@name="viewport"]
元素,则可以使用以下内容来检测移动设备:@media only screen and (width: 980px), (hover: none) { … }
device-width
测试添加进来。@media only screen and (max-device-width: 800px) and (width: 980px), (hover: none) { … }
hover
属性似乎是检测移动设备是否无法真正执行hover
的最终新方法;它在Firefox 64(2018年)中才被引入,虽然自2016年以来就已经得到Android Chrome 50(2016年)的支持,甚至自2014年以来就已经得到Chrome 38(2014年)的支持。
Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0 Mobile/15E148 Safari/604.1
。 - Adrian