--- id: howto-css-center-website title: "How To - Center Website" category: "Web_Recipes" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["margin auto center page", "center page horizontally CSS"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.82 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["howto", "css", "w3schools", "layout", "centering"] raw_sources: ["https://www.w3schools.com/howto/howto_css_center_website.asp"] applied_in: [] github_commit: "" --- # [[HOWTO CSS Center Website]] ## π― ν μ€ ν΅μ°° (One-line insight) `margin: auto` only centers a block horizontally if the element has an EXPLICIT width (here via `max-width`) β an unconstrained block always stretches to fill its parent, leaving no leftover space for the browser to distribute as equal left/right margins, which is the entire reason this recipe pairs `max-width: 500px` with `margin: auto` rather than using either alone; this is the simplest of all the centering recipes in this cookbook because it needs neither `position`, `transform`, nor Flexbox β just a width constraint plus auto margins. [S1] ## π§ ν΅μ¬ κ°λ (Core concepts) - **`margin: auto` requires a constrained width** β auto margins only distribute leftover horizontal space; a full-width block has none to distribute. [S1] - **`max-width` instead of `width`** β allows the content wrapper to shrink on narrow viewports (spanning the full page below the breakpoint) while capping its width on wide screens. [S1] - **No positioning/transform/flexbox needed** β the simplest technique in this cookbook's centering family, applicable to whole-page content wrappers rather than individual elements. [S1] ## π μΈλΆ λ΄μ© (Details) - `.content { max-width: 500px; margin: auto; }` applied to a wrapper `