下面是“首页横幅(Banner)”示例的逐行注释版(保存为 banner-demo-commented.html 可直接运行):
<!doctype html> <!-- 声明 HTML5 文档类型,让浏览器以标准模式渲染 -->
<html lang="zh-CN"> <!-- 页面语言设为简体中文 -->
<head>
<meta charset="utf-8" /> <!-- 使用 UTF-8 编码,避免中文乱码 -->
<meta name="viewport" content="width=device-width, initial-scale=1" /> <!-- 视口设置:移动端适配 -->
<title>Hero Banner Demo</title> <!-- 浏览器标签标题 -->
<style> /* 内联样式开始(示例用,正式项目可放到独立 .css 文件) */
/* --- 基础变量(便于课堂演示时快速微调) --- */
:root{ /* 变量作用域:根元素 */
--hero-height: 320px; /* 横幅高度,接近截图比例 */
--overlay: rgba(0,0,0,.35); /* 遮罩基色(越大越暗) */
--title-size: clamp(28px, 6vw, 64px); /* 主标题的响应式字号 */
--subtitle-size: clamp(14px, 2.2vw, 24px); /* 副标题的响应式字号 */
} /* :root 结束 */
/* --- 轻量 reset:确保不同浏览器起点一致 --- */
*{ box-sizing: border-box; } /* 统一盒模型:包含内边距与边框 */
html,body{ margin:0; padding:0; } /* 清除默认外边距与内边距 */
/* --- 横幅容器 --- */
.hero{ /* 承载背景图与文本的主区域 */
position: relative; /* 为伪元素遮罩提供定位上下文 */
height: var(--hero-height); /* 使用变量控制高度 */
width: 100%; /* 全宽铺满 */
background-image: /* 设置背景图片(可替换为项目内资源) */
url("<https://images.unsplash.com/photo-1465101162946-4377e57745c3?q=80&w=1920&auto=format&fit=crop>");
background-size: cover; /* 按比例裁切铺满容器 */
background-position: center; /* 背景图居中对齐 */
color: #fff; /* 文本默认白色,提升对比度 */
display: flex; /* 使用 Flex 布局 */
align-items: center; /* 垂直方向居中文案块 */
} /* .hero 结束 */
/* --- 朦胧遮罩:提升文字可读性 --- */
.hero::before{ /* 使用 ::before 生成一个覆盖层 */
content: ""; /* 必需:使伪元素可见 */
position: absolute; /* 绝对定位,覆盖在背景图之上 */
inset: 0; /* top/right/bottom/left 全部为 0,铺满容器 */
background: linear-gradient( /* 线性渐变:左侧更暗,右侧渐淡,接近截图质感 */
to right,
var(--overlay) 0%,
rgba(0,0,0,.25) 40%,
rgba(0,0,0,.20) 70%,
rgba(0,0,0,.15) 100%
);
} /* ::before 遮罩结束 */
/* --- 文案容器:控制文本区位置与宽度 --- */
.hero-inner{
position: relative; /* 使其位于遮罩层之上(不被遮罩覆盖) */
max-width: 1200px; /* 文案行宽上限,便于大屏阅读 */
padding: 0 24px; /* 两侧留白,避免贴边 */
margin-left: 48px; /* 略向内缩,贴近截图版式的左对齐效果 */
} /* .hero-inner 结束 */
/* --- 主标题样式 --- */
.hero-title{
margin: 0 0 10px; /* 清除上边距,保留下边距分隔副标题 */
font-weight: 800; /* 极粗,增强标题力量感 */
font-size: var(--title-size); /* 使用响应式字号变量 */
letter-spacing: .5px; /* 微调字距,让大字更稳重 */
} /* .hero-title 结束 */
/* --- 副标题样式 --- */
.hero-subtitle{
margin: 0; /* 去掉默认外边距,避免上下挤压 */
font-size: var(--subtitle-size); /* 使用响应式字号变量 */
font-weight: 400; /* 常规字重,层级弱于标题 */
opacity: .95; /* 略微降低不透明度,显得柔和 */
} /* .hero-subtitle 结束 */
/* --- 窄屏适配(手机端) --- */
@media (max-width: 576px){ /* 当视口宽度 ≤ 576px 生效 */
:root{ --hero-height: 260px; } /* 降低横幅高度,避免占屏过多 */
.hero-inner{ margin-left: 24px; }/* 左侧内缩更小,贴合小屏视觉 */
} /* 媒体查询结束 */
</style> <!-- 内联样式结束 -->
</head>
<body> <!-- 页面可见内容开始 -->
<!-- 首页横幅(Hero Section):添加无障碍属性方便读屏器识别含义 -->
<section class="hero" role="img" aria-label="花朵背景横幅"> <!-- 背景图由 CSS 提供 -->
<div class="hero-inner"> <!-- 文案容器,放置标题与副标题 -->
<h1 class="hero-title">前端成长工坊</h1> <!-- 大标题:与截图一致的文案 -->
<p class="hero-subtitle">点亮课堂瞬间,我们一起见证学生成长每一步</p> <!-- 副标题描述 -->
</div> <!-- .hero-inner 结束 -->
</section> <!-- .hero 区域结束 -->
</body> <!-- 页面主体结束 -->
</html> <!-- 文档结束 -->