Html – Left, center, and right text

csshtml

What's the best way to obtain three texts on the same line: one to the left, another in the middle, and the third one to the right?

+---------------------------------------------------------------------+
|Page generated in 1 ms           Copyright 2010              Email me|
+---------------------------------------------------------------------+

One text may be longer than the others, but their position must not change. The one in the middle must always be in the middle, regardless of the length of the others.

Doing it with tables would be easy, and I already know how to do that, so "correct" solutions only (using tables for layout is wrong).


EDIT: many people assumed for some reason that I needed to display tabular data over several rows. I'm not sure why they thought it was the case, but:

  • It will have only one row
  • It's not tabular data

For this reason a table is inappropriate, and the solution only needs to work with one line.

Best Answer

Taking your question literally, here's how I'd do it.

<div style="position:absolute;text-align:left">Text 1, a bit longer</div>
<div style="position:absolute;width:100%;text-align:center">Text 2</div>
<div style="text-align:right">Text 3</div>

Whether this will work for you in practice, depends on what the texts actually are. Note that if the divs' container is too narrow, the texts will overlap with one another, and not wrap like they would if they were in table cells.

If you want table-like layout behaviour, you have two choices. You can use tables, or you can use the CSS display:table, display:table-cell etc properties. Sadly, the latter is not supported in IE6 or IE7, so probably isn't really usable on the web just yet.