為了創建響應式設計(并且廣泛支持瀏覽器),我們最常檢測到的功能是視口寬度,如果視口大于或小于某個寬度(或確切的寬度),則可以使用`min-width`、`max-width`以及`width`媒體功能。
~~~css
/* 如果文檔寬度小于 300 像素則修改背景顏色 */
@media screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
~~~
`orientation`媒體功能定義輸出設備中的頁面可見區域高度是否大于或等于寬度,能夠測試縱向或橫向模式。
~~~css
/* 在設備橫向放置時更改正文文本顏色 */
@media (orientation: landscape) {
body {
color: lightblue;
}
}
~~~
使用`@media`查詢來制作相應式設計:
~~~html
<p class="example">操作瀏覽器窗口,查看效果</p>
~~~
~~~css
.example {
padding: 20px;
color: white;
}
/* Extra small devices (phones, 576px and down) */
@media only screen and (max-width: 576px) {
.example {
background-color: red;
}
}
/* Small devices (portrait tablets and large phones, 576px and up) */
@media only screen and (min-width: 576px) {
.example {
background-color: green;
}
}
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
.example {
background-color: blue;
}
}
/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
.example {
background-color: orange;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
.example {
background-color: pink;
}
}
~~~
- 第一章:移動開發入門
- 第一節:概述
- 第二節:基礎概念
- 第一課時:像素
- 第二課時:視口
- 第二章:Flex 布局
- 第一節:概述
- 第二節:容器屬性
- 第一課時:flex-direction 屬性
- 第二課時:flex-wrap 屬性
- 第三課時:flex-flow 屬性
- 第四課時:justify-content 屬性
- 第五課時:align-items 屬性
- 第六課時:align-content 屬性
- 第三節:項目屬性
- 第一課時:order 屬性
- 第二課時:flex-grow 屬性
- 第三課時:flex-shrink 屬性
- 第四課時:flex-basis 屬性
- 第五課時:flex 屬性
- 第六課時:align-self 屬性
- 第四節:Flex 實例
- 第一課時:常見頁面布局
- 第三章:響應式布局
- 第一節:概述
- 第二節:媒體查詢
- 第一課時:概述
- 第二課時:響應式設計
- 第三節:柵格系統
- 第一課時:概述
- 第二課時:案例分析
- 第三課時:Bootstrap 簡介
- 第四節:響應式案例
- 第一課時:三星首頁
- 第四章:移動端適配
- 第五章:移動端事件
- 第一節:概述
- 第二節:touch 事件
- 第三節:觸摸事件對象
- 第四節:其他事件
- 第五節:移動端幻燈片
- 第六章:移動端常見問題
- 第一節:瀏覽器兼容性
- 第二節:移動端動畫
- 第三節:300ms 延遲
- 第四節:文字溢出省略
- 第五節:水平居中和垂直居中
- 第七章:項目案例
- 第一節:美團外賣
- 第一課時:首頁
- 第二課時:訂單頁面
- 第三課時:我的頁面
- 第四課時:詳情頁面
- 第五課時:購物車頁