views:

705

answers:

10

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?

+5  A: 

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.

Jonathan Sampson
+20  A: 

The <center> element was deprecated because it defines the presentation of its contents -- it doesn't describe its contents.

One method of centering is to set the margin property of the element to 0 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
Yes, CSS is all about spliting presentation and data.
Ivan Nevostruev
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
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 `<center>` did work... I'll try to find an example.
fudgey
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
The <center> cannot hold...
edosoft
the auto margin trick only works for block level elements. use `display: block;` on an inline element.
Steve Graham
+4  A: 

Hi, 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
A: 

For text and images you can use text-align:

<div style="text-align: center;">
    I'm centered.
</div>
Tomas Markauskas
+1  A: 

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
+1 for explaining why it was deprecated in addition to an alternative.
HackedByChinese
but `text-align` will align the content of the container, not the container itself
Andreas Grech
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
(oops, I meant auto margin, not padding)
keithjgrant
+4  A: 

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
+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
+7  A: 

According to the W3C,

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

Conrad Meyer
I *so* wish this was actually true.
badp
This is actually true, but DIV's align atribute is also deprecated :D
BlaXpirit
+5  A: 

HTML is all about providing structure and meaning to content, and this should be its sole function.

However, there are assorted tags that were added in the past by stupid browser developers, which violate this basic principle.

At some point, people came to their senses (partially, at least), and started (trying to) agree upon a specification for writing correct HTML, so they depreciated some of these tags.

However, the damage has already been done - too many people have learnt HTML as "put tags around stuff and stuff happens", rather than understanding the (important and useful) distinction between semantics and presentation, and those people have gone on to write books and tutorial, teaching other people the wrong way to write HTML, who themselves have done the same, and we've ended up with a right old mess.

Ideally, the center tag should never have existed, because people should have pointed out it's not 'real' HTML, and told the browser makers to go back and do it properly.


And for doing it properly... alignment is of course a presentation issue - applying layout and formatting to (structured) content - and the language to use for that is CSS.

However, yet again, stupid browser developers screwed up early CSS - and in many cases people creating web pages had to use incorrect HTML because the CSS simply didn't work.

These days, we're starting to get some useful functionality added to CSS, but there's still a lot that isn't implemented, and quite a few things that could have been implemented in far superior ways, but are now standardised so we're stuck with the old ways.

If stylesheets had been implemented by sensible people, you'd have all the controls you have in any decent office software - for aligning blocks and text, it'd work horizontally, vertically, upside-down, rotated, and so on - and it wouldn't be done in the stupid way of setting left/right margins to auto.


I'll add quickly, for completeness, the third 'level' for web pages, which deals with adding interactivity to an otherwise static page, which is of course the (mis-named) JavaScript.

Yet again, those stupid browser developers didn't know what they were doing. They picked an utterly inaccurate/invalid name (for marketing reasons), and again riddled a potentially great and powerful language with flaws, lack of functionality, and created something that was (until recently) hated by many people.

We have of course in the past few years seen a large uprising of JavaScript libraries which have helped to show some of the cool stuff JS has been capable of for years, hidden away many of the browser flaws which still exist in the browsers, and have partially helped to patch up CSS support so it's a bit more useful (still lots of things missing though).


So yeah, to summarise all that, the ultimate answer is because browser makers were stupid. :)

Peter Boughton
+1 because is funny and mostly true. If you were following the debate on HTML 5 you would probably have been less optimistic on the fact we are starting to come to senses... Does the Microsoft attitude towards "extensibility of HTML" tells you something? ;)
mac
Yeah I did put the "partially" in there because of that, but I've deliberately not delved into the details of it all to avoid getting annoyed at all the idiocy.
Peter Boughton
+1  A: 

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

Jörg W Mittag
A: 

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