
- 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 索引
- CSS - 底部
- CSS - 導航欄
- CSS - 覆蓋層
- CSS - 表單
- CSS - 對齊
- CSS - 圖示
- CSS - 圖片庫
- CSS - 註釋
- CSS - 載入器
- CSS - 屬性選擇器
- CSS - 組合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 計數器
- CSS - 剪裁
- CSS - 書寫模式
- CSS - Unicode 雙向演算法
- 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 響應式設計媒體查詢
在響應式網頁設計中,媒體查詢規則用於根據特定的螢幕寬度、縱橫比和解析度有條件地應用樣式。在本節中,我們將學習如何使用媒體查詢規則設計響應式網頁。
什麼是媒體查詢?
CSS 中的媒體查詢用於根據螢幕大小、解析度以及使用者裝置的其他特性應用不同的 CSS 樣式。媒體查詢使用@media 規則,在滿足特定條件時包含額外的 CSS 屬性塊。
新增斷點
在響應式設計中,**斷點**是佈局發生變化以更好地適應螢幕大小的特定螢幕寬度。我們可以透過定義具有最大或最小寬度的媒體查詢來新增斷點。以下程式碼顯示了確切的語法。
/* Example of a breakpoint for screens smaller than 768px */ @media (max-width: 768px) { .container { flex-direction: column; } }
在此示例中,當螢幕寬度為 768px 或更小時,`.container` 佈局會更改為列方向。
媒體查詢設定寬度限制
我們可以在媒體查詢中設定寬度限制,以僅在特定的螢幕寬度範圍內應用樣式。透過定義最小和最大寬度,我們可以在所需的螢幕尺寸範圍內控制元素的佈局和外觀。
示例
在以下示例中,當視口寬度大於 35em 且小於 85em 時,body 的背景顏色會更改為李子色。
<!DOCTYPE html> <html> <head> <style> body { background-color: white; /* Default background */ } @media (min-width: 35em) and (max-width: 85em) { body { background-color: plum; } } </style> </head> <body> <h1>Media Query Width Limit Example</h1> <p> Resize the browser window to see the background color change. </p> <p> <b> Note: </b> If you can't resize here, Run in Tutorialspoint HTML compiler </p> </body> </html>
媒體查詢多個斷點
透過在媒體查詢中使用多個斷點,我們可以為不同的螢幕尺寸和使用者裝置條件設計佈局樣式。讓我們看一個例子。
示例
在此示例中,我們定義了三個斷點,以調整小型、中型和大型螢幕的字型大小和佈局。
<!DOCTYPE html> <html> <head> <style> body { font-size: 16px; /* Default font size */ } /* Small screens (up to 600px wide) */ @media (max-width: 600px) { body { font-size: 14px; } } /* Medium screens (601px to 900px wide) */ @media (min-width: 601px) and (max-width: 900px) { body { font-size: 16px; } } /* Large screens (901px and up) */ @media (min-width: 901px) { body { font-size: 18px; } } </style> </head> <body> <h1>Multiple Breakpoints Example</h1> <p> Resize the browser window to see the background color change. </p> <p> <b> Note: </b> If you can't resize here, Run in Tutorialspoint HTML compiler </p> </body> </html>
CSS 媒體查詢用於螢幕方向
我們可以為使用者裝置的特定方向(橫向或縱向)定義樣式。此選項的預設值為縱向。
@media (orientation: portrait) { /* Styles */ }
示例
以下示例演示了當螢幕寬度大於 500px 且裝置處於橫向方向時,文字顏色會更改為藍色。
<!DOCTYPE html> <html> <head> <style> body { color: red; } @media (min-width: 500px) and (orientation: landscape) { body { color: blue; } } </style> </head> <body> <h3>Resize the browser window to see the effect.</h3> <p> The text color changed to blue when the viewport width is at least 500px and the device is in landscape orientation. </p> <p> <b> Note: </b> If you can't resize here, Run in Tutorialspoint HTML compiler </p> </body> </html>
廣告