在设计多种屏幕尺寸的网页布局时,媒体查询成为必不可少的工具。这些查询允许开发人员指定如何根据屏幕宽度调整布局。
要定位特定屏幕尺寸,请使用 media 属性最大宽度属性。例如,要将样式应用到宽度小于 800 像素的屏幕,请使用:
@media screen and (max-width: 800px) { /* Styles for screens less than 800px wide */ }
提供的代码示例使用以下媒体查询:
/* Smartphones (portrait and landscape) */ @media only screen and (min-device-width : 320px) and (max-device-width : 480px) { /* Styles for smartphones in portrait and landscape */ } /* Smartphones (landscape) */ @media only screen and (min-width : 321px) { /* Styles for smartphones in landscape */ } /* Smartphones (portrait) */ @media only screen and (max-width : 320px) { /* Styles for smartphones in portrait */ } /* iPads (portrait and landscape) */ @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) { /* Styles for iPads in portrait and landscape */ } /* iPads (landscape) */ @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) { /* Styles for iPads in landscape */ } /* iPads (portrait) */ @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) { /* Styles for iPads in portrait */ } /* Desktops and laptops */ @media only screen and (min-width : 1224px) { /* Styles for desktops and laptops */ } /* Large screens */ @media only screen and (min-width : 1824px) { /* Styles for large screens */ } /* iPhone 4 - 5s */ @media only screen and (-webkit-min-device-pixel-ratio : 1.5), only screen and (min-device-pixel-ratio : 1.5) { /* Styles for iPhone 4 - 5s */ } /* iPhone 6 */ @media only screen and (max-device-width: 667px) only screen and (-webkit-device-pixel-ratio: 2) { /* Styles for iPhone 6 */ } /* iPhone 6+ */ @media only screen and (min-device-width : 414px) only screen and (-webkit-device-pixel-ratio: 3) { /* Styles for iPhone 6+ */ } /* Samsung Galaxy S7 Edge */ @media only screen and (-webkit-min-device-pixel-ratio: 3), and (min-resolution: 192dpi)and (max-width:640px) { /* Styles for Samsung Galaxy S7 Edge */ }
这些媒体查询涵盖各种屏幕尺寸,包括智能手机、平板电脑和更大的屏幕尺寸屏幕。
考虑使用 em 值而不是像素,以便在不同屏幕尺寸下获得更大的灵活性。如需进一步指导,请参阅 Zell Weekley 的文章“媒体查询单元”。
以上是媒体查询如何帮助您设计适合不同屏幕尺寸的响应式网站?的详细内容。更多信息请关注PHP中文网其他相关文章!