
- CSS 教程
- CSS - 首頁
- CSS - 路線圖
- CSS - 簡介
- CSS - 語法
- CSS - 選擇器
- CSS - 包含
- CSS - 度量單位
- CSS - 顏色
- CSS - 背景
- CSS - 字型
- CSS - 文字
- CSS - 圖片
- CSS - 連結
- CSS - 表格
- CSS - 邊框
- CSS - 塊級邊框
- CSS - 內聯邊框
- CSS - 外邊距
- CSS - 列表
- CSS - 內邊距
- CSS - 游標
- CSS - 輪廓
- CSS - 尺寸
- CSS - 捲軸
- CSS - 內聯塊
- CSS - 下拉選單
- CSS - 可見性
- CSS - 溢位
- CSS - 清除浮動
- CSS - 浮動
- CSS - 箭頭
- CSS - 調整大小
- CSS - 引號
- CSS - 順序
- CSS - 位置
- CSS - 連字元
- CSS - 懸停
- CSS - 顯示
- CSS - 聚焦
- CSS - 縮放
- CSS - 轉換
- CSS - 高度
- CSS - 連字元字元
- CSS - 寬度
- CSS - 不透明度
- CSS - Z-Index
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪下
- CSS - 書寫模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - 全部
- CSS - 內嵌
- CSS - 隔離
- CSS - 滾動溢位
- CSS - Justify Items
- CSS - Justify Self
- CSS - 製表符大小
- CSS - 指標事件
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - 最大塊大小
- CSS - 最小塊大小
- CSS - 混合模式
- CSS - 最大內聯大小
- CSS - 最小內聯大小
- CSS - 偏移量
- CSS - 口音色
- CSS - 使用者選擇
- CSS 高階
- CSS - 網格
- CSS - 網格佈局
- CSS - Flexbox
- CSS - 可見性
- CSS - 定位
- CSS - 圖層
- CSS - 偽類
- CSS - 偽元素
- CSS - @規則
- CSS - 文字效果
- CSS - 分頁媒體
- CSS - 列印
- CSS - 佈局
- CSS - 驗證
- CSS - 圖片精靈
- CSS - Important
- CSS - 資料型別
- CSS3 教程
- CSS3 - 教程
- CSS - 圓角
- CSS - 邊框圖片
- CSS - 多重背景
- CSS - 顏色
- CSS - 漸變
- CSS - 盒陰影
- CSS - 盒裝飾中斷
- CSS - 游標顏色
- CSS - 文字陰影
- CSS - 文字
- CSS - 2d 轉換
- CSS - 3d 轉換
- CSS - 過渡
- CSS - 動畫
- CSS - 多列
- CSS - 盒尺寸
- CSS - 工具提示
- CSS - 按鈕
- CSS - 分頁
- CSS - 變數
- CSS - 媒體查詢
- CSS - 函式
- CSS - 數學函式
- CSS - 遮罩
- CSS - 形狀
- CSS - 樣式圖片
- CSS - 特異性
- CSS - 自定義屬性
- CSS 響應式
- CSS RWD - 簡介
- CSS RWD - 視口
- CSS RWD - 網格檢視
- CSS RWD - 媒體查詢
- CSS RWD - 圖片
- CSS RWD - 影片
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 轉換器
- CSS - 顏色選擇器和動畫
- CSS 資源
- CSS - 有用資源
- CSS - 討論
CSS @media 規則
CSS @media 規則允許針對特定裝置並向其應用 CSS 樣式。如果媒體查詢與使用內容的裝置匹配,則 CSS 程式碼塊將應用於文件。我們可以在 JavaScript 中使用 CSSMediaRule CSS 物件模型介面來訪問使用 @media 媒體查詢生成的 CSS 規則。
語法
@media [media-type] ([media-feature]) { /* CSS Styles */ }
媒體型別
媒體型別用於 CSS 媒體查詢,以根據裝置應用不同的樣式。最常見的媒體型別是 all、print 和 screen。如果您未指定媒體型別,則它將匹配所有裝置。但是,如果您使用 not 或 only 邏輯運算子,則必須指定媒體型別。
值 | 描述 |
---|---|
all | 預設值。此值適用於所有媒體型別。 |
此值僅在列印文件時有效。 | |
screen | 此值僅對螢幕有效,例如計算機、平板電腦和智慧手機。 |
要了解有關媒體型別的更多資訊並檢視示例,請檢視 媒體型別。
媒體特徵
媒體特徵根據特定特徵、輸出裝置或環境對網頁應用不同的樣式。每個媒體特徵表示式都需要在其周圍加上括號。
要了解有關媒體特徵的更多資訊並檢視示例,請檢視 媒體特徵。
邏輯運算子
媒體查詢可以使用邏輯運算子(如 not、and、only 和 or 以及逗號)組合。下表顯示了可在媒體查詢中使用的邏輯運算子
邏輯運算子 | 說明 |
---|---|
and | 它將多個媒體特徵組合到單個媒體查詢中,其中每個連線的特徵都必須為真,整個查詢才能為真。 |
or | 這類似於逗號 , 運算子。這是在媒體查詢級別 4 中引入的。 |
not | 它可用於反轉條件的邏輯。只有在所有媒體特徵都為假時,not 運算子才為真。 |
only | 僅當整個媒體查詢匹配時才應用樣式。這有助於防止舊版瀏覽器應用樣式。 |
comma | 它將兩個或多個媒體查詢組合到單個規則中。如果逗號分隔列表中的任何媒體查詢為真,則整個媒體語句也將為真。這類似於 or 邏輯運算子。 |
CSS @media 規則示例
以下示例顯示了 @media 規則的使用。
@media 規則與列印和螢幕
以下示例演示瞭如何使用媒體查詢對元素應用不同的樣式,當它在螢幕上顯示或以列印頁面模式顯示時。
示例
<!DOCTYPE html> <html> <head> <style> @media print { p { color: red; } } @media screen { p { color: blue; } } button { background-color: violet; padding: 5px; } </style> </head> <body> <h2> CSS @media Rule </h2> <p> On the screen, the text will appear in blue. </p> <p> When you open the print preview, the text will be displayed in red. </p> <p> Click on below button to see the effect when you print the page. </p> <button onclick="printPage()"> Print Page </button> <script> function printPage() { window.print(); } </script> </body> </html>
@media 規則與 and 邏輯運算子
以下示例演示了當螢幕寬度在 600px 和 1000px 之間時,段落元素將具有藍色文字和黃色背景顏色。
示例
<!DOCTYPE html> <html> <head> <style> @media only screen and (min-width: 600px) and (max-width: 1100px) { p { color: blue; background: yellow } } </style> </head> <body> <h2> CSS @media Rule </h2> <p> When you open page on a screen, the paragraph elements will have blue text and a yellow background color. </p> </body> </html>
@media 規則與逗號邏輯運算子
以下示例演示了 @media screen, print 媒體查詢將應用於螢幕和列印媒體型別,元素的文字顏色對於兩者都將為紅色。
示例
<!DOCTYPE html> <html> <head> <style> @media screen, print { p { color: red; } } button { background-color: violet; padding: 5px; } </style> </head> <body> <h2> CSS @media Rule </h2> <p> When you open a page on a screen or printed page mode, the paragraph element will have red text. </p> <p> Click on below button to see the effect when you print the page. </p> <button onclick="printedPage()"> Print Page </button> <script> function printedPage() { window.print(); } </script> </body> </html>
@media 規則與範圍值
以下示例演示了當高度大於 300px 時,文字將為藍色,背景將為黃色。當寬度在 600px 和 1000px 之間時,文字將為紫色。
示例
<!DOCTYPE html> <html> <head> <style> @media (height < 300px) { h4 { background-color: yellow; color: blue; } } @media (600px <= width <= 1000px) { h5 { background-color: violet; } } </style> </head> </body> <h2> CSS @media Rule </h2> <h3> Resize the browser window to see the effect. </h3> <h4> When the height is greater than 300px, the text will be blue and background will be yellow. </h4> <h5> When the width is between 600 and 1000px, the text will be violet. </h5> </body> </html>
@media 規則與響應式導航選單
以下示例演示了導航欄的佈局將水平顯示,背景為粉紅色。當螢幕尺寸小於 700px 時,導航欄將垂直顯示,背景為紅色。
示例
<!DOCTYPE html> <html> <head> <style> nav ul { list-style: none; padding: 0; margin: 0; background-color: pink; padding: 10px; } nav li { display: inline; margin-right: 20px; color: blue; } nav a { text-decoration: none; text-align: center; } @media screen and (max-width: 700px) { nav ul { background-color: red; } nav li { display: block; margin: 10px 0; } } </style> </head> <body> <h2> CSS @media Rule </h2> <h2> Resize the browser window to see the effect. </h2> <nav> <ul> <li><a href="#"> Tutorialspoint </a></li> <li><a href="#"> Home </a></li> <li><a href="#"> Articles </a></li> <li><a href="#"> About us </a></li> </ul> </nav> </body> </html>
@media 規則與響應式列布局
以下示例演示了響應式列布局。當螢幕寬度小於 992px 時,列布局將從四列更改為兩列,當螢幕寬度小於 500px 時,列布局將更改,它們將彼此堆疊。
示例
<!DOCTYPE html> <html> <head> <style> .column-box { float: left; width: 25%; padding: 3px; box-sizing: border-box; background-color: pink; border: 2px solid blue; } @media screen and (max-width: 992px) { .column-box { width: 50%; } } @media screen and (max-width: 500px) { .column-box { width: 100%; } } </style> </head> <body> <h2> CSS @media Rule </h2> <h2> Resize the browser window to see the effect. </h2> <div class="column-box"> Box 1 </div> <div class="column-box"> Box 2 </div> <div class="column-box"> Box 3 </div> <div class="column-box"> Box 4 </div> </body> </html>
@media 規則與方向
以下示例演示了 orientation: landscape 媒體特徵,當視口處於橫向方向時,body 元素將具有綠色背景、黃色文字,並且 h3 元素將具有粉紅色文字。
示例
<!DOCTYPE html> <html> <head> <style> body { background-color: violet; } @media only screen and (orientation: landscape) { body { background-color: green; color: yellow; } h3 { color: pink; } } </style> </head> <body> <h2> CSS @media Rule </h2> <h3> Resize the browser window to see the effect. </h3> <p> when the viewport is in landscape orientation, the body element will have a green background, yellow text, and the h3 element will have pink text. </p> </body> </html>
支援的瀏覽器
屬性 | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
@media | 21 | 9 | 3.5 | 4.0 | 9 |