XHTML DOCTYPE Validation: tag <center> </center>

by Aggiorno Team 1. February 2008 03:47

It has been a frequent practice (there will be a future post with statistics) to use the HTML tag <center> to center-align its contents.  However, the <center> tag has been deprecated by the W3C because of its lack of flexibility and inconsistent treatment by different browsers.

If you want XHTML validation and compliance (and you already know XHTML validation is important because you read the post DOCTYPE Validation in XHTML/ASP.NET web pages how should you convert the tag to a supported syntax without affecting the rendering of the page?

There are 2 scenarios depending on the contents that are being centered.

The first scenario is when the centered elements are simply text elements and inline elements (eg.: img, span, em, strong, i, b, ...).  In this case the <center> tag needs to be replaced by a <div> tag with the attribute style="text-align:center".

For example if you have the following code:

... <center> This text is centered! </center>  ...

should be converted to:

... <div style="text-align:center"> This text is centered! </div> ...

The second scenario is when the centered elements are block elements (eg.: address, blockquote, div, fieldset, headings, hr, lists, p, pre, table, ...).  In this case if there is a combination of inline elements with block elements, the <center> tag also needs to be replaced by a div tag.  Additionally it is necessary to add the following CSS attribute to each nested block element: margin-left:auto;margin-righ:auto. 

Let's see an example:

...
<center>
<span>This text is centered! </span>
<table>
<tr>
<td>This text is also centered!</td>
</tr>
</table>
</center>
...

should be converted to:

...
<div style="text-align:center">
<span>This text is centered! </span>
<table style="margin-left:auto;margin-right:auto">
<tr>
<td>This text is also centered!</td>
</tr>
</table>
</div>
...  

The convertion is simple but tedious specially when there is a combination of centered elements.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



About Aggiorno

Aggiorno RSS Feedsaggiorno is a unique knowledge-encapsulation platform that can make any website a valid, findable, accessible, standards compliant one. Read on

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

RecentComments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign out

ArtinSoft Corporation ArtinSoft is Microsoft Certified Partner ISV/Software Solutions and Microsft Visual Studio Partner

With over fifteen years of experience, ArtinSoft has proven to be a key player in software evolution, by allowing customers from all over the world to ensure business continuity and compliance through software migration solutions and developer tools created upon principles of artificial intelligence. At present time, ArtinSoft Corporation remains a private firm in constant growth through a strategic partner network. Read More...