35

How can I center the text in the headers for an AG-grid control? I have tried using cellstyle and cellclass in the column definition but that did not work. Thank you

Alex Wachira
  • 1,225
  • 16
  • 19
user7400346
  • 799
  • 1
  • 8
  • 15

14 Answers14

41

I'm using React and I just added the following snippet to my Table component's .css

.ag-header-cell-label {
  justify-content: center;
}

I'm overriding the .css class class from ag-grid which I found via devtools inspection, and discovered it's using flexbox for display.

See example (not react but same concept): https://embed.plnkr.co/O3NI4WgHxkFiJCyacn0r/

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Alex Wachira
  • 1,225
  • 16
  • 19
19

You may use this property:

cellStyle: {textAlign: 'center'}
Maharramoff
  • 941
  • 8
  • 15
7

A more complete solution I've been using for a while now, is to create a column definition helper like this:

export const columnCentered = {
  headerClass: 'text-center',
  cellStyle: {
    textAlign: 'center',
    // Add the following if you are using .ag-header-cell-menu-button 
    // and column borders are set to none.
    // marginLeft: '-16px'
  }
}

then add the following to your style.scss

.ag-header-cell.text-center {
  .ag-header-cell-label {
    justify-content: center;
  }
}

finally you can easily use it like this:

columnDefs: [
  { field: 'normalCol' },
  { field: 'centeredColA' ...columnCentered },
  { field: 'centeredColB' ...columnCentered }
]
Stefanos Chrs
  • 2,228
  • 3
  • 19
  • 46
5

Assign a class to the cell as below:

gridOptions = {
  ...
  columnDefs: [
    ...
        { headerName: "field1", field: "field1", cellClass: "grid-cell-centered"}
    ...
  ]
}

css file:

.grid-cell-centered {
  text-align: center;
}
ZooZ
  • 933
  • 1
  • 17
  • 25
5

If you want to align your text right, left or center in ag-grid. Then use this:-

cellStyle: { textAlign: 'right' }
cellStyle: { textAlign: 'left' } 
cellStyle: { textAlign: 'center' }
MEDZ
  • 2,227
  • 2
  • 14
  • 18
akash mishra
  • 113
  • 1
  • 1
5

To use a custom class, add headerClass: "text-center" to the column definition and css as follows:

text-center * {
    justifyContent: center
}
James
  • 1,720
  • 5
  • 29
  • 50
2

For (independent) customization of each header, you can make a custom header component: https://www.ag-grid.com/javascript-grid-header-rendering/

Alex P.
  • 1,140
  • 13
  • 27
2

I'm using Angular 8 and AG-Grid is nested deep in some other component.

I had to do this in order to make it work.

.ag-header-group-cell-label {
      justify-content: center !important;
}

And, this strictly needs to be under styles.scss file and not in the component scss file. Or else, it will not work.

Hope this helps someone.

Pramesh Bajracharya
  • 2,153
  • 3
  • 28
  • 54
  • 1
    in component scss file: :host ::ng-deep .ag-header-cell-label { justify-content: center !important; } – Slava Oct 30 '20 at 17:01
  • 1
    ::ng-deep is being deprecated, just set component's ViewEncapsulation.None and your style works. – tsvedas Apr 28 '21 at 10:12
2

My CSS:

/*HEADER STYLE*/
        .ag-theme-alpine .ag-header-cell {
            padding-left: 10px;
            padding-right: 10px;
        }
        
        .ag-right-header .ag-header-cell-label {
             flex-direction: row-reverse;
        }
    
        .ag-center-header .ag-header-cell-label {
            justify-content: center;
        }

        .ag-left-header .ag-header-cell-label {
            flex-direction: row;
        }

        .ag-center-cell {
            text-align: center;
        }

        .ag-right-cell {
            text-align: right;
        }

        .ag-left-cell {
            text-align: left;
        }

And use inside columnDefs:

{
                headerName: 'header_name',
                field: 'header_field',
                headerClass: "ag-center-header",
                cellClass: "ag-center-cell"
            }
O Thạnh Ldt
  • 1,103
  • 10
  • 11
1

It should be:

.ag-header-cell-label {
  text-align: center;
}
1

you can do this by overriding the css like:-

    ::ng-deep .ag-header-group-cell-label {
      justify-content: center !important;
    }
    
    ::ng-deep .ag-header-cell-label {
      justify-content: center !important;
    }

Thanks

Dave
  • 3,073
  • 7
  • 20
  • 33
Akash
  • 11
  • 1
0

I'm using angular and I just want to center text in the grouped header so use this code to style.css

.ag-header-group-cell-label {
  justify-content: center;
}
0

columndef has a property. headerClass: '', you can add css to header

zodiake
  • 1
  • 1
0
:host ::ng-deep .my-center-header-class .ag-header-cell-text {
    width: 100%;
    text-align: center;
}

Set headerClass as 'my-center-header-class' in colDef