CSS3 Media Queries 實現響應式設計

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   使用   sp   java   for   

Max Width

下面的樣式會在可視地區的寬度小於 600px 的時候被應用。

12345 @media screen and (max-width: 600px) {  .class {    background: #ccc;  }}

如果你想連結到一個單獨的樣式表,把下面的代碼放在<head>標籤裡。

1 <link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css" />
Min Width

下面的樣式會在可視地區的寬度大於 900px 的時候被應用。

12345 @media screen and (min-width: 900px) {  .class {    background: #666;  }}
Multiple Media Queries

你還可以使用過個匹配條件,下面的樣式會在可視地區的寬度在 600px 和 900px 之間的時候被應用。

12345 @media screen and (min-width: 600px) and (max-width: 900px) {  .class {    background: #333;  }}
Device Width

下面的樣式會在 max-device-width 是 480px 的裝置上觸發。(提示:max-device-width 是裝置的實際解析度,而 max-width 指的是可視地區解析度。)

12345 @media screen and (max-device-width: 480px) {  .class {    background: #000;  }} 
For iPhone 4

下面的樣式是為 iPhone 4 專門寫的 (作者: Thomas Maier)。

1 <link rel="stylesheet" media="only screen and (-webkit-min-device-pixel-ratio: 2)" type="text/css" href="iphone4.css" /> 
For iPad

你還可以使用 media query 在 iPad 上檢測方向(portrait or landscapse) (作者: Cloud Four)。

12 <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css"><link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css"> 
Media Queries for IE

遺憾是的,IE8 及更老版本的瀏覽器不支援 CSS3 Media Queries,不過可以使用 Javascript  彌補,下面是一些解決方案:

 

CSS3 Media Queries 實現響應式設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.