示例工程 14-flex-alignment
14-flex-alignment/app/style.css
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
*,
*::before,
*::after {
box-sizing: border-box;
}
body,
h1,
h2,
h3,
p {
margin: 0;
}
body {
min-height: 100vh;
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;
gap: 16px;
padding: 16px 24px;
border-bottom: 1px solid #e2e8f0;
background: #ffffff;
}
.brand {
font-size: 13px;
font-weight: 700;
letter-spacing: 0.16em;
}
.site-header nav {
display: flex;
gap: 16px;
}
.site-header nav a {
color: #64748b;
font-size: 13px;
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: 11px;
letter-spacing: 0.18em;
}
h1 {
margin: 6px 0 10px;
font-size: clamp(30px, 5vw, 44px);
line-height: 1.12;
}
.lead {
color: #334155;
font-size: 16px;
}
.panel {
margin-top: 18px;
padding: 24px;
border: 1px solid #e2e8f0;
border-radius: 16px;
background: #ffffff;
}
h2 {
font-size: 19px;
}
h3 {
font-size: 15px;
}
.panel p {
margin-top: 8px;
color: #334155;
font-size: 14px;
}
.tag {
display: inline-block;
margin-bottom: 6px;
color: #2563eb;
font-size: 11px;
font-family: ui-monospace, Menlo, monospace;
}
.stage {
display: flex;
gap: 8px;
padding: 8px;
border: 1px dashed #cbd5e1;
border-radius: 10px;
background: #f8fafc;
}
.stage i {
display: grid;
place-items: center;
border-radius: 6px;
background: #2563eb;
color: #ffffff;
font-size: 12px;
font-style: normal;
font-family: ui-monospace, Menlo, monospace;
}
/* 1. justify-content */
.justify-list {
display: grid;
gap: 10px;
margin-top: 14px;
}
.justify-row {
display: grid;
grid-template-columns: 128px minmax(0, 1fr);
align-items: center;
gap: 10px;
}
.justify-row .tag {
margin: 0;
}
.justify {
min-height: 52px;
}
.justify i {
width: 56px;
height: 34px;
}
.start { justify-content: flex-start; }
.center { justify-content: center; }
.between { justify-content: space-between; }
.around { justify-content: space-around; }
.evenly { justify-content: space-evenly; }
/* 2. align-items */
.align-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
margin-top: 14px;
}
.tall {
height: 120px;
}
.tall i {
width: 46px;
height: 28px;
}
.tall i:nth-child(2) { height: 52px; }
.tall i:nth-child(3) { height: 76px; }
.stretch { align-items: stretch; }
.tall.stretch i { height: auto; }
.start { align-items: flex-start; }
.center-y { align-items: center; }
.end { align-items: flex-end; }
/* 3. align-content */
.lines-grid {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
margin-top: 14px;
}
.multi {
flex-wrap: wrap;
align-content: stretch;
width: 100%;
height: 160px;
gap: 6px;
padding: 6px;
}
.multi i {
flex: 0 0 auto;
width: 52px;
height: 30px;
}
.between-lines { align-content: space-between; }
.center-lines { align-content: center; }
/* 4. align-self 与 baseline */
.self-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 12px;
margin-top: 14px;
}
.self-demo i {
width: 46px;
height: 44px;
}
.self-demo .pick {
align-self: flex-end;
height: 30px;
background: #db2777;
}
.baseline-demo {
align-items: baseline;
min-height: 96px;
padding: 12px;
}
.baseline-demo b {
color: #0f172a;
font-family: ui-monospace, Menlo, monospace;
}
.baseline-demo .small { font-size: 12px; }
.baseline-demo .mid { font-size: 18px; }
.baseline-demo .large { font-size: 28px; }
/* 5. 等高卡片 */
.cards {
display: flex;
gap: 12px;
margin-top: 14px;
}
.card {
display: flex;
flex: 1 1 0;
flex-direction: column;
gap: 8px;
padding: 16px;
border: 1px solid #e2e8f0;
border-radius: 12px;
background: #f8fafc;
}
.card p {
margin: 0;
flex: 1 1 auto;
color: #64748b;
font-size: 13px;
}
.card button {
margin-top: auto;
min-height: 44px;
border: 0;
border-radius: 8px;
background: #2563eb;
color: #ffffff;
font-size: 13px;
}
.site-footer {
padding: 20px 24px 36px;
color: #64748b;
font-size: 12px;
text-align: center;
}
@media (max-width: 720px) {
.site-header {
align-items: flex-start;
flex-direction: column;
padding: 14px 18px;
}
.page {
width: min(100% - 28px, 880px);
padding: 28px 0 48px;
}
.panel {
padding: 18px;
}
.justify-row {
grid-template-columns: 1fr;
}
.align-grid,
.lines-grid,
.self-grid {
grid-template-columns: 1fr;
}
.cards {
flex-direction: column;
}
}