Question: Can align text of specific column?
See original GitHub issueBody Cell Alignment
I would like to align specific columns to the center. Today I can only align all columns or specific header.
I was able to select specific header, but can’t do the same for the body cells.

Code used:
MUIDataTableHeadCell: {
root: {
border: 'solid 1px #00FF',
'&:nth-child(5)': {
backgroundColor: 'red',
}
}
}
What I tried and how I would like to do that:
MUIDataTableBodyCell: {
root: {
border: 'solid 1px #000',
'&:nth-child(5)': {
backgroundColor: 'red',
textAlign: 'center'
}
}
}
Please advise if there’s a way to do that today. Also advise if I can help somehow.
Your Environment
| Tech | Version |
|---|---|
| Material-UI | 3.5.1 |
| MUI-datatables | 2.0.0-beta-52 |
| React | 16.5.2 |
| browser | chrome 71.0.3578.98 |
| etc |
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:18
Top Results From Across the Web
Alignment of text in specific column of table - Stack Overflow
I'm trying to change the alignment of specific columns of text to the right. Tried using class and it does not work.
Read more >text-align - CSS: Cascading Style Sheets - MDN Web Docs
The text-align CSS property sets the horizontal alignment of the inline-level content inside a block element or table-cell box.
Read more >Align text in a cell - Microsoft Support
Select the cells that have the text you want aligned. · On the Home tab choose one of the following alignment options: ·...
Read more >Aligning text in S columns in a way that respects the decimal ...
Is it possible to have the numerical entries aligned by their decimal, while controlling the text entries' alignment as it relates to the ......
Read more >Different text alignment in Header, and depending on Column
Struggling to align text the way I need to, and can't find a previous thread that resolves this issue specifically. I need my...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Wow, okay, so this was a long journey just to get a specific column’s header centered.
Now first, there are two solutions here that won’t work quite well. @chrishawn proposed this:
setCellHeaderProps: () => ({ style: {display: 'flex', justifyContent: 'right', flexDirection: 'row-reverse', borderBottom: 'none'}})This works, but only to a degree. Tried changing resolution so you need to scroll? The headers that have this (as he admitted) dirty hack don’t stick because they need
display: table-cell, which is overwritten by thedisplay: flex. But without this, it won’t center. So no solution here.@mdrafee03 proposed this:
This also works, but only for all cell headers. Mostly, of course, you want to change the alignment of a specific cell header. The first solution does this, but imperfectly. This one doesn’t. But we can combine both solutions and get where we want.
MUIDataTableHeadCell.toolButtonis thespanthat is a child of thethwhich is modified insetCellHeaderProps. So we need to somehow get to this childspanfromsetCellHeaderProps.The solution: First, we add a new JSS class for the cell header we want to have centered (or aligned in any other way than left). In this JSS class, we will implement a child selector that targets the
spanelement.We will then pass the this class into our MUIDatatable for creating the columns. However you do this, is up to you. you can do this via props like in the Example from here or if you use mainly functional components (like me) you can use the Material UI way of
const classes = useStyles()and pass those in a function that creates the column.Now we just need to add this class to the specific cell header using
setCellHeaderProps. To do so, we can simply use clsx which already ships with MaterialUI. This can look something like this (short version):And that’s it. Simply add the class via cslx in the
setCellHeaderPropsfor every table header that should be centered and it works.However, I think it’s far to complicated for such a simple task. There should be a simple option to this somewhere else. Would you be open to an issue request adding such an option, @patorjk ?
Using
customHeadRenderseems to mean I have to re-do the whole cell component myself in order to keep the layout the same as the rest. This looks like a lot of unneeded work when I only want to add one prop to the underlying Material-UITableCelljust as with regular columns (cetCellProps).