好嘞—把“背景图链接”从 CSS 移到 HTML 的 style 属性里。下面给出最小可执行的三份文件:
/ (项目根)
├─ banner-local.html
├─ banner-url.html
├─ styles/
│ └─ hero.css
└─ images/
└─ banner.jpg ← 本地图片放这里
styles/hero.css(无背景图,仅通用样式):root {
--hero-height: 320px;
--overlay: rgba(0,0,0,.35);
--title-size: clamp(28px, 6vw, 64px);
--subtitle-size: clamp(14px, 2.2vw, 24px);
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
.hero {
position: relative;
height: var(--hero-height);
width: 100%;
color: #fff;
display: flex;
align-items: center;
background-size: cover;
background-position: center;
}
.hero::before {
content: "";
position: absolute;
inset: 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%
);
}
.hero-inner {
position: relative;
max-width: 1200px;
padding: 0 24px;
margin-left: 48px;
}
.hero-title {
margin: 0 0 10px;
font-weight: 800;
font-size: var(--title-size);
letter-spacing: .5px;
}
.hero-subtitle {
margin: 0;
font-size: var(--subtitle-size);
font-weight: 400;
opacity: .95;
}
@media (max-width: 576px) {
:root { --hero-height: 260px; }
.hero-inner { margin-left: 24px; }
}
banner-local.html<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hero Banner - Local Image</title>
<link rel="stylesheet" href="styles/hero.css">
</head>
<body>
<!-- 注意:这里把背景图写在 HTML 的 style 属性里 -->
<section class="hero" style="background-image:url('images/banner.jpg')"
role="img" aria-label="花朵背景横幅(本地图片)">
<div class="hero-inner">
<h1 class="hero-title">前端成长工坊</h1>
<p class="hero-subtitle">点亮课堂瞬间,我们一起见证学生成长每一步</p>
</div>
</section>
</body>
</html>
banner-url.html<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hero Banner - URL Image</title>
<link rel="stylesheet" href="styles/hero.css">
</head>
<body>
<!-- 使用在线图片 URL,同样写在 style 属性中 -->
<section class="hero" style="background-image:url('<https://images.unsplash.com/photo-1465101162946-4377e57745c3?q=80&w=1920&auto=format&fit=crop>')"
role="img" aria-label="花朵背景横幅(URL 图片)">
<div class="hero-inner">
<h1 class="hero-title">前端成长工坊</h1>
<p class="hero-subtitle">点亮课堂瞬间,我们一起见证学生成长每一步</p>
</div>
</section>
</body>
</html>
小提示:
images/banner.jpg 的相对路径是相对于 HTML 文件的(不是相对于 CSS)。styles/hero.css,结构清晰、便于课堂演示与维护。