` 等标签让内容更有语义,也便于后期添加样式。
图片占位符:`src=" 是一个在线占位图片服务,在实际项目中应替换为你自己的图片路径。
三、美化与适配:使用CSS进行响应式设计
CSS负责网页的视觉效果。我们的目标是让网页在不同宽度的屏幕(尤其是手机)上都能好看好用。
1. 设置基础样式与重置
打开 `style.css` 文件,首先添加一些基础样式,清除不同浏览器的默认差异,并设置盒模型:
```css
/ 基础重置与设置 /
{
margin: 0;
padding: 0;
box-sizing: border-box; / 重要:使元素宽度包含padding和border /
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
line-height: 1.6;
color: 333;
background-color: f9f9f9;
padding: 10px; / 为手机屏幕增加一点内边距 /
/ 限制更大宽度,防止在大屏幕上内容过宽 /
container {
max-width: 优质成分;
overflow-x: hidden; / 防止横向滚动条 /
```
2. 为各区域添加移动端优先的样式
我们采用“移动端优先”的原则,先为小屏幕设计样式。
```css
/ Header 样式 /
header {
background: linear-gradient(135deg, 6a11cb 0%, 2575fc 优质成分);
color: white;
padding: 1.5rem 1rem;
text-align: center;
border-radius: 0 0 10px 10px;
header h1 {
font-size: 1.8rem;
margin-bottom: 0.5rem;
nav a {
color: white;
text-decoration: none;
margin: 0 10px;
padding: 5px 10px;
display: inline-block; / 方便设置内边距 /
/ Main 内容区样式 /
hero {
text-align: center;
padding: 2rem 1rem;
background-color: white;
margin: 1rem 0;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
hero h2 {
font-size: 1.5rem;
color: 2575fc;
margin-bottom: 1rem;
content article {
background: white;
padding: 1.5rem;
margin-bottom: 1rem;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
content h3 {
font-size: 1.3rem;
margin-bottom: 0.8rem;
color: 333;
content p {
margin-bottom: 1rem;
font-size: 1rem; / 手机端基础字号 /
/ 图片响应式 /
img {
max-width: 优质成分; / 关键:图片宽度不会超过其容器 /
height: auto; / 高度自动等比例缩放 /
display: block;
margin: 1rem auto;
border-radius: 5px;
/ Footer 样式 /
footer {
text-align: center;
padding: 1.5rem;
margin-top: 2rem;
color: 666;
font-size: 0.9rem;
border-top: 1px solid eee;
```
3. 使用媒体查询适配更大屏幕(可选但推荐)
媒体查询(Media Query)是响应式设计的核心工具。当屏幕宽度大于某个值时(例如平板或电脑),我们覆盖之前的样式,提供更适合的布局。
```css
/ 针对平板及以上屏幕的优化 (屏幕宽度大于768px时生效) /
@media (min-width: 768px) {
body {
padding: 20px;
max-width: 750px; / 居中显示,限制宽度 /
margin: 0 auto; / 水平居中 /
header h1 {
font-size: 2.2rem;
hero h2 {
font-size: 1.8rem;
content {
display: flex; / 使用弹性布局,让文章并排 /
flex-wrap: wrap; / 如果空间不足则换行 /
gap: 1rem; / 设置子元素之间的间隙 /
content article {
flex: 1 1 calc(50%
1rem); / 每行显示两个,减去间隙 /
min-width: 300px; / 小巧宽度 /
```
四、测试、发布与优化
1. 本地测试
在浏览器中直接打开 `index.html` 文件。
按 F12 或右键选择“检查”打开开启者工具。
找到 切换设备工具栏 的图标(通常像手机和平板叠在一起),点击它。
在上方的设备型号下拉菜单中,选择不同的手机型号(如iPhone 12, Pixel 5)查看效果,也可以手动拖动边缘调整宽度。
确保在不同宽度下,文字清晰可读,布局没有错乱,图片正常缩放,所有按钮链接都可点击。
2. 发布到网络(让其他人能访问)
要将网页放到公网上,你需要一个托管服务。对于静态网页,有很多免费选择:
GitHub Pages:关联你的GitHub仓库即可自动发布。
Netlify / Vercel:拖拽项目文件夹上传或关联Git仓库,几分钟即可获得一个专属网址。
3. 基础优化检查清单
速度:压缩图片(使用 TinyPNG 等在线工具),减少不必要的代码。
可访问性:为所有图片添加 `alt` 描述文字,确保颜色对比度足够(可使用浏览器开启者工具中的“无障碍”功能检查)。
功能:确保所有链接有效,表单(如果有)能正常工作。