简体   繁体   English

为什么设备宽度在桌面浏览器上有效?

[英]Why does device-width work on desktop browser?

I thought device-width only worked on tablets and other mobile devices until today. 我认为直到今天, device-width仅适用于平板电脑和其他移动设备。 (Or maybe I was just wrong for a long time...) (或者也许我很久没做错...)

 .test { width: 100px; height: 100px; padding: 5%; } @media only screen and (max-width : 1900px) { .test { background: blue; color: yellow; } } @media screen and (min-device-width : 768px) and (max-device-width : 1024px) { .test { background: #000000; color: #ffffff; } } 
 <div class="test"> TEST</div> 

If I set my monitor resolution as the width 1024x768, the media query below will work. 如果将显示器分辨率设置为1024x768宽,则下面的媒体查询将起作用。

@media screen and (min-device-width : 768px) and (max-device-width : 1024px) {
/*...*/
}

How can I target tablet and mobile with media queries since device-width can work in desktop browsers? 由于device-width可以在台式机浏览器中工作,因此如何通过媒体查询定位平板电脑和移动device-width

You could try @media handheld instead of @media screen . 您可以尝试使用@media handheld而不是@media screen

But you might want to reconsider. 但是您可能需要重新考虑。 After all, desktop or not, if the screen size is smaller you might want to show the mobile layout anyway, since your desktop version will probably be optimized for higher resolutions. 毕竟,无论桌面与否,如果屏幕尺寸较小,您可能仍要显示移动布局,因为您的桌面版本可能已针对更高的分辨率进行了优化。 Desktop-users who are annoyed by this should just buy a bigger screen. 对此感到恼火的桌面用户应该只购买更大的屏幕。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM