How to change default font size for ag-grid row cells in react js?
Asked Answered
W

4

5

I am using ag-grid table for first time. I am not able to change the default font size of row cells(Not header portion). I want to change the font size of row cells(row data). I am using ag-theme-alpine.

Here is the code which I am using.

import React, { Component } from 'react';
import { render } from 'react-dom';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/dist/styles/ag-theme-alpine.css';

class App extends Component {
    constructor(props) {
        super(props);

        this.state = {
            columnDefs: [
                {headerName: 'Make', field: 'make'},
                {headerName: 'Model', field: 'model'},
                {headerName: 'Price', field: 'price'}

            ],
            rowData: [
                {make: 'Toyota', model: 'Celica', price: 35000},
                {make: 'Ford', model: 'Mondeo', price: 32000},
                {make: 'Porsche', model: 'Boxter', price: 72000}
            ]
        }
    }

    render() {
        return (
            <div
                className="ag-theme-alpine"
          style={{ height: '500px', width: '100%',fontSize:'20px' }}
            >
                <AgGridReact
                    columnDefs={this.state.columnDefs}
                    rowData={this.state.rowData}>
                </AgGridReact>
            </div>
        );
    }
}

render(<App />, document.getElementById('root'));

Please suggest a good solution.

Winnah answered 6/6, 2020 at 14:33 Comment(0)
C
4

You can override the font size via CSS. Ag-Grid's CSS sets the font at the row level, and unfortunately it has an !important tag. Providing a higher selector specificity should do the trick:

div.ag-theme-alpine div.ag-row {
    font-size: 12px !important;
}
Coumas answered 15/10, 2020 at 11:29 Comment(0)
H
11

You can do this in the ColDef by setting the cellStyle property in ColDef. You can set the font size as follows

columnDefs: [
                {
                  headerName: 'Make', 
                  field: 'make', 
                  cellStyle: {fontSize: '11px'}
                }
            ]
Hearse answered 25/10, 2021 at 15:48 Comment(0)
C
4

You can override the font size via CSS. Ag-Grid's CSS sets the font at the row level, and unfortunately it has an !important tag. Providing a higher selector specificity should do the trick:

div.ag-theme-alpine div.ag-row {
    font-size: 12px !important;
}
Coumas answered 15/10, 2020 at 11:29 Comment(0)
K
1

You can set font-size and other parameters globally by customizing a theme: React Data Grid: Customising Themes

Define the style parameters in the styles.scss file like this:

.ag-theme-alpine {
  @include ag-theme-alpine((        
      font-size: 10px,
  ));
}
Kragh answered 7/2, 2022 at 13:55 Comment(0)
I
0

ajaali's answer works as described. However, it must be applied to each individual column using that approach.

To apply the same font-size to all columns at once, you can use the same "cell-style" setting in the "defaultColDef" section of the gridOptions:

defaultColDef: {
    cellStyle: { fontSize: '11px'},
},
Intramolecular answered 10/7 at 18:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.