203

I am just curious as to why the <center> tag in HTML was deprecated.

The <center> was a simple way of quickly center-aligning blocks of text and images by encapsulating the container in a <center> tag, and I really cannot find any simpler way on how to do it now.

Anyone know of any simple way on how to center "stuff" (not the margin-left:auto; margin-right:auto; and width thing), something that replaces <center> ? And also, why was it deprecated?

Andreas Grech
  • 98,643
  • 98
  • 284
  • 354

12 Answers12

238

The <center> element was deprecated because it defines the presentation of its contents — it does not describe its contents.

One method of centering is to set the margin-left and margin-right properties of the element to auto, and then set the parent element’s text-align property to center. This guarantees that the element will be centered in all modern browsers.

Jordan Ryan Moore
  • 6,657
  • 2
  • 24
  • 27
  • 7
    Yes, CSS is all about spliting presentation and data. – Ivan Nevostruev Nov 25 '09 at 18:10
  • 3
    Ivan, CSS is all about presentation; it is not about splitting - as the inline style attributes in some answers here demonstrate! It is of course a very good idea to use sensible classes and not inline CSS, but that is a distinct concept to using CSS for presentation. – Peter Boughton Nov 25 '09 at 18:45
  • 15
    I've had some cases (which I can't recall atm) where setting both the `margin` and `text-align` did not center my element in IE, but wrapping it with `
    ` did work... I'll try to find an example.
    – Mottie Nov 25 '09 at 19:39
  • fudgey, use a DOCTYPE and write valid code, and you will get *nearly* compliant behaviour from IE. Still far from perfect, but using a DOCTYPE is the vital first step to avoiding browser quirks. – Peter Boughton Nov 25 '09 at 22:44
  • 12
    the auto margin trick only works for block level elements. use `display: block;` on an inline element. – Steve Graham Dec 14 '09 at 15:21
  • 89
    I'm sure the seventeen nested divs on a typical WP or Drupal template really help to separate out the presentation details :P ( – detly Dec 14 '10 at 15:22
  • Does the `margin` have to be `0 auto`? I've tried just `auto` on a `pre` element, and it works the same in Gecko as `0 auto`, or when the `div` has `text-align: -moz-center` instead of `text-align: center`, or when the `div` used to be `center`. What's the point of `0`? Why not just `auto`? – cnst Feb 17 '13 at 04:43
  • 2
    `auto` is fine. `0 auto` sets the top/bottom to `0` and left/right to `auto`. – Jordan Ryan Moore Feb 26 '13 at 23:49
  • This only works when your `div` has a defined and previously known width. – fiatjaf Sep 01 '14 at 22:53
  • Note that `0 auto` also sets the margin for top & bottom to 0 - which has nothing to do with the current problem. A better solution is to set `margin-left: auto; margin-right:auto` which is a bit longer but more correct. – Marco Prins Sep 22 '14 at 10:41
  • Everyone talking about structure and presentation. But, why

    is not deprecated? that tag has the same purpose about presentation in HTML.

    – Ele Jan 08 '15 at 18:01
  • 1
    @EleazarEnrique:

    denotes a paragraph of text, which is what it *is*, not how it is presented. Just because browsers make

    a block element with top and bottom margins by default doesn't make it presentational in nature.

    – Jordan Ryan Moore Jan 08 '15 at 18:38
  • 1
    @JordanRyanMoore you said: "not how it is presented".. Browsers automatically add some space (margin) before and after each

    element. The margins can be modified with CSS (with the margin properties). source: [W3Schools](http://www.w3schools.com/TAgs/tag_p.asp)

    – Ele Jan 08 '15 at 23:09
  • @EleazarEnrique - That was brave of you to cite W3Schools here! The best way to deal with browsers doing WTF ever they feel like doing with

    tags etc. is to use a reset.css like [Eric Meyer](http://meyerweb.com/eric/tools/css/reset/) wrote

    – BillyNair Mar 19 '15 at 22:20
  • 1
    What about `
    ` and `` ? They define the layout and don't describe it's contents.
    – Chris Smith Jan 15 '17 at 02:39
  • @ChrisSmith, they are still "semantatic" elements, in that they define (topological) *structures* in the page, not looks (unlike CENTER). – Sz. Feb 06 '17 at 14:24
17

According to W3Schools.com,

The center element was deprecated in HTML 4.01, and is not supported in XHTML 1.0 Strict DTD.

The HTML 4.01 spec gives this reason for deprecating the tag:

The CENTER element is exactly equivalent to specifying the DIV element with the align attribute set to "center".

ComFreek
  • 27,416
  • 16
  • 97
  • 150
Conrad Meyer
  • 2,763
  • 18
  • 23
  • 17
    I *so* wish this was actually true. – badp Feb 12 '10 at 01:11
  • 64
    This is actually true, but DIV's align atribute is also deprecated :D – Oleh Prypin Jul 26 '10 at 23:40
  • Note that it's not equivalent to `text-align: center`, though. In Gecko, the `center` element is equivalent to a `div` element with the `style` attribute of `text-align: -moz-center;`. Heh, so much for interoperability! – cnst Feb 17 '13 at 04:51
  • 23
    Note that w3schools is *not* W3C. – showdev Sep 05 '13 at 00:27
  • 4
    "The CENTER element is exactly equivalent to specifying the DIV element with the align attribute set to "center"." except it is clear by looking at the tag that it is centered. If this was a valid reason, we could depricate , , all and other tags and say they do the same thing as with some css. besides, XHTML doesn't even recognize HTML5 tags such as canvas. It is only deprecated because of the war against marquees, centers, tables, frame which is purely religious, as they all have a place. – Dmitry Jun 05 '16 at 18:23
  • 2
    @Dmitry and 's have semantic meaning that goes beyond visual styling. I guess if
    was still around you could style it with CSS, eg to make its content left justified.
    – Nick Rice Dec 08 '16 at 16:41
  • 1
    @NickRice, good point. I'd been certainly in a pro-CENTER mood today until I saw your comment. :) I'm still sure something between that and the dreadful alternatives is badly missing from the spec. Why can't there be a `horizontal-align: center` attribute, for instance? – Sz. Feb 06 '17 at 14:39
16

What I do is take common tasks like centering or floating and make CSS classes out of them. When I do that I can use them throughout any of the pages. I can also call as many as I want on the same element.

.text_center {text-align: center;}
.center {margin: auto 0px;}
.float_left {float: left;}

Now I can use them in my HTML code to perform simple tasks.

<p class="text_center">Some Text</p>
Scott Radcliff
  • 1,461
  • 1
  • 9
  • 13
14

I still use the <center> tag sometimes because nothing in CSS works as well. Examples of trying to use a <div> trick and failing:

<div style="text-align: center;">This div is centered, but it's a simple example.</div>
<br />
<div style="text-align: center;"><table border="1"><tr><td>&lt;div style="text-align: center;"&gt; didn't center correctly.</td></tr></table></div>
<br />
<div style="text-align: center;margin-left:auto;margin-right:auto"><table border="1"><tr><td>&lt;div style="text-align: center;margin-left:auto;margin-right:auto"&gt; still didn't center either</td></tr></table></div>
<br />
<center><table border="1"><tr><td>Actually Centered with &lt;center&gt; tag</td></tr></table></center>

<center> gets results. To use CSS instead, you sometimes have to put CSS in several places and mess with it to get it to center right. To answer your question, CSS has become a religion with believers and followers who shunned <center> <b> <i> <u> as blasphemy, unholy, and much too simple for the sake of their own job security. And if they try to take your <table> away from you, ask them what the CSS equivalent of the colspan or rowspan attribute is.

It is not the abstract or bookish truth, but the lived truth that counts.
-- Zen

Russell Hankins
  • 1,018
  • 9
  • 16
11

You can still use this with XHTML 1.0 Transitional and HTML 4.01 Transitional if you like. The only other way (best way, in my opinion) is with margins:

<div style="width:200px;margin:auto;">
  <p>Hello World</p>
</div>

Your HTML should define the element, not govern its presentation.

Sampson
  • 251,934
  • 70
  • 517
  • 549
8

HTML is intended for structuring data, not controlling layout. CSS is intended to control layout. You'll also find that many designers frown on using <table> for layouts for this very same reason.

jkndrkn
  • 3,860
  • 4
  • 32
  • 40
8

It's intended that markup, i.e. the HTML tags, represent meaning and structure, not appearance. It was badly mixed up in early versions of HTML but the standards people are trying to clean that up now.

One problem with letting tags control appearance is that your pages don't play well with devices for the handicapped, such as screen readers. It also leads to having lots and lots of tags in your text that don't help clarify the meaning, but rather clutter it with information of a different level.

So CSS was thought up to move formatting/display to a different language, which is separate from the text and can easily be kept that way. Among other things, this allows switching stylesheets to change the appearance of a Web page without touching the other markup. And to be able to do that for lots of pages in one swell foop.

The tools CSS gives you to do this are not always elegant, I'm on your side there. For instance, there is no way to do effective vertical centering. And horizontal centering, if it's not just text amenable to text-align, is not much better.

You have the choice of doing easy, effective and muddled or clean, elegant and cumbersome. I don't understand why Web developers put up with this mess, but I guess they're happy to have at least a chance to get their stuff done.

Carl Smotricz
  • 62,722
  • 17
  • 119
  • 161
  • 5
    +1: As someone who's not even a web developer, I recognize how cumbersome it can be to specify certain presentational attributes using CSS. I still find it better to use CSS than muddling the structural markup, but... WTF? Who's in charge of defining that CSS spec, random monkeys they found in some remote jungle? Seriously. – Dan Moulding Nov 25 '09 at 19:48
  • 3
    Please, don't insult random monkeys in remote jungles! – DaveWalley Aug 15 '14 at 16:03
7

The Least Popular Answer

  1. A deprecated tag is not necessarily a bad tag;
  2. There are tags that deal with presentation logic, center is one of them;
  3. A center tag is not the same as a div with text-align:center;
  4. The fact that you have another way to do something doesn't make it invalid.

Let me explain because there are notorious downvoters here who will think I'm defending oldschool HTML4 or something. No I'm not. But the debate around center is simply a trend war, there is no real reason to ditch a tag that serves a valid purpose well.

So let's see the major arguments against it.

  • "It describes presentation, not semantics!"
    No. It describes a logical arrangement - and yes, it has a default appearance, just as other tags like <p> or <ul> do. But the point is the enclosed part's relation to its surroundings. Center says "this is something we separate by visually different positioning".

  • "It's not valid"
    Yes it is. It's just deprecated, as in, could be removed later. For 15+ years now. And it's not going anywhere, apparently. There are major sites (including google.com) that use this tag because it's very readable and to the point - and those are the same reasons we like HTML5 tags for.

  • "It's not supported in HTML5"
    It's one of the most widely supported tags, actually. MDN says "its use is discouraged since it could be removed at any time" - good point, but that day may never come, to quote a classic. Center was already deprecated in like 2004 or so - it's still here and still useful.

  • "It's oldschool"
    Shoelaces are oldschool too. New methods don't invalidate the old. You want to feel progressive and modern: fine. But don't make it the law.

  • "It's stupid / awkward / lame / tells a story about you"
    None of these. It's like a hammer: one tool for a specific job. There are other tools for the same job and other jobs for the same tool; but it was created to solve a certain problem and that problem is still around so we might as well just use a dedicated solution.

  • "You shouldn't do this, because, CSS"
    Centering can absolutely be achieved by CSS but that's just one way, not the only one, let alone the only appropriate one. Anything that's supported, working and readable should be ok to use. Also, the same argument happened before flexboxes and CSS grids, which is funny because back then there was no CSS way to achieve what center did. No, text-align:center is not the same. No, margin:auto is not the same. Anyone who argued against center tags before flexbox simply didn't know enough about CSS.

TL;DR:

The only reason not to use <center> is because people will hate you.

dkellner
  • 5,301
  • 1
  • 31
  • 36
6

For text and images you can use text-align:

<div style="text-align: center;">
    I'm centered.
</div>
Tomas Markauskas
  • 10,866
  • 2
  • 31
  • 35
3

CSS has a text-align: center property, and since this is purely a visual thing, not semantic, it should be relegated to CSS, not HTML.

keithjgrant
  • 11,328
  • 5
  • 46
  • 83
  • 1
    +1 for explaining why it was deprecated in addition to an alternative. – moribvndvs Nov 25 '09 at 18:09
  • 10
    but `text-align` will align the content of the container, not the container itself – Andreas Grech Nov 25 '09 at 18:12
  • Ah, yeah, if it's a block-level element, "auto" padding will do it. If it's an inline element, use `text-align: center` on its parent element. – keithjgrant Nov 25 '09 at 18:25
  • 1
    (oops, I meant auto margin, not padding) – keithjgrant Nov 25 '09 at 18:27
  • @AndreasGrech — `
    ` does *both*. It's annoyingly broad.
    – Quentin Aug 18 '16 at 09:38
  • Actually, center does something that can only be expressed with a longer CSS. This, in itself, should be a reason to let the poor thing live. It's a simple form of centering the container and the contents - and there's CSS for anything more specific. Still can't see the point of being against it. – dkellner Oct 24 '20 at 21:50
3

Food for thought: what would a text-to-speech synthesizer do with <center>?

Jörg W Mittag
  • 337,159
  • 71
  • 413
  • 614
  • 1
    Probably speak "center" or "centered". – DaveWalley Aug 15 '14 at 16:01
  • The same as it would do with css' text-align: center; I suppose. – MSpreij Apr 06 '16 at 11:52
  • Yeah, nah, what would it do with "display:inline-block; color:#772fe3; background-repeat:no-repeat; border:17px dotted beige; transform:rotate(3deg)"? I would totally be confused as a screen reader. I think I'd just go with the text. Duh. – dkellner Dec 05 '20 at 09:07
  • ...and okay, maybe you're saying "markup should always have a speech representation"; but no, absolutely not, see "div" or "span" for example. HTML is for documents, SSML is for speech. – dkellner Dec 05 '20 at 09:08
2

You can add this to your css and use <div class="center"></div>

.center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}

or if you want to keep <center></center> and be prepared in case its ever removed, add this to your css

center{
  text-align: center;
  margin: auto;
  justify-content: center;
  display: flex;
}