我有这个@media setup:
HTML:
<head>
<meta name="viewport" content="width=device-width, user-scalable=no" />
</head>
CSS:
@media screen and (min-width: 769px) {
/* STYLES HERE */
}
@media screen and (min-device-width: 481px) and (max-device-width: 768px) {
/* STYLES HERE */
}
@media only screen and (max-device-width: 480px) {
/* STYLES HERE */
}
通过这种设置,它可以在iPhone上运行,但不能在浏览器上运行。
是因为我已经在元设备,也许有max-width:480px代替?
我发现最好的方法是为旧的浏览器编写默认的CSS,因为旧的浏览器(包括IE 5.5、6、7和8)不能读取@media。当我使用@media时,我是这样使用的:
<style type="text/css">
/* default styles here for older browsers.
I tend to go for a 600px - 960px width max but using percentages
*/
@media only screen and (min-width: 960px) {
/* styles for browsers larger than 960px; */
}
@media only screen and (min-width: 1440px) {
/* styles for browsers larger than 1440px; */
}
@media only screen and (min-width: 2000px) {
/* for sumo sized (mac) screens */
}
@media only screen and (max-device-width: 480px) {
/* styles for mobile browsers smaller than 480px; (iPhone) */
}
@media only screen and (device-width: 768px) {
/* default iPad screens */
}
/* different techniques for iPad screening */
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:portrait) {
/* For portrait layouts only */
}
@media only screen and (min-device-width: 481px) and (max-device-width: 1024px) and (orientation:landscape) {
/* For landscape layouts only */
}
</style>
但是你可以用你的@media做任何你喜欢的事情。这只是我在为所有浏览器构建样式时发现的最适合我的一个例子。
iPad CSS规范。
也!如果你在寻找可打印性,你可以使用@media print{}。
潜在的问题是使用max-device-width vs使用普通的max-width。
使用“device”关键字的目标是屏幕的物理尺寸,而不是浏览器窗口的宽度。
例如:
@media only screen and (max-device-width: 480px) {
/* STYLES HERE for DEVICES with physical max-screen width of 480px */
}
与
@media only screen and (max-width: 480px) {
/* STYLES HERE for BROWSER WINDOWS with a max-width of 480px.
This will work on desktops when the window is narrowed. */
}
如果网站在小设备上的行为像桌面屏幕,那么你必须把这个元标签放在头之前
<meta name="viewport" content="width=device-width, initial-scale=1">
对于媒体查询,您可以将其设置为
这将覆盖你所有的移动/手机宽度
@media only screen and (min-width: 200px) and (max-width: 767px) {
//Put your CSS here for 200px to 767px width devices (cover all width between 200px to 767px //
}
对于iPad和iPad pro,你必须使用
@media only screen and (min-width: 768px) and (max-width: 1024px) {
//Put your CSS here for 768px to 1024px width devices(covers all width between 768px to 1024px //
}
如果你想为横向模式添加css,你可以添加这个
和(朝向:景观)
@media only screen and (min-width: 200px) and (max-width: 767px) and (orientation : portrait) {
//Put your CSS here for 200px to 767px width devices (cover all mobile portrait width //
}