'Scroll Material UI table body vertically

I am using Material UI table for my React.js application. I want to scroll its body vertically if table is too long. Here is the table.

<Table style={TableBorder}>
                            <colgroup>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '30%'}}/>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '10%'}}/>
                                <col style={{width: '10%'}}/>
                            </colgroup>
                            <TableHead>
                                <TableRow>
                                    <TableCell align="center"
                                               style={styles.table.tableHead.tableCell}>PRODUCT</TableCell>
                                    <TableCell align="center"
                                               style={styles.table.tableHead.tableCell}>BUILDS</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>INSTRUCTIONS(%)</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>BRANCHES(%)</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>COMPLEXITY(%)</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>LINES(%)</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>METHODS(%)</TableCell>
                                    <TableCell align="right"
                                               style={styles.table.tableHead.tableCell}>CLASSES(%)</TableCell>
                                </TableRow>
                            </TableHead>
                            <TableBody>
                                {this.state.tableSummary.map((row, index) => (
                                    <TableRow key={index}
                                              style={((row[1] > 0) ? styles.table.tableBody.tableCell.cursorPointer : styles.table.tableBody.tableCell.cursorText)}
                                              hover>
                                        <TableCell component="th" scope="row"
                                                   align="left">{row.name}</TableCell>
                                        <TableCell style={builds}
                                                   align="left">{formatBuildString(row.build_no)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalIns, row.data.missIns)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalBranches, row.data.missBranches)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalCxty, row.data.missCxty)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalLines, row.data.missLines)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalMethods, row.data.missMethods)}</TableCell>
                                        <TableCell
                                            align="right">{getPercentage(row.data.totalClasses, row.data.missClasses)}</TableCell>
                                    </TableRow>
                                ))}
                            </TableBody>
                        </Table>

I used below styles to scroll table body vertically.

thead, tbody { display: block; }
tbody {
   height: 100px;       /* Just for the demo          */
   overflow-y: auto;    /* Trigger vertical scroll    */
   overflow-x: hidden;  /* Hide the horizontal scroll */
}

But when I use it my table is not aligned well.



Solution 1:[1]

You should give style to table in this way.

<Table className="scroll">
....
</Table>


*.css file

.scroll {

   height: 100px;     
   overflow-y: auto;    
   overflow-x: hidden;

}

Or if you customize the table using styled components you can do in this way:

const ScrollYTable = styled(Table)&#768;

   height: 100px;     
   overflow-y: auto;    
   overflow-x: hidden;

&#768;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Hulterstrom