示例工程 56-responsive-nav
56-responsive-nav/app/style.css
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
*,
*::before,
*::after {
box-sizing: border-box;
}
body,
h1,
h2,
p {
margin: 0;
padding: 0;
}
body {
background: var(--paper);
color: var(--ink);
font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
line-height: 1.7;
}
.container {
width: min(100% - 2rem, 72rem);
margin-inline: auto;
}
.site-header {
position: sticky;
inset-block-start: 0;
z-index: 20;
border-block-end: 1px solid var(--border);
background: var(--surface);
}
.header-inner {
display: flex;
align-items: center;
justify-content: space-between;
gap: var(--space-3);
min-height: 64px;
}
.brand {
color: var(--ink);
font-size: var(--text-sm);
font-weight: 700;
letter-spacing: 0.18em;
text-decoration: none;
}
.site-nav {
display: flex;
flex-wrap: wrap;
gap: var(--space-4);
}
.site-nav a {
color: var(--muted);
font-size: var(--text-sm);
text-decoration: none;
}
.site-nav a:hover,
.site-nav a:focus-visible,
.site-nav a[aria-current="page"] {
color: var(--brand);
}
.nav-toggle {
display: none;
align-items: center;
justify-content: center;
min-height: 44px;
padding-inline: var(--space-4);
border: 1px solid var(--border);
border-radius: var(--radius-sm);
background: var(--surface);
color: var(--ink);
font: inherit;
font-size: var(--text-sm);
cursor: pointer;
}
.brand:focus-visible,
.site-nav a:focus-visible,
.nav-toggle:focus-visible,
.drawer-nav a:focus-visible,
.drawer-close:focus-visible {
outline: 3px solid #b45309;
outline-offset: 2px;
}
.hero {
padding-block: clamp(3rem, 8vw, 6rem);
background: var(--brand-soft);
}
.eyebrow {
color: var(--brand-dark);
font-size: var(--text-xs);
letter-spacing: 0.18em;
}
.hero h1 {
max-width: 20ch;
margin-block: var(--space-2) var(--space-3);
font-size: clamp(2rem, 1.3rem + 3vw, 3.5rem);
line-height: 1.08;
}
.lead {
max-width: 46ch;
color: var(--muted);
}
.section {
padding-block: var(--space-8);
scroll-margin-block-start: 80px;
}
.section + .section {
border-block-start: 1px solid var(--border);
}
.section h2 {
font-size: var(--text-2xl);
}
.section p {
margin-block-start: var(--space-2);
color: var(--muted);
}
.link {
color: var(--brand);
text-underline-offset: 3px;
}
.link:focus-visible {
outline: 3px solid #b45309;
outline-offset: 2px;
}
.site-footer {
border-block-start: 1px solid var(--border);
background: var(--surface);
}
.footer-inner {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
gap: var(--space-2);
padding-block: var(--space-4);
color: var(--muted);
font-size: var(--text-xs);
}
/* 抽屉:原生 dialog,模态打开 */
.nav-drawer {
position: fixed;
inset: 0 auto 0 0;
width: min(86vw, 360px);
max-width: none;
height: 100dvh;
max-height: none;
margin: 0;
padding: 0;
border: 0;
background: var(--surface);
color: var(--ink);
box-shadow: 12px 0 32px rgb(15 23 42 / 18%);
}
.nav-drawer::backdrop {
background: rgb(15 23 42 / 45%);
}
.drawer-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: var(--space-4);
border-block-end: 1px solid var(--border);
}
.drawer-title {
font-size: var(--text-xs);
font-weight: 700;
letter-spacing: 0.18em;
}
.drawer-close {
display: inline-flex;
align-items: center;
justify-content: center;
inline-size: 44px;
block-size: 44px;
border: 1px solid var(--border);
border-radius: 50%;
background: var(--surface);
color: var(--ink);
font: inherit;
font-size: 20px;
line-height: 1;
cursor: pointer;
}
.drawer-nav {
display: grid;
padding: var(--space-2);
}
.drawer-nav a {
display: flex;
align-items: center;
min-height: 44px;
padding-inline: var(--space-3);
border-radius: var(--radius-sm);
color: var(--ink);
font-size: var(--text-base);
text-decoration: none;
}
.drawer-nav a:hover,
.drawer-nav a:focus-visible,
.drawer-nav a[aria-current="page"] {
background: var(--brand-soft);
color: var(--brand-dark);
}
/* 打开抽屉时锁定背景滚动 */
body:has(.nav-drawer[open]) {
overflow: hidden;
}
@media (max-width: 767px) {
.site-nav {
display: none;
}
.nav-toggle {
display: inline-flex;
}
}
@media (min-width: 768px) {
.nav-drawer {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto;
}
}