---
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).