HTML <center> Tag

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

Definition and Usage

The <center> element is a block-level container that horizontally centers its contents. It was common in early HTML for titles and banners.

Do not use it in new projects. Prefer CSS: text-align: center for text/inline content, and margin: 0 auto for block boxes with a set width. Keep presentation in CSS and meaning in HTML (<div>, <p>, and so on).

Deprecated / obsolete.

Avoid in new code. Deprecated since HTML 4 in favor of CSS; obsolete in HTML5. Browsers still render it for old pages, but conforming documents must not use it.

HTML Syntax

syntax.html
<center> content</center>

Both tags are required when you encounter legacy markup. Nesting more <center> elements is unnecessary — one wrapper centered everything inside. Modern replacement: a normal block + CSS, not this tag.

Attributes

<center> has no element-specific attributes. Only the global attributes apply (and the element itself is obsolete).

AttributeDescriptionStatus
None specificNo dedicated centering attributes — the tag itself forced centered layout.Obsolete element

Global Attributes

The <center> tag also supports the Global Attributes in HTML.

Live Preview

Compare obsolete <center> with the CSS text-align: center approach.

Same sentence, three ways. Prefer CSS for new pages.

Preview

Welcome to CodeToFun

Using text-align: center on a div

HTML Examples

Study the old tag, then ship the CSS replacements. Use Try It Yourself to experiment.

Example 1 — Obsolete <center> (Legacy Only)

How old pages centered content. Do not copy this into new projects.

HTML
<center>
  This text will be centered.
  <p>So will this paragraph.</p>
</center>
Try It Yourself

How It Works

  1. Open <center>. It creates a block that centers its contents.
  2. Put text or blocks inside — paragraphs and inline text inherit the centering effect.
  3. Close the tag. Browsers still paint it for compatibility.
  4. Do not ship this. Validators reject it; use CSS instead (next examples).

Example 2 — Center Text with CSS

Recommended replacement: a normal container plus text-align: center.

HTML
<style>
  .center { text-align: center; }
</style>

<div class="center">
  This text will be centered.
  <p>So will this paragraph.</p>
</div>
Try It Yourself

How It Works

  1. Use a semantic or generic block such as <div> or <p>.
  2. Set text-align: center on that block.
  3. Inline content and text inside are centered; the box width itself stays full (unless you change it).
  4. Prefer a class over repeating inline styles so many elements share one rule.

Example 3 — Center a Block Box

To center a box (not just its text), set a width and use auto left/right margins.

HTML
<style>
  .box {
    width: 200px;
    margin: 0 auto;
    border: 1px solid #94a3b8;
    text-align: center;
  }
</style>

<div class="box">Centered box</div>
Try It Yourself

How It Works

  1. Give the block a width less than its parent (e.g. 200px).
  2. Set margin-left and margin-right to auto (shorthand margin: 0 auto).
  3. The leftover space is split equally, so the box sits in the middle.
  4. Optional: add text-align: center if the text inside should also be centered.

Example 4 — Migrate Old Markup

Replace a legacy <center> heading with a heading + CSS.

HTML
<!-- Before (obsolete) -->
<center><h1>Site Title</h1></center>

<!-- After (recommended) -->
<style>
  .page-title { text-align: center; }
</style>
<h1 class="page-title">Site Title</h1>
Try It Yourself

How It Works

  1. Keep the meaningful element (<h1> stays an <h1>).
  2. Remove <center> from the markup.
  3. Move centering to CSS with text-align: center on the heading or a parent.
  4. Result: same look, valid HTML, styles reusable across pages.

Edge Cases & Pitfalls

Most mistakes mix “center the text” with “center the box,” or keep shipping obsolete tags.

obsolete

Do not use in new HTML

Even though browsers still draw <center>, HTML5 marks it obsolete. Validators fail, and teams should treat it as legacy-only.

css

text-align ≠ block centering

text-align: center centers inline content inside a box. To center the box itself, use a width plus margin: 0 auto (or modern layout like flex/grid).

semantics

Presentation is not meaning

<center> only means “look centered.” Prefer headings, paragraphs, and sections, then style them.

legacy

Nested <center> stacks

Old pages sometimes nested many centers. One CSS rule on a parent replaces all of that noise.

Browser Support

Browsers still render <center> for old pages, but the element is obsolete in the HTML standard. Prefer CSS everywhere.

✓ Obsolete · Legacy rendering

<center>

Still painted by Chrome, Firefox, Safari, Edge, and Opera for compatibility. Do not use it in new documents — use text-align and margin (or flex/grid) instead.

— Not recommended for new sites
Google Chrome All
Renders (legacy)
Mozilla Firefox All
Renders (legacy)
Apple Safari All
Renders (legacy)
Microsoft Edge All
Renders (legacy)
Opera All
Renders (legacy)
Samsung Internet All
Renders (legacy)
Internet Explorer All
Renders (legacy)
<center> Legacy support only

Bottom line: Support exists only so old pages keep working. For new work, choose CSS centering — never the center element.

Key Takeaways

  • Status: <center> is obsolete — study it, do not ship it.
  • Text: use text-align: center on a block container.
  • Boxes: width + margin: 0 auto centers the block itself.
  • Migrate: keep semantic tags; move layout into CSS classes.

One line: replace <center> with normal HTML plus CSS text-align / margin: auto.

Default CSS Settings

User agents map the obsolete element roughly to this presentational rule:

default.css
center {
  display: block;
  text-align: center;
}

That is why a CSS class with the same text-align rule is the direct modern equivalent for centered text.

Frequently Asked Questions

No. The center element is obsolete (deprecated). Do not use it in new pages. Prefer CSS text-align: center for inline content and margin: 0 auto for block boxes with a width.
It was a block-level element that horizontally centered its contents inside the containing block (often the body).
Yes, browsers still render it for backward compatibility, but validators flag it and it is not allowed in conforming HTML documents.
Apply text-align: center to a block container such as a div or p. That centers the text (and inline content) inside the element.
Give the block a width, then use margin-left: auto and margin-right: auto (or margin: 0 auto). text-align alone does not shrink or center the block box itself.
Yes. Replace <center>…</center> with a semantic element (div, p, section, etc.) plus CSS, so layout stays in stylesheets and markup stays meaningful.

Next: cite

Mark the title of a creative work with the HTML cite element.

cite tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful