Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/CSS table right align text not working in IE

Again, Internet Explorer is not getting easy on me. I have a table with two cells in the same row. I want to display some text aligned to the left (first cell), and another one aligned to the right (second cell). This is working fully in Chrome and Firefox, but in IE all the text appears left-aligned:

<table width="660px">
<tr>
    <td align="left" width="160px">Text 1</td>
    <td align="right" width="160px">Text 2</td>
</tr>

​​​​​​​​​​​​​

After some research I wondered if I should put it on the CSS, so I changed it to:

HTML:

<table class="anchors" width="660px">
<tr>
    <td class="left" width="160px">Text 1</td>
    <td class="right" width="160px">Text 2</td>
</tr>

CSS:

table.anchors td.left
{
    text-align: left;
}

table.anchors td.right
{
      text-align: right;
}

It still doesn't work in IE (version 9, at least). Does anybody has a hint on this? Should I be using something else (a div, e. g.)?

like image 494
João Fernandes Avatar asked Aug 01 '12 15:08

João Fernandes


People also ask

How do I right-align text in a table in HTML?

HTML | <td> align Attribute left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justify: It stretches the text of paragraph to set the width of all lines equal.

How do you right-align a cell in a table?

To change the alignment, simply click inside that cell and click Align Right in the Paragraph group on the Home tab.

How do I align text in a table?

Select the text and go to the Layout tab and the Alignment section of the ribbon. Choose “Align Center.” Your text will then be right in the middle of the cell. Centering the text in your Word table is a simple task whether horizontally, vertically, or both.


1 Answers

The code is syntactically malformed (width attributes take numeric or percentage values, not with px units), though this pardoned by browsers. More seriously, you are setting conflicting requirements: the table should be 660 pixels wide but consist of two 160 pixels wide cells. It is not surprising that browser behavior is inconsistent.

However, IE 8 and IE 9 behave like other browsers when in “Standards Mode”. Otherwise, in Quirks Mode, anything may happen, and you cannot call it a bug, because the document is non-conforming. So add an adequate doctype declaration.

In addition, it is best to avoid conflicting requirements. If you need to set a total width on the table in pixels, so be it. Then either set column widths so that they add up or, simpler, set e.g. the widths to 50% (for a two-column table that should be balanced).

like image 95
Jukka K. Korpela Avatar answered Sep 20 '22 04:09

Jukka K. Korpela