263

私はこの@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うか?

4

6 に答える 6

408

古いブラウザ(IE 5.5、6、7、8を含む)は読み取れないため、古いブラウザ用にデフォルトのCSSを作成するのが最善の方法であることがわかりました@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{}

于 2012-11-25T11:47:55.520 に答える
45

根本的な問題はmax-device-widthvs plain old の使用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.  */
}
于 2014-07-08T19:24:23.623 に答える
9

属性の正しい値には、代わりに次contentを含める必要があります。initial-scale

<meta name="viewport" content="width=device-width, initial-scale=1">
                                                   ^^^^^^^^^^^^^^^

于 2016-11-11T21:06:43.383 に答える