從VSCode起手式到多媒體網頁製作
開啟VSCode,建立新檔案,檔名存為 index.html
在空白檔案中輸入驚嘆號「!」然後按Tab鍵
在 VSCode 輸入: !
按 Tab 鍵後自動產生:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>把 <title>Document</title> 改成你想要的網頁標題
<title>我的第一個網頁</title>
在 <body> 和 </body> 之間加入內容
<body>
<h1>歡迎來到我的世界</h1>
<h2>這裡有我的夢想與故事</h2>
<h3>一起探索美好的未來</h3>
</body>在body內加入style標籤,設定基本樣式
重要:白色文字需要深色背景才看得見!
<body>
<style>
body {
text-align: center;
padding: 50px;
color: white;
background-color: navy; /* 加入深藍色背景 */
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 32px;
margin-bottom: 15px;
}
h3 {
font-size: 24px;
}
</style>
<h1>歡迎來到我的世界</h1>
<h2>這裡有我的夢想與故事</h2>
<h3>一起探索美好的未來</h3>
</body>存檔後,在瀏覽器中開啟你的 index.html 檔案
你應該會看到三個不同大小的標題,文字是白色的,置中顯示
重要:你只需要加入一行程式碼就好!
先準備一張圖片,放在跟HTML檔案同一個資料夾
然後在你現有的body樣式中只要加入一行 background-image
在你現有的body樣式中,只要加入這一行:
background-image: url('your-image.jpg');
完整的body樣式會變成:
body {
text-align: center;
padding: 50px;
color: white;
background-color: navy;
background-image: url('your-image.jpg'); ← 只加這一行!
}如果圖片沒有顯示,按照以下步驟檢查:
如果圖片載入失敗,會顯示深藍色背景(backup)
現在要學習重要的背景圖片置中技巧
在body樣式中加入這些屬性:
<style>
body {
text-align: center;
padding: 50px;
color: white;
background-image: url('your-image.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 100vh;
}
h1 {
font-size: 48px;
margin-bottom: 20px;
}
h2 {
font-size: 32px;
margin-bottom: 15px;
}
h3 {
font-size: 24px;
}
</style>在深色背景上,白色文字可能不夠清楚,加入文字陰影
h1 {
font-size: 48px;
margin-bottom: 20px;
text-shadow: 2px 2px 4px black;
}
h2 {
font-size: 32px;
margin-bottom: 15px;
text-shadow: 2px 2px 4px black;
}
h3 {
font-size: 24px;
text-shadow: 2px 2px 4px black;
}在三個標題上方加入導覽列
<body>
<style>
/* 之前的樣式保持不變 */
</style>
<nav>
<div class="logo">我的網站</div>
<ul>
<li><a href="index.html">首頁</a></li>
<li><a href="media.html">多媒體</a></li>
<li><a href="#contact">聯絡</a></li>
</ul>
</nav>
<h1>歡迎來到我的世界</h1>
<h2>這裡有我的夢想與故事</h2>
<h3>一起探索美好的未來</h3>
</body>在style標籤內加入導覽列的CSS
nav {
background-color: rgba(0, 0, 0, 0.8);
padding: 15px 20px;
position: fixed;
top: 0;
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
color: white;
font-size: 24px;
font-weight: bold;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
display: flex;
}
nav ul li {
margin-left: 30px;
}
nav ul li a {
color: white;
text-decoration: none;
font-size: 18px;
}
nav ul li a:hover {
color: yellow;
}因為導覽列是固定在上方,需要調整內容位置
body {
text-align: center;
padding-top: 100px;
color: white;
background-image: url('your-image.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 100vh;
}現在要建立第二個HTML檔案,命名為 media.html
這個新網頁將包含:音樂 + 影片 + 3個標語
在VSCode中建立新檔案,再次使用"!"起手式
在新檔案中輸入: ! 按Tab產生基本結構後,修改title: <title>多媒體展示</title>
在body中加入導覽列、標語、影片和音樂
<body>
<style>
body {
text-align: center;
padding-top: 100px;
padding-bottom: 50px;
color: white;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
}
nav {
background-color: rgba(0, 0, 0, 0.8);
padding: 15px 20px;
position: fixed;
top: 0;
left: 0;
right: 0;
}
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav ul li {
display: inline;
margin-right: 20px;
}
nav ul li a {
color: white;
text-decoration: none;
font-size: 18px;
}
nav ul li a:hover {
color: yellow;
}
.logo {
color: white;
font-size: 24px;
font-weight: bold;
display: inline;
}
h1 {
font-size: 42px;
margin-bottom: 20px;
text-shadow: 2px 2px 4px black;
}
h2 {
font-size: 28px;
margin-bottom: 15px;
text-shadow: 2px 2px 4px black;
}
h3 {
font-size: 22px;
margin-bottom: 30px;
text-shadow: 2px 2px 4px black;
}
video {
width: 80%;
max-width: 800px;
margin: 30px 0;
border-radius: 10px;
}
audio {
width: 80%;
max-width: 600px;
margin: 30px 0;
}
</style>
<nav>
<div class="logo">我的網站</div>
<ul>
<li><a href="index.html">首頁</a></li>
<li><a href="media.html">多媒體</a></li>
<li><a href="#contact">聯絡</a></li>
</ul>
</nav>
<h1>感受生活的美好</h1>
<h2>用心聆聽世界的聲音</h2>
<h3>讓音樂與影像說故事</h3>
<video controls>
<source src="your-video.mp4" type="video/mp4">
您的瀏覽器不支援影片播放
</video>
<br>
<audio controls>
<source src="your-music.mp3" type="audio/mpeg">
您的瀏覽器不支援音樂播放
</audio>
</body>完成後你需要準備以下檔案: