{
  "format": "css.observer/playground@1",
  "id": "66a01275-6b7c-41cf-9428-272ab67c7de1",
  "url": "https://css.observer/u/mighil/s/98eod6hfba",
  "title": "How to center a div",
  "description": "Beginner-friendly visual guide to centering a child div. Each example labels the parent container and child element, explains the result, and includes matching CSS comments.",
  "category": "layouts",
  "tags": [
    "centering",
    "flexbox",
    "grid",
    "css",
    "layout"
  ],
  "visibility": "public",
  "status": "published",
  "revision": 4,
  "author": "mighil",
  "forked_from": null,
  "created_at": "2026-12-02T13:40:01.300Z",
  "updated_at": "2026-12-02T14:08:15.892Z",
  "published_at": "2026-12-02T13:44:30.050Z",
  "source": {
    "html": "<main class=\"demo\">\n  <header class=\"page-header\">\n    <p class=\"eyebrow\">CSS BASICS / LAYOUT</p>\n    <h1>How to center a div</h1>\n    <p class=\"intro\">\n      The\n      <strong>parent</strong>\n      is the outer box. The\n      <strong>child</strong>\n      is the box we want to move.\n    </p>\n    <div class=\"legend\">\n      <span>\n        <i class=\"legend-parent\"></i>\n        Parent container\n      </span>\n      <span>\n        <i class=\"legend-child\"></i>\n        Child div\n      </span>\n    </div>\n  </header>\n\n  <section class=\"lesson\">\n    <div class=\"lesson-heading\">\n      <span class=\"number\">01</span>\n      <div>\n        <h2>Center horizontally with margin: auto</h2>\n        <p>Moves the child to the horizontal middle. It does not center vertically.</p>\n      </div>\n    </div>\n    <div class=\"stage stage-short\">\n      <span class=\"stage-label\">PARENT CONTAINER</span>\n      <div class=\"child margin-center\">CHILD DIV</div>\n    </div>\n    <pre><code>/* Give the child a width, then auto margins share the free space. */\n.child {\n  width: 190px;\n  margin: 0 auto;\n}</code></pre>\n    <p class=\"tip\">\n      <strong>Remember:</strong>\n      <code>margin: 0 auto</code>\n      means top/bottom margin is 0 and left/right margins are automatic.\n    </p>\n  </section>\n\n  <section class=\"lesson\">\n    <div class=\"lesson-heading\">\n      <span class=\"number\">02</span>\n      <div>\n        <h2>Center both ways with Flexbox</h2>\n        <p>\n          Make the parent a flex container. These two rules center its child horizontally and\n          vertically.\n        </p>\n      </div>\n    </div>\n    <div class=\"stage flex-center\">\n      <span class=\"stage-label\">PARENT CONTAINER</span>\n      <div class=\"child\">CHILD DIV</div>\n      <span class=\"axis-note\">Centered left-to-right and top-to-bottom</span>\n    </div>\n    <pre><code>/* Put these rules on the PARENT, not the child. */\n.parent {\n  display: flex;\n  justify-content: center; /* horizontal */\n  align-items: center;     /* vertical */\n}</code></pre>\n    <p class=\"tip\">\n      <strong>Remember:</strong>\n      The parent needs some height for vertical centering to be visible.\n    </p>\n  </section>\n\n  <section class=\"lesson\">\n    <div class=\"lesson-heading\">\n      <span class=\"number\">03</span>\n      <div>\n        <h2>Center both ways with CSS Grid</h2>\n        <p>\n          A compact alternative:\n          <code>place-items: center</code>\n          centers the child on both axes.\n        </p>\n      </div>\n    </div>\n    <div class=\"stage grid-center\">\n      <span class=\"stage-label\">PARENT CONTAINER</span>\n      <div class=\"child\">CHILD DIV</div>\n    </div>\n    <pre><code>/* Put this on the PARENT. */\n.parent {\n  display: grid;\n  place-items: center;\n}</code></pre>\n    <p class=\"tip\">\n      <strong>Remember:</strong>\n      Like Flexbox, the parent needs height if you want to see vertical centering.\n    </p>\n  </section>\n\n  <section class=\"lesson\">\n    <div class=\"lesson-heading\">\n      <span class=\"number\">04</span>\n      <div>\n        <h2>Center with absolute positioning</h2>\n        <p>Place the child at the parent's midpoint, then shift it back by half its own size.</p>\n      </div>\n    </div>\n    <div class=\"stage relative-stage\">\n      <span class=\"stage-label\">PARENT CONTAINER</span>\n      <div class=\"child absolute-center\">CHILD DIV</div>\n    </div>\n    <pre><code>/* Parent establishes the positioning area. */\n.parent {\n  position: relative;\n}\n\n/* These rules go on the CHILD. */\n.child {\n  position: absolute;\n  top: 50%;\n  left: 50%;\n  transform: translate(-50%, -50%);\n}</code></pre>\n    <p class=\"tip\">\n      <strong>Remember:</strong>\n      The parent needs\n      <code>position: relative</code>\n      so the child is positioned inside it.\n    </p>\n  </section>\n\n  <aside class=\"takeaway\">\n    <h2>Which one should I use?</h2>\n    <p>\n      For most everyday layouts, start with\n      <strong>Flexbox</strong>\n      or\n      <strong>Grid</strong>\n      . Use\n      <code>margin: 0 auto</code>\n      when you only need horizontal centering.\n    </p>\n  </aside>\n</main>\n",
    "css": "/* GLOBAL: make sizing easier to reason about. */\n* { box-sizing: border-box; }\n\nbody {\n  margin: 0;\n  padding: 36px 16px;\n  font-family: system-ui, sans-serif;\n  background: #f5f6f8;\n  color: #20242b;\n}\n\n.demo { width: min(100%, 760px); margin: 0 auto; }\n.page-header { margin-bottom: 34px; }\n.eyebrow { color: #64748b; font-size: 11px; font-weight: 800; letter-spacing: .12em; }\nh1 { margin: 8px 0; font-size: clamp(28px, 5vw, 40px); }\n.intro { color: #586273; line-height: 1.6; }\n.legend { display: flex; flex-wrap: wrap; gap: 18px; margin-top: 18px; color: #586273; font-size: 13px; }\n.legend span { display: flex; align-items: center; gap: 8px; }\n.legend i { width: 18px; height: 12px; display: inline-block; border-radius: 3px; }\n.legend-parent { border: 2px dashed #8995a5; }\n.legend-child { background: #dceafe; border: 1px solid #8bb5f0; }\n\n.lesson { margin: 34px 0; }\n.lesson-heading { display: flex; align-items: flex-start; gap: 14px; margin-bottom: 12px; }\n.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; }\nh2 { margin: 0 0 5px; font-size: 18px; }\n.lesson-heading p { margin: 0; color: #626b78; font-size: 14px; line-height: 1.5; }\n\n/* VISUAL KEY: dashed outer border = parent; blue box = child. */\n.stage {\n  position: relative;\n  min-height: 190px;\n  padding: 16px;\n  border: 2px dashed #9aa4b2;\n  border-radius: 12px;\n  background: #fff;\n}\n.stage-short { min-height: 112px; }\n.stage-label { position: absolute; top: 8px; left: 10px; color: #7b8492; font-size: 9px; font-weight: 800; letter-spacing: .1em; }\n\n.child {\n  width: 190px;\n  min-height: 58px;\n  display: flex;\n  align-items: center;\n  justify-content: center;\n  padding: 12px;\n  border: 1px solid #8bb5f0;\n  border-radius: 8px;\n  background: #dceafe;\n  color: #173b70;\n  font-weight: 800;\n  font-size: 12px;\n  text-align: center;\n}\n\n/* 01 - Only the CHILD needs this rule. A width lets auto margins work. */\n.margin-center { margin: 22px auto 0; }\n\n/* 02 - These alignment rules belong to the PARENT container. */\n.flex-center {\n  display: flex;\n  justify-content: center;\n  align-items: center;\n}\n\n/* 03 - Grid's place-items centers the CHILD in the PARENT. */\n.grid-center {\n  display: grid;\n  place-items: center;\n}\n\n/* 04 - Parent is the positioning reference for its absolute child. */\n.relative-stage { position: relative; }\n.absolute-center {\n  position: absolute;\n  top: 50%;\n  left: 50%;\n  transform: translate(-50%, -50%);\n}\n\n.axis-note { position: absolute; bottom: 9px; left: 0; right: 0; text-align: center; color: #64748b; font-size: 11px; pointer-events: none; }\npre { overflow-x: auto; margin: 12px 0 8px; padding: 16px; border-radius: 10px; background: #202936; color: #e5edf7; font-size: 12px; line-height: 1.65; }\ncode { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }\n.tip { margin: 8px 0 0; color: #596579; font-size: 13px; line-height: 1.6; }\n.tip code { background: #edf1f6; padding: 2px 4px; border-radius: 4px; }\n.takeaway { margin-top: 42px; padding: 20px; border: 1px solid #d8e0eb; border-radius: 12px; background: #eef3f9; }\n.takeaway p { margin-bottom: 0; color: #4b586a; line-height: 1.6; }\n\n@media (max-width: 480px) {\n  body { padding: 24px 14px; }\n  .child { width: 160px; }\n  pre { font-size: 11px; }\n}",
    "js": ""
  }
}