示例工程 25-responsive-images

25-responsive-images/demo-02-object-fit/style.css

css · 0.8 KB

运行:object-fit 与比例

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

body {
  margin: 0;
  padding: 32px;
  background: #f8fafc;
  color: #0f172a;
  font-family: system-ui, sans-serif;
  line-height: 1.7;
}

h1 {
  margin: 0 0 8px;
  font-size: 26px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-top: 16px;
}

figure {
  margin: 0;
  overflow: hidden;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #ffffff;
}

img {
  display: block;
  width: 100%;
  height: 160px;
}

.cover { object-fit: cover; }
.contain { object-fit: contain; }
.fill { object-fit: fill; }

figcaption {
  padding: 8px;
  border-top: 1px solid #e2e8f0;
  color: #64748b;
  font-size: 11px;
  font-family: ui-monospace, monospace;
}

@media (max-width: 640px) {
  .grid {
    grid-template-columns: minmax(0, 1fr);
  }
}