Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

83%
+8 −0
Q&A How to set text-align for whole column of HTML table?

To start off, here is the complete HTML of a simple example table: <html lang="en-US"> <head> <title>Title</title> <style> table { width: 20em; ...

2 answers  ·  posted 3y ago by Olin Lathrop‭  ·  edited 3y ago by Alexei‭

#3: Post edited by user avatar Alexei‭ · 2021-06-09T04:31:03Z (over 3 years ago)
switched tag to have less redundancy
#2: Post edited by user avatar Alexei‭ · 2021-06-08T07:49:07Z (over 3 years ago)
added relevant tag
#1: Initial revision by user avatar Olin Lathrop‭ · 2021-05-10T22:59:37Z (over 3 years ago)
How to set text-align for whole column of HTML table?
To start off, here is the complete HTML of a simple example table:

<pre>
&lt;html lang="en-US"&gt;
&lt;head&gt;
  &lt;title&gt;Title&lt;/title&gt;
  &lt;style&gt;
    table {
      width: 20em;
      table-layout: fixed;
      border-collapse: collapse;
      text-align: center;
      }
    td {
      border: black solid 0.1em;
      }
    &lt;/style&gt;
  &lt;/head&gt;
&lt;body&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;A
      &lt;th&gt;B
      &lt;th&gt;C
    &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;aardvark
      &lt;td&gt;bale
      &lt;td&gt;crab
    &lt;tr&gt;
      &lt;td&gt;apple
      &lt;td&gt;blade
      &lt;td&gt;crib
    &lt;/tbody&gt;
  &lt;/table&gt;

&lt;/body&gt;&lt;/html&gt;

</pre>

This results in the following table, as expected:

<img src="https://software.codidact.com/uploads/SF1y8kyq3yv3b4Nae27HfHKn">

W3SCHOOLS says the following about the &lt;col&gt; tag: <blockquote>
The <col> tag is useful for applying styles to entire columns, instead of repeating the styles for each cell, for each row.</blockquote>

That makes sense.  As an example, I inserted the following HTML after TABLE and before THEAD:

<pre>
  &lt;colgroup&gt;
    &lt;col&gt;
    &lt;col style="background-color:red"&gt;
    &lt;col&gt;
    &lt;/colgroup&gt;
</pre>

That caused the background of the whole second column to be red, as expected:

<img src="https://software.codidact.com/uploads/gcCcJk5WZ76A5MjM7t8pYQa4">

However, when I try to set the text-align attribute for the same column as a whole

<pre>
    &lt;col style="text-align:left;background-color:red"&gt;
</pre>

it doesn't work.  I get the same display as above.  The second column is still red, so the COL tag isn't getting ignored.  To make sure there wasn't a typo, I copied and pasted the text alignment into the TD tag of one of the table entries:

<pre>
      &lt;td style="text-align:left"&gt;bale
</pre>

The text for that one cell did get left-aligned:

<img src="https://software.codidact.com/uploads/W4XwXtJwKjGjRRwD6TwwCzLV">

I tried various things, but couldn't get a different default alignment for a whole column without having to set it individually for each cell in that column.  That seems to be exactly what COLGROUP and COL are for.  It works for background color, but not text alignment.  What is going on?  How does one set default text alignment in one place for a whole column?

In case it matters, these tests were done with Edge on Windows 10.