目錄
Understanding the syntax
Common breakpoints for devices
Using range conditions
Orientation and resolution
首頁 web前端 css教學 如何將媒體查詢用於CSS中的不同屏幕大小

如何將媒體查詢用於CSS中的不同屏幕大小

Sep 21, 2025 am 04:23 AM
css

響應式設計通過媒體查詢實現,先定義移動優先的最小寬度條件,再逐步適配平板和桌面端,結合範圍、方向與分辨率優化佈局。

How to use media queries for different screen sizes in CSS

Responsive design starts with understanding how to adapt your layout based on screen size using media queries. These allow you to apply CSS rules only when certain conditions—like screen width—are met.

Understanding the syntax

A media query uses the @media rule followed by a condition and a block of CSS. The most common use checks the viewport width.

@media (max-width: 768px) {
  .container {
    width: 100%;
    padding: 10px;
  }
}

This applies styles only when the screen is 768px or smaller. You can also use min-width for larger screens, which supports mobile-first design.

Common breakpoints for devices

Designers often target typical device widths. Here are standard ranges:

  • Mobile: up to 767px
  • Tablet: 768px to 1023px
  • Desktop: 1024px and above

You can define multiple queries to handle each case:

/* Mobile styles */
.container {
  padding: 15px;
}
<p>/ <em>Tablet</em> /
@media (min-width: 768px) {
.container {
width: 750px;
margin: 0 auto;
}
}</p> <p>/ <em>Desktop</em> /
@media (min-width: 1024px) {
.container {
width: 1000px;
}
}</p>

Using range conditions

Sometimes you need styles for a specific range. Combine min and max values:

@media (min-width: 768px) and (max-width: 1023px) {
  .sidebar {
    display: none;
  }
}

This hides the sidebar only on tablets, keeping it visible on desktop and mobile if needed.

Orientation and resolution

You can also target device orientation or pixel density:

  • Landscape vs portrait: @media (orientation: landscape)
  • High-DPI screens: @media (-webkit-min-device-pixel-ratio: 2)

Useful for optimizing images or layout on phones held sideways or retina displays.

Basically, start small and scale up with min-width, test on real devices, and keep layout changes logical. Media queries give precise control without JavaScript.

以上是如何將媒體查詢用於CSS中的不同屏幕大小的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Stock Market GPT

Stock Market GPT

人工智慧支援投資研究,做出更明智的決策

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

如何使用純CSS創建下拉菜單 如何使用純CSS創建下拉菜單 Sep 20, 2025 am 02:19 AM

使用HTML和CSS可創建無需JavaScript的下拉菜單。 2.通過:hover偽類觸發子菜單顯示。 3.利用嵌套列表構建結構,CSS設置隱藏與懸浮顯示效果。 4.可添加過渡動畫提升視覺體驗。

如何防止圖像拉伸或縮小CSS 如何防止圖像拉伸或縮小CSS Sep 21, 2025 am 12:04 AM

useobject-fitormax-widthwithheight:自動置換式; object-fitControlshowimagesfillcontainersfillcontainerswhilepreservingaspectratios,andmax-width:100%;高度;高度:autoEsoensuresResresresResresRessersRessiveScalingScalingWithOutStertracterging。

如何使用CSS添加盒子陰影效果 如何使用CSS添加盒子陰影效果 Sep 20, 2025 am 12:23 AM

USETHEBOX-SHADOWPROPERTYTOADDDROPSHADOWS.DEFINEHORIZONTALANDVERTICALESTESETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETSETESTESTESTESTESTESTEMENG:MMULTIPLESHADOWSARECOMMA-SEPARAWS.MEULTIPLESHADOWSARECOMMA-SEPARATED.EXAMPL

如何在CSS中使用clamp()函數進行響應式版式 如何在CSS中使用clamp()函數進行響應式版式 Sep 23, 2025 am 01:24 AM

clamp()函數通過最小、首选和最大值實現響應式字體縮放;2.語法為clamp(最小值,首選值,最大值),常用rem和vw單位;3.字體在小屏取最小值,隨屏幕增大按vw縮放,不超過最大值;4​​.合理選擇數值確保可讀性,避免過大或過小;5.結合rem類型比例提升設計一致性。

如何使用CSS背景過濾器進行磨砂玻璃效果 如何使用CSS背景過濾器進行磨砂玻璃效果 Sep 24, 2025 am 01:55 AM

使用backdrop-filter:blur()實現磨砂玻璃效果,結合rgba透明背景、細邊框和圓角,如.frosted-card{backdrop-filter:blur(10px);background-color:rgba(255,255,255,0.1);border:1pxsolidrgba(255,255,255,0.2);border-radius:12px;padding:20px;},需確保元素背後有內容,且注意瀏覽器兼容性。

Chrome瀏覽器怎麼設置打印時去掉頁眉頁腳_打印頁面頁眉頁腳隱藏方法 Chrome瀏覽器怎麼設置打印時去掉頁眉頁腳_打印頁面頁眉頁腳隱藏方法 Sep 25, 2025 am 09:54 AM

1、打開網頁打印界面,點擊“更多設置”並取消勾選“頁眉和頁腳”即可去除自動添加的網址、日期等信息。 2、通過在網頁代碼中添加@mediaprint{@page{margin:0}}的CSS樣式,可清除默認邊距與頁眉頁腳。 3、安裝如PrintEdit等第三方打印擴展程序,能更靈活編輯打印內容並禁用默認頁眉頁腳。

如何使用CSS創建響應式正方形 如何使用CSS創建響應式正方形 Sep 24, 2025 am 03:28 AM

使用aspect-ratio:1/1可創建響應式正方形,現代瀏覽器中設置寬高比即可;若需兼容舊版瀏覽器,可用padding-top:100%技巧,通過相對單位保持寬高一致;也可用vw單位使正方形隨視口變化。

如何將媒體查詢用於CSS中的不同屏幕大小 如何將媒體查詢用於CSS中的不同屏幕大小 Sep 21, 2025 am 04:23 AM

響應式設計通過媒體查詢實現,先定義移動優先的最小寬度條件,再逐步適配平板和桌面端,結合範圍、方向與分辨率優化佈局。

See all articles