CSS 文档
本文介绍如何通过自定义样式覆盖 Gantt 图元素的默认颜色设置。内容涵盖了用于为 Gantt 图各部分设置样式的主要类选 择器和模板,包括 表格区域(Grid area)、时间轴区域(Timeline area) 和 资源面板(Resource panel)。
表格区域样式(Styling Grid)
本节介绍用于为 表格区域 主要元素设置样式的 CSS 选择器。

表格元素的 DOM 结构如下:
- .gantt_grid
- .gantt_grid_scale
- .gantt_grid_head_cell
- .gantt_grid_data
- .gantt_row.odd
- .gantt_row
- .gantt_row.gantt_row_task
- .gantt_row.gantt_row_project
- .gantt_row.gantt_row_milestone
- gantt_cell.gantt_cell_tree
- .gantt_tree_indent
- .gantt_tree_icon.gantt_close
- .gantt_tree_icon.gantt_open
- .gantt_tree_content
- gantt_cell
- .gantt_tree_content
表格头部(Grid header)
可以使用 .gantt_grid_scale 类选择器自定义表格头部的样式。
以下示例为表格和时间轴的头部设置统一的背景和字体颜色:
.gantt_grid_scale, .gantt_task_scale, .gantt_task_vscroll {
background-color: #eee;
}
.gantt_grid_scale, .gantt_task_scale,
.gantt_task .gantt_task_scale .gantt_scale_cell,
.gantt_grid_scale .gantt_grid_head_cell {
color: #000;
font-size: 12px;
border-bottom: 1px solid #cecece;
}

Related example: Styling grid and timeline headers
标尺高度(Scale height)
请避免通过 CSS 更改表格头部和时间标尺的高度。
建议通过 Gantt 的 scale_height 配置属性设置标尺高度:
gantt.config.scale_height = 50;
表格头部 单元格
要为表格头部的单元格设置样式,请使用 .gantt_grid_head_cell 选择器。
可用于定位单元格的选择器包括:
- .gantt_grid_head_cell[data-column-id='columnName'] - 选中特定列的单元格;
其中 columnName 应对应于 column 的 name 属性:
<style>
.gantt_grid_head_cell[data-column-id='columnName'] {
background-color: #ededed;
color:black;
}
</style>
gantt.config.columns = [
...
{name: "columnName", align: "center"},
...
];

Related example: Styling a particular cell in the grid header
-
.gantt_grid_head_cell[data-column-index='1'] - 通过索引选中列;
-
.gantt_grid_head_cell[data-column-name='start_date'] - 通过名称选中列。
表格主体(Grid body)
可以通过对 .gantt_grid_data 应用 CSS 样式自定义表格主体的颜色。

表格行样式
可以通过 .gantt_row 选择器更改表格行的样式。

间隔行(Every other row)
要设置交替行的样式,可将 CSS 应用于 .gantt_row.odd 选择器,例如:
.gantt_row.odd{
background-color:#f4f4fb;
}

Related example: Styling every other row in grid
虽然界面上高亮的是偶数行,但样式实际应用于索引为奇数(1, 3, 5 等)的行,具体请参见 row indexes。
选中行(Selected row)
要设置表格中选中行的样式,请使用 .gantt_row.gantt_selected 选择器:
.gantt_grid_data .gantt_row.gantt_selected,
.gantt_grid_data .gantt_row.odd.gantt_selected,
.gantt_task_row.gantt_selected {
background-color: #fff3a1;
}
Related example: Styling selected row