How to center a div

Mighil·1 hour ago
/* GLOBAL: make sizing easier to reason about. */
* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 36px 16px;
  font-family: system-ui, sans-serif;
  background: #f5f6f8;
  color: #20242b;
}

.demo { width: min(100%, 760px); margin: 0 auto; }
.page-header { margin-bottom: 34px; }
.eyebrow { color: #64748b; font-size: 11px; font-weight: 800; letter-spacing: .12em; }
h1 { margin: 8px 0; font-size: clamp(28px, 5vw, 40px); }
.intro { color: #586273; line-height: 1.6; }
.legend { display: flex; flex-wrap: wrap; gap: 18px; margin-top: 18px; color: #586273; font-size: 13px; }
.legend span { display: flex; align-items: center; gap: 8px; }
.legend i { width: 18px; height: 12px; display: inline-block; border-radius: 3px; }
.legend-parent { border: 2px dashed #8995a5; }
.legend-child { background: #dceafe; border: 1px solid #8bb5f0; }

.lesson { margin: 34px 0; }
.lesson-heading { display: flex; align-items: flex-start; gap: 14px; margin-bottom: 12px; }
.number { display: grid; place-items: center; flex: 0 0 36px; height: 36px; border-radius: 10px; background: #e8edf5; color: #334155; font-weight: 800; font-size: 13px; }
h2 { margin: 0 0 5px; font-size: 18px; }
.lesson-heading p { margin: 0; color: #626b78; font-size: 14px; line-height: 1.5; }

/* VISUAL KEY: dashed outer border = parent; blue box = child. */
.stage {
  position: relative;
  min-height: 190px;
  padding: 16px;
  border: 2px dashed #9aa4b2;
  border-radius: 12px;
  background: #fff;
}
.stage-short { min-height: 112px; }
.stage-label { position: absolute; top: 8px; left: 10px; color: #7b8492; font-size: 9px; font-weight: 800; letter-spacing: .1em; }

.child {
  width: 190px;
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  border: 1px solid #8bb5f0;
  border-radius: 8px;
  background: #dceafe;
  color: #173b70;
  font-weight: 800;
  font-size: 12px;
  text-align: center;
}

/* 01 - Only the CHILD needs this rule. A width lets auto margins work. */
.margin-center { margin: 22px auto 0; }

/* 02 - These alignment rules belong to the PARENT container. */
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 03 - Grid's place-items centers the CHILD in the PARENT. */
.grid-center {
  display: grid;
  place-items: center;
}

/* 04 - Parent is the positioning reference for its absolute child. */
.relative-stage { position: relative; }
.absolute-center {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.axis-note { position: absolute; bottom: 9px; left: 0; right: 0; text-align: center; color: #64748b; font-size: 11px; pointer-events: none; }
pre { overflow-x: auto; margin: 12px 0 8px; padding: 16px; border-radius: 10px; background: #202936; color: #e5edf7; font-size: 12px; line-height: 1.65; }
code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.tip { margin: 8px 0 0; color: #596579; font-size: 13px; line-height: 1.6; }
.tip code { background: #edf1f6; padding: 2px 4px; border-radius: 4px; }
.takeaway { margin-top: 42px; padding: 20px; border: 1px solid #d8e0eb; border-radius: 12px; background: #eef3f9; }
.takeaway p { margin-bottom: 0; color: #4b586a; line-height: 1.6; }

@media (max-width: 480px) {
  body { padding: 24px 14px; }
  .child { width: 160px; }
  pre { font-size: 11px; }
}