material-table icon indicating copy to clipboard operation
material-table copied to clipboard

Centering action button

Open FroggDev opened this issue 7 years ago • 18 comments

Hi,

The property actionsCellStyle apply to the action If i want to center the custom action button in the TD, I need to access to the properties of the extra

container added from the code.

BTW, i don't know what is the utility of this div.

When the div

is removed ( using the browser console for example) I can correctly align action button using actionsCellStyle:{justifyContent:'center'}

Thanks.

FroggDev avatar Jul 26 '19 15:07 FroggDev

It's hard to read what you wrote. You can center an action button like this:

options={{actionsCellStyle: {display:"flex", justifyContent: "center"}}}

pak11273 avatar Aug 03 '19 15:08 pak11273

2019-08-06 11_23_02-Window

Thanks, but as you can see the bottom line isn't aligned with your code

I can pass through using actionsCellStyle:{display:"flex", justifyContent: "center",marginBottom:' -1px'},

But that's look tricky ...

FroggDev avatar Aug 06 '19 09:08 FroggDev

Just go down to read the solution...

  • Read the post of the 10 Apr
  • You can also then check JeffEmery commit

FroggDev avatar Sep 26 '19 08:09 FroggDev

that is actually what i did. but no, it does not work

actionsCellStyle: {
            justifyContent: 'center'
          },

Even adding display: 'flex' or textAlign: 'center'

Please see image. the css instead gets applied to the cell but it does not center it because the buttons have a

<div style="display: flex">

container. cellstyle

chitgoks avatar Feb 14 '20 05:02 chitgoks

The solutions above don't work, this issue should be reopened

trianta2 avatar Mar 24 '20 21:03 trianta2

You can try something tricky like

actionsCellStyle:{justifyContent: "center",maxWidth: 90,width: 90}

FroggDev avatar Mar 25 '20 18:03 FroggDev

In waiting for the official fix, set it like this:

actionsCellStyle: {
  display: 'flex',
  justifyContent: 'center',
  padding: 16,
  width: '100%'
}

It seems to be lack of 1px on bottom, but better than nothing. If you find a better one please share it. 😉

ghost avatar Mar 30 '20 14:03 ghost

Thanks @lucacattide, works fine for me.

arbaz1006 avatar Apr 08 '20 15:04 arbaz1006

I had to add a bit more padding and push the bottom margin...

actionsCellStyle: {
    display: 'flex',
    justifyContent: 'center',
    padding: '24px',
    width: '100%',
    marginBottom: '-1px',
},

JeffEmery avatar Apr 09 '20 20:04 JeffEmery

seems we all have to wait for this fix.

chitgoks avatar Apr 10 '20 01:04 chitgoks

As i said in the first post, the problem come from the DIV which contains the buttons. When actionsCellStyle is set, it is applied to the TD, so it does not change the DIV behavior.

If you want to CENTER the BUTTONS you have to apply a style to the DIV which contains the BUTTONS instead of the TD

You can do it in multiple ways.

Here an exemple using external style

for exemple:

.MuiTable-root tr td:last-child div {
  justify-content: center;
}

Result:

centered

You can see a working demo at: https://codesandbox.io/s/pedantic-villani-xl7nf?file=/src/styles.css:59-126

Anyway, there should be an option for that in material-table, and for now it is missing.

But if you want to contribute you can try to do a PR with an option that apply "justify-content: center;" to the action DIV container.

Or maybe there is another way and it need to be found.

FroggDev avatar Apr 10 '20 13:04 FroggDev

@JeffEmery Thanks.

FroggDev avatar Apr 22 '20 18:04 FroggDev

finally i got it:

actionsCellStyle:{justifyContent: "center"},

I tried the justifyContent prop. but it is not working, I don't know why, I used paddingLeft instead to move the icon to the center of the cell.

actionsCellStyle: {
    paddingLeft: '2rem',
}

royliu007 avatar May 29 '20 08:05 royliu007

finally i got it: actionsCellStyle:{justifyContent: "center"},

I tried the justifyContent prop. but it is not working, I don't know why, I used paddingLeft instead to move the icon to the center of the cell.

actionsCellStyle: {
    paddingLeft: '2rem',
}

Read the post of the 10 Apr then check JeffEmery commit

FroggDev avatar May 29 '20 08:05 FroggDev

@FroggDev .MuiTable-root tr td:last-child div { justify-content: center; } thanks,it works for me but at present, the acticon title is centered by default, how to change it to left, I tried the headerStyle, but it does not work.

zhushenhang avatar Jul 17 '20 08:07 zhushenhang

[...] but at present, the acticon title is centered by default, how to change it to left, I tried the headerStyle, but it does not work.

Try:

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    root: {
      '& .MuiTable-root tr th:last-child': {
        textAlign: 'right !important',
        paddingRight: theme.spacing(3),
      }
    },
  }),
);

sebaranowski avatar Jul 24 '20 12:07 sebaranowski

actionsCellStyle: { margin: "0 auto", display: "flex", flexGrow: "1", justifyContent: "center", // border: "3px solid black", }, in options worked for me

abhi-dasgupta avatar Oct 14 '20 05:10 abhi-dasgupta

actionsCellStyle: { margin: "0 auto", display: "flex", flexGrow: "1", justifyContent: "center", // border: "3px solid black", }, in options worked for me

It has been fixed there: https://github.com/ChrisHampu/material-table/commit/0177d77a7bd2cd9902760a0c8760500f620edb3a

FroggDev avatar Aug 06 '21 17:08 FroggDev