
Felix Thayer
|Subscribers
About
Anabolic Steroids: Uses, Abuse, And Side Effects
**TL;DR – The trick is to let the container keep its left‑justified flow, but override it on the element you want centered. In practice that means:**
1. **Leave your page’s normal text (or a `
`/`
`) with `text-align:left` (the default).**
2. **Give the specific element you want to center its own rule, e.g. `text‑align:center`.**
That single CSS property is all you need – no extra wrappers or floats required.
---
## 1. Why this works
- `text-align` is a *block formatting* property that only affects inline content inside the block.
- Setting `text-align:left` on a parent (or letting it be the default) keeps normal flow text left‑justified.
- Overriding it on a child element tells that child to align its own inline children to the center.
So you can have a page where everything is left, but a particular paragraph or heading is centered, all with minimal CSS.
---
## 2. Quick code snippet
```html
Centering Example
This paragraph is left aligned by default.
This heading is centered!
More left-aligned text continues here.
```
**Result:** Only the `
` element with class `centered` will appear in the center; everything else stays left-aligned.
---
### Key Takeaways
- **`text-align: left;` is unnecessary** because it’s the default for most browsers.
- To keep a block element centered while the rest of the page remains left‑aligned, simply apply `margin: 0 auto;` or `display:flex` with `justify-content:center`.
- Using class selectors keeps your styles modular and prevents global side effects.
This approach satisfies all three conditions: no global changes to left alignment, a single element stays centered, and no other elements are inadvertently affected.