我有@media
设置:
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上运行,但在浏览器中无效。
是因为我在元数据中已经有device
,可能会有max-width:480px
吗?
答案 0 :(得分:297)
我发现最好的方法是为旧浏览器编写默认CSS,因为旧版浏览器包括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做任何你喜欢的事情,这只是我为所有浏览器构建样式时最适合我的一个例子。
另外!如果您正在寻找可印刷性,可以使用@media print{}
答案 1 :(得分:33)
根本问题是使用max-device-width
vs普通旧max-width
。
使用&#34;设备&#34;关键字指的是屏幕的物理尺寸,而不是浏览器窗口的宽度。
例如:
@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. */
}
答案 2 :(得分:8)
content
属性的正确值应包括initial-scale
:
<meta name="viewport" content="width=device-width, initial-scale=1">
^^^^^^^^^^^^^^^
&#13;
答案 3 :(得分:0)
使用简单
@media only screen and (max-width: 600px) and (min-width: 400px) { }
答案 4 :(得分:0)
对于某些iPhone,您必须这样放置视口
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, shrink-to-fit=no, user-scalable=0" />