Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper CSS way to implement right-aligned text on table columns?
    primarykey
    data
    text
    <p>To my surprise I just found out that applying text-alignment to a table column is fairly bad supported in current browsers. Neither Firefox 3.5.2, Safari 4.0.3 or IE8 shows the "amount" column below as right aligned.</p> <p>HTML:</p> <pre><code>&lt;table class="full_width"&gt; &lt;caption&gt;Listing employees of department X&lt;/caption&gt; &lt;col&gt;&lt;/col&gt; &lt;col&gt;&lt;/col&gt; &lt;col&gt;&lt;/col&gt; &lt;col class="amount" width="180"&gt;&lt;/col&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Phone number&lt;/th&gt; &lt;th&gt;Email&lt;/th&gt; &lt;th&gt;Salary&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;John Doe&lt;/td&gt; &lt;td&gt;+45 2373 6220&lt;/td&gt; &lt;td&gt;john@doe.com&lt;/td&gt; &lt;td&gt;20000&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>CSS</p> <pre><code>.amount{ text-align: right; } </code></pre> <p>Why isn't this working? Also I tried (via firebug) to turn off Firefox' native rule that left-aligns TD elements, but that didn't work either.</p> <p>I can see that setting background color rule in the amount css class actually works. So I know that the .amount class is applied to all columns:</p> <p>CSS</p> <pre><code>.amount{ text-align: right; background-color: aqua; } </code></pre> <p>The CSS 2 spec apparently says that only four attributes are supported by col element -- see <a href="https://stackoverflow.com/questions/1119106/why-is-styling-table-columns-not-allowed">Why is styling table columns not allowed?</a></p> <p>Criteria for selecting the best solution: must be supported fairly cross-browser (not necessarily in IE6 where I could live with using jquery or a conditional comment to include a specific solution). Also, I expect to apply multiple classes multiple different columns (ie. <code>class="amount before_tax"</code>) </p> <p>I'd hate to set classes on the relevant td in each row. What are my options?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload