假設讀者具備 HTML/CSS/JavaScript 基礎(元素、屬性、選擇器、盒模型、變數、函式、迴路)。需安裝現代瀏覽器最新版:Firefox 3.5+、Safari 4+、Opera 10+、Chrome 3+。無需伺服器,本地檔案即可運行所有範例。
<header>、<footer>、<nav>、<section>、<article>、<aside>、<main>、<figure>、<figcaption>、<time>、<mark>,賦予內容結構意義,利於 SEO 與無障礙。<canvas> + getContext('2d'),路徑、漸層、文字、影像、變換、像素操作,構建圖表、遊戲、編輯器。<video>、<audio>、<track>,徹底替代 Flash,支援字幕、多音軌、DRM (EME)。localStorage/sessionStorage(鍵值)、IndexedDB(資料庫)、Service Worker(可程式化 Proxy、離線快取、背景同步、推播)、Cache API。FormData、constraint validation API。<!DOCTYPE html>
<html lang="zh-Hant">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#2c3e50">
<title>現代部落格</title>
<link rel="manifest" href="manifest.json">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header class="site-header">
<h1><a href="/">現代部落格</a></h1>
<nav aria-label="主導航">
<ul><li><a href="/">首頁</a></li><li><a href="/archives">歸檔</a></li><li><a href="/about">關於</a></li></ul>
</nav>
</header>
<main>
<section class="hero" aria-labelledby="hero-title">
<h2 id="hero-title">探索前端新視野</h2>
<p>HTML5、CSS3、JavaScript 實戰教學</p>
</section>
<section class="posts" aria-labelledby="posts-title">
<h2 id="posts-title">最新文章</h2>
<article class="post-card">
<header><h3><a href="/pwa-intro">PWA 入門指南</a></h3><time datetime="2024-05-10">2024-05-10</time></header>
<p>Service Worker、Manifest、離線快取完整實作…</p>
<footer><a href="/pwa-intro">閱讀全文 →</a></footer>
</article>
<!-- 更多文章卡片 -->
</section>
</main>
<aside class="sidebar">
<section><h3>分類</h3><nav><ul><li><a>HTML5</a></li><li><a>CSS3</a></li><li><a>JS</a></li></ul></nav></section>
</aside>
<footer class="site-footer">
<p>© 2024 現代部落格. 以 CC BY-NC 4.0 授權釋出。</p>
</footer>
<script>if ('serviceWorker' in navigator) navigator.serviceWorker.register('/sw.js');</script>
</body>
</html>
:root {
--color-primary: #2c3e50;
--color-accent: #e74c3c;
--space: 1rem;
--radius: 6px;
--transition: 0.2s ease;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font: 1rem/1.6 system-ui, sans-serif; color: #333; background: #fafafa; }
.site-header { display: flex; justify-content: space-between; align-items: center; padding: var(--space); background: var(--color-primary); color: #fff; }
.posts { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: var(--space); padding: var(--space); }
.post-card { background: #fff; border-radius: var(--radius); box-shadow: 0 2px 8px rgba(0,0,0,0.08); overflow: hidden; transition: transform var(--transition), box-shadow var(--transition); }
.post-card:hover { transform: translateY(-4px); box-shadow: 0 8px 24px rgba(0,0,0,0.12); }
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
@media (max-width: 600px) { .site-header { flex-direction: column; gap: 0.5rem; } }
manifest.json
{
"name": "現代部落格",
"short_name": "部落格",
"start_url": "/",
"display": "standalone",
"background_color": "#fafafa",
"theme_color": "#2c3e50",
"icons": [
{"src": "icons/icon-192.png", "sizes": "192x192", "type": "image/png"},
{"src": "icons/icon-512.png", "sizes": "512x512", "type": "image/png"}
]
}
sw.js (Cache First 策略)
const CACHE = 'blog-v1';
const ASSETS = ['/', '/styles.css', '/manifest.json', '/icons/icon-192.png', '/icons/icon-512.png'];
self.addEventListener('install', e => {
e.waitUntil(caches.open(CACHE).then(c => c.addAll(ASSETS)));
});
self.addEventListener('fetch', e => {
e.respondWith(
caches.match(e.request).then(r => r || fetch(e.request).then(res => {
const copy = res.clone();
caches.open(CACHE).then(c => c.put(e.request, copy));
return res;
}))
);
});
self.addEventListener('activate', e => {
e.waitUntil(caches.keys().then(keys => Promise.all(keys.filter(k => k !== CACHE).map(k => caches.delete(k)))));
});
vite-plugin-pwa)從語意 HTML、現代 CSS 到 PWA 離線體驗,HTML5 + CSS3 已構建完整的現代 Web 開發閉環。