--- id: css-horizontal-align title: "CSS Horizontal Align" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["horizontal align", "margin auto", "center block element", "center image", "left right align"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.89 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "alignment", "layout"] raw_sources: ["https://www.w3schools.com/css/css_align_horizontal.asp"] applied_in: [] github_commit: "" --- # [[CSS Horizontal Align]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Horizontal alignment in CSS depends on the element: `margin: auto` (with a width) centers block elements, `text-align: center` centers text, `auto` left/right margins center images, and `position: absolute` or `float` push elements to the left or right. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Center block elements with `margin: auto`** β€” used to horizontally center a block-level element like `
`. [S1] - **Width is required for margin auto** β€” the example sets `width: 50%`; centering with `margin: auto` needs a defined width to take effect. [S1] - **Center text with `text-align: center`** β€” used to center text inside a block-level element. [S1] - **Center an image** β€” set `margin-left` and `margin-right` to `auto` and turn the image into a `block` element. [S1] - **Directional alignment** β€” `position: absolute` (with `right`/`left`) or `float` push an element to one side. [S1] - **Absolute positioning caveat** β€” absolute positioned elements are removed from the normal flow, and can overlap other elements. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Block centering pattern** β€” `margin: auto` + an explicit `width`. [S1] - **Image centering pattern** β€” `display: block` + `margin-left: auto` + `margin-right: auto`. [S1] - **Right-align patterns** β€” `position: absolute; right: 0px;` (removed from flow) or `float: right;` (stays in flow). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Center Align Block Elements** Use `margin: auto;`, to horizontally center a block-level element (like `
`). [S1] ```css .center { margin: auto; width: 50%; border: 3px solid green; padding: 10px; } ``` **Center Align Text** To center the text inside a block-level element, use `text-align: center;`. [S1] ```css p { text-align: center; } ``` **Center Align an Image** To center an image, set `margin-left` and `margin-right` to `auto`, and also turn the image into a `block` element. [S1] ```css img { display: block; margin-left: auto; margin-right: auto; width: 40%; } ``` **Left and Right Align β€” Using `position`** Another method for aligning elements is to use `position: absolute;`. Note: absolute positioned elements are removed from the normal flow, and can overlap other elements. [S1] ```css .right { position: absolute; right: 0px; width: 300px; border: 3px solid green; padding: 10px; } ``` **Left and Right Align β€” Using `float`** Another method for aligning an element to the left or right is to use the `float` property. [S1] ```css .right { float: right; width: 300px; border: 3px solid green; padding: 10px; } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The page's own applied examples are: centering a block `div` with `margin: auto`, centering paragraph text, centering an image, right-aligning with `position: absolute`, and right-aligning with `float`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Center a block element (language: CSS): ```css .center { margin: auto; width: 50%; } ``` Center an image: ```css img { display: block; margin-left: auto; margin-right: auto; width: 40%; } ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) - **`position: absolute` vs `float` for right alignment** β€” both push an element to the right, but `position: absolute` removes the element from the normal flow (it can overlap other elements), whereas `float: right` keeps it in the flow. [S1] - **By content type** β€” block elements β†’ `margin: auto`; text β†’ `text-align: center`; images β†’ block + auto side margins. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.89 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[CSS Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSS Center Align]], [[CSS Vertical Align]], [[CSS Float Examples]], [[CSS Position]] - **μ°Έμ‘° λ§₯락:** Referenced when horizontally centering or side-aligning blocks, text, and images. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” CSS Horizontal Align β€” https://www.w3schools.com/css/css_align_horizontal.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "CSS Horizontal Align" page (Astra wiki-curation, P-Reinforce v3.1 format).