In my quest to clean up my HTML act from older applications, I'm trying to move all my HTML in several applications over to XHTML and as much as possible eliminate all the various VS.NET warnings for legacy HTML constructs. For the most part this has been pretty easily done by removing most attributes and sticking them into CSS properties and either using style sheet classes right off or using style tags on controls.
There are a however a few things that I'm having a hard time with. The most common problem I run into has to do with centering content. Specifically centering content like tables and <div> tags. In the past I often used the <center> tag which is now obsolete.
It still works in browsers, but officially it's deprecated. XHTML also renders anything that is centered in the parent container very differently than HTML does – it by default inherits the parent container's alignment so if you do something like this:
<center>
<div style="width:300px;background:green;height:300px; ">
here's some text
</div>
</center>
You'll get the text inside of the <div> also centered. That's easy to fix by adding an explicit text-align:left style to the <div> tag, but in conversion this can really bite you.
Back to the <center> replacement.
It looks like there's a float style attribute that can be applied to a container such as a div tag to make it go right or left.
<div style="width:300px;background:green;height:300px;float:right;">
here's some text
</div>
That works great actually, but it doesn't have a value for center.
If you're not dealing with a DIV tag using <p style="text-align:center"><div>..</div></p> works. But a <div> tag is not allowed inside of a <p> tag.
Ok, so I can see a way to do this like this:
<div style="width:100%;text-align:center">
<div style="width:300px;background:green;height:300px;float:middle;">
here's some text
</div>
</div>
which seems like overkill for such a simple thing.
Anybody know of a better way?
FWIW, here are a couple of others that I have in many places and weren't real obvious:
TD nowrap="nowrap"
CSS style: white-space: nowrap
IMG align="right"
CSS style: float: right;
The IMG align has the same limitation as above - you can't center using the float CSS tag.
Other Posts you might also like