示例工程 21-media-queries

21-media-queries/app/style.css

css · 4.9 KB

运行:媒体查询实验室

该示例需要 localStorage / IndexedDB / Service Worker,请用「新窗口打开」获得完整能力。

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

*,
*::before,
*::after {
  box-sizing: border-box;
}

body,
h1,
h2,
h3,
p {
  margin: 0;
}

body {
  background: #f8fafc;
  color: #0f172a;
  font-family: system-ui, -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif;
  line-height: 1.7;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px 24px;
  border-bottom: 1px solid #e2e8f0;
  background: #ffffff;
}

.brand {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.14em;
}

.site-header nav {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
}

.site-header nav a {
  color: #64748b;
  font-size: 0.8125rem;
  text-decoration: none;
}

.site-header nav a:hover,
.site-header nav a:focus-visible {
  color: #2563eb;
}

.page {
  width: min(100% - 40px, 880px);
  margin: 0 auto;
  padding: 40px 0 64px;
}

.eyebrow {
  color: #64748b;
  font-size: 0.6875rem;
  letter-spacing: 0.18em;
}

h1 {
  margin: 6px 0 10px;
  font-size: clamp(1.75rem, 5vw, 2.5rem);
  line-height: 1.15;
}

.lead {
  max-width: 52ch;
  color: #334155;
  font-size: 1rem;
}

.panel {
  margin-top: 18px;
  padding: 22px;
  border: 1px solid #e2e8f0;
  border-radius: 16px;
  background: #ffffff;
}

h2 {
  font-size: 1.125rem;
}

.panel > p {
  margin-top: 6px;
  color: #334155;
  font-size: 0.875rem;
}

.meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
  color: #64748b;
  font-size: 0.75rem;
}

.meta span {
  padding: 2px 8px;
  border-radius: 999px;
  background: #eff6ff;
  color: #1e40af;
}

/* 1. 宽度断点 */
.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
  margin-top: 12px;
}

.tile {
  display: grid;
  place-items: center;
  min-height: 64px;
  border-radius: 8px;
  background: #2563eb;
  color: #ffffff;
  font-size: 0.75rem;
  font-family: ui-monospace, Menlo, monospace;
}

.bp-mid,
.bp-narrow {
  display: none;
  background: #f5f3ff;
  color: #6d28d9;
}

/* 2. 配色 */
.scheme-card {
  margin-top: 12px;
  padding: 18px;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: #ffffff;
  color: #0f172a;
}

.scheme-card h3 {
  font-size: 1rem;
}

.scheme-card p {
  margin-top: 6px;
  color: #64748b;
  font-size: 0.8125rem;
}

/* 3. 动效 */
.motion-demo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 12px;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #2563eb;
  animation: pulse 1.6s ease-in-out infinite;
}

.motion-text {
  color: #64748b;
  font-size: 0.75rem;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.6); opacity: 0.55; }
}

/* 4. 输入与方向 */
.input-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.input-card {
  padding: 16px;
  border: 1px solid #e2e8f0;
  border-radius: 12px;
  background: #f8fafc;
}

.input-card h3 {
  font-size: 0.9375rem;
}

.input-card p {
  margin-top: 6px;
  color: #64748b;
  font-size: 0.8125rem;
}

.hover-yes,
.landscape {
  display: none;
}

.print-only {
  display: none;
}

.site-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 20px 24px 36px;
  color: #64748b;
  font-size: 0.75rem;
}

/* 断点:先写宽屏默认,再向下覆盖 */
@media (max-width: 1024px) {
  .grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .bp-wide,
  .bp-narrow {
    display: none;
  }

  .bp-mid {
    display: inline-block;
  }
}

@media (max-width: 640px) {
  .site-header {
    padding: 12px 18px;
  }

  .page {
    width: min(100% - 28px, 880px);
    padding: 24px 0 44px;
  }

  .panel {
    padding: 16px;
  }

  .grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .input-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .bp-wide,
  .bp-mid {
    display: none;
  }

  .bp-narrow {
    display: inline-block;
  }
}

/* 系统配色 */
@media (prefers-color-scheme: dark) {
  .scheme-card {
    border-color: #334155;
    background: #0f172a;
    color: #e2e8f0;
  }

  .scheme-card p {
    color: #94a3b8;
  }
}

/* 减少动效 */
@media (prefers-reduced-motion: reduce) {
  .dot {
    animation: none;
  }
}

/* 悬停能力 */
@media (hover: hover) {
  .hover-no {
    display: none;
  }

  .hover-yes {
    display: block;
  }

  .input-card:hover {
    border-color: #bfdbfe;
    background: #ffffff;
  }
}

/* 屏幕方向 */
@media (orientation: landscape) {
  .portrait {
    display: none;
  }

  .landscape {
    display: block;
  }
}

/* 打印 */
@media print {
  .site-header,
  .site-footer,
  .meta,
  .motion-demo {
    display: none;
  }

  .panel {
    border: 0;
    padding: 0 0 12px;
  }

  .print-only {
    display: block;
    margin-top: 12px;
    padding: 8px 0;
    border-top: 1px solid #94a3b8;
    font-size: 0.75rem;
  }

  .scheme-card,
  .input-card {
    border: 1px solid #94a3b8;
    box-shadow: none;
  }

  body {
    background: #ffffff;
  }
}