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

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

You cannot set text-align on a column element (Well, you can, but it won't have any effect) There are only a couple of properties that have an effect, namely border, background, width, and visibi...

posted 3y ago by Moshi‭

Answer
#1: Initial revision by user avatar Moshi‭ · 2021-05-10T23:42:17Z (almost 3 years ago)
## You cannot set `text-align` on a column element

(Well, you can, but it won't have any effect)

There are only a couple of properties that have an effect, namely `border`, `background`, `width`, and `visibility`.

-----

If you need to style a column outside of those attributes, [MDN notes some possible workarounds](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/col#deprecated_attributes).

> - To achieve the same effect as the `left`, `center`, `right` or `justify` values:
>   - Do not try to set the text-align property on a selector giving a `<col>` element. Because `<td>` elements are not descendant of the `<col>` element, they won't inherit it.
>   - If the table doesn't use a `colspan` attribute, use the `td:nth-child(an+b)` CSS selector. Set `a` to zero and `b` to the position of the column in the table, e.g. `td:nth-child(2) { text-align: right; }` to right-align the second column.
>   - If the table does use a `colspan` attribute, the effect can be achieved by combining adequate CSS attribute selectors like `[colspan=n]`, though this is not trivial.

Eg.

```css
table {
  width: 20em;
  table-layout: fixed;
  border-collapse: collapse;
  text-align: center;
}

td {
  border: black solid 0.1em;
}

td:nth-child(2) {
  text-align: left;
}
```

[jsfiddle](https://jsfiddle.net/od1zh27x/)

If you have greater control over the HTML generation, however, it is probably better to just add a class to the cells in question and style those.

[^1]: For whatever reason, MDN lists `width` as depreciated