CSS中響應式設計的概述

CSS中響應式設計的概述

本文說明了CSS中響應式設計的概觀。

我們將介紹響應式設計的基本概念,以及CSS在實現響應式設計時所扮演的角色。

YouTube Video

預覽的HTML

css-responsive-overview.html
  1<!DOCTYPE html>
  2<html lang="en">
  3<head>
  4    <meta charset="UTF-8">
  5    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6    <title>CSS Properties Example</title>
  7    <link rel="stylesheet" href="css-base.css">
  8    <link rel="stylesheet" href="css-responsive-overview.css">
  9</head>
 10<body>
 11    <!-- Header -->
 12    <header>
 13        <h1>CSS Properties For Responsive Design</h1>
 14    </header>
 15
 16    <!-- Main content -->
 17    <main>
 18        <header>
 19            <h2>Responsive Design Related Properties</h2>
 20        </header>
 21        <article>
 22            <h3>Media Queries</h3>
 23            <section>
 24                <header><h4>CSS</h4></header>
 25<pre class="sample">
 26/* When the screen width is 600px or less */
 27@media (max-width: 600px) {
 28  body {
 29    background-color: lightblue;
 30  }
 31}
 32/* When the screen width is between 601px and 1200px */
 33@media (min-width: 601px) and (max-width: 1200px) {
 34  body {
 35    background-color: lightgreen;
 36  }
 37}
 38/* When the screen width is 1201px or more */
 39@media (min-width: 1201px) {
 40  body {
 41    background-color: lightyellow;
 42  }
 43}
 44</pre>
 45            </section>
 46        </article>
 47        <article>
 48            <h3>Fluid Layouts</h3>
 49            <section>
 50                <header><h4>CSS</h4></header>
 51<pre class="sample">
 52.container {
 53  width: 80%; /* 80% width relative to the parent element */
 54  margin: 0 auto; /* Center the element */
 55}
 56</pre>
 57            </section>
 58        </article>
 59        <article>
 60            <h3>Responsive Images</h3>
 61            <section>
 62                <header><h4>CSS</h4></header>
 63<pre class="sample">
 64img {
 65  max-width: 100%;
 66  height: auto;
 67}
 68</pre>
 69            </section>
 70        </article>
 71        <article>
 72            <h3>Viewport</h3>
 73            <section>
 74                <header><h4>HTML</h4></header>
 75                <pre>&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;</pre>
 76            </section>
 77        </article>
 78        <article>
 79            <h3>CSS Frameworks</h3>
 80            <section>
 81                <header><h4>HTML</h4></header>
 82<pre>
 83&lt;div class="container"&gt;
 84  &lt;div class="row"&gt;
 85    &lt;div class="col-md-6"&gt;Left content&lt;/div&gt;
 86    &lt;div class="col-md-6"&gt;Right content&lt;/div&gt;
 87  &lt;/div&gt;
 88&lt;/div&gt;
 89</pre>
 90            </section>
 91        </article>
 92        <article>
 93            <h3>Flexbox &amp; Grid Layout</h3>
 94            <section>
 95                <header><h4>CSS</h4></header>
 96<pre class="sample">
 97/* Flex Example */
 98.container {
 99  display: flex;
100  flex-wrap: wrap; /* Allow elements to wrap */
101}
102
103.item {
104  flex: 1 1 200px; /* Each item has a minimum width of 200px */
105}
106
107/* Grid Example */
108.container {
109  display: grid;
110  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
111  gap: 16px;
112}
113</pre>
114            </section>
115        </article>
116    </main>
117</body>
118</html>

CSS中響應式設計的概述

在CSS中,響應式設計是一種技術,用於在各種設備和螢幕尺寸(如智能手機、平板電腦和桌面電腦)上適當地顯示網頁的佈局和內容。為了實現響應式設計,主要使用以下技術和方法。

媒體查詢

 1/* When the screen width is 600px or less */
 2@media (max-width: 600px) {
 3  body {
 4    background-color: lightblue;
 5  }
 6}
 7/* When the screen width is between 601px and 1200px */
 8@media (min-width: 601px) and (max-width: 1200px) {
 9  body {
10    background-color: lightgreen;
11  }
12}
13/* When the screen width is 1201px or more */
14@media (min-width: 1201px) {
15  body {
16    background-color: lightyellow;
17  }
18}

媒體查詢是一個功能,可根據設備的寬度、高度、解析度等應用不同的CSS規則。這使得可以根據螢幕大小應用不同的樣式。

在此例中,背景顏色根據螢幕寬度發生變化。

流體佈局

1.container {
2  width: 80%; /* 80% width relative to the parent element */
3  margin: 0 auto; /* Center the element */
4}

在響應式設計中,元素的寬度和高度使用百分比或em等相對單位來指定,而不是絕對像素值。這使得佈局可以根據螢幕大小靈活變化。

響應式圖片

1img {
2  max-width: 100%;
3  height: auto;
4}

圖片也會根據螢幕大小進行縮放。例如,使用max-width可以調整圖片,確保其不超過父元素的寬度。

視窗設定

1<meta name="viewport" content="width=device-width, initial-scale=1.0">

在HTML中使用<meta>標籤以確保瀏覽器正確解釋設備的螢幕寬度。如果沒有這個設定,響應式設計將無法正常工作,尤其是在移動設備上。

CSS框架的使用

像Bootstrap和Tailwind CSS這樣的CSS框架經常被用來高效實現響應式設計。這些框架被設計用來通過預定義的類輕鬆創建響應式佈局。

範例:Bootstrap的網格系統

1<div class="container">
2  <div class="row">
3    <div class="col-md-6">Left content</div>
4    <div class="col-md-6">Right content</div>
5  </div>
6</div>

在上述範例中,當螢幕寬度為 md(中等大小,通常為 768px 或以上)時,兩列並排排列;但在較窄的螢幕上,則垂直排列。

Flexbox 和網格佈局

 1/* Flex Example */
 2.container {
 3  display: flex;
 4  flex-wrap: wrap; /* Allow elements to wrap */
 5}
 6
 7.item {
 8  flex: 1 1 200px; /* Each item has a minimum width of 200px */
 9}
10
11/* Grid Example */
12.container {
13  display: grid;
14  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
15  gap: 16px;
16}

使用 CSS 的 flexboxgrid 可以進行靈活的佈局調整。這些佈局技術非常適合響應式設計,因為它們可以根據螢幕尺寸輕鬆自動調整佈局。

總結

響應式設計是一種重要的方法,能確保用戶無論使用何種設備都能舒適瀏覽網站。通過結合媒體查詢、靈活的佈局、框架以及像 Flexbox 和 Grid 這樣的新佈局技術,可以創建更靈活和自適應的網頁。

您可以在我們的 YouTube 頻道上使用 Visual Studio Code 來跟隨上述文章一起學習。 請也查看我們的 YouTube 頻道。

YouTube Video