CSS

CSS#

You can use CSS to alter how tables are rendered.

The recommended way to do this is to pass your CSS to itables.options.css (or to the css argument of show). This CSS is embedded in the HTML output of every table, so it is always guaranteed to be applied - unlike a standalone display(HTML(f"<style>{css}</style>")) cell, which some notebook front-ends (e.g. VS Code) may not render until that specific cell output is scrolled into view, see issue #572.

For instance, we change the font size for all the tables in the document with this code:

import itables

itables.options.css = """
.dt-container {
  font-size: small;
}
"""

itables.init_notebook_mode()

df = itables.sample_dfs.get_countries()

itables.show(df)
region country capital longitude latitude
code
AWLatin America & Caribbean ArubaOranjestad-70.01670012.516700
AFSouth AsiaAfghanistanKabul69.17610034.522800
AOSub-Saharan Africa AngolaLuanda13.242000-8.811550
ALEurope & Central AsiaAlbaniaTirane19.81720041.331700
ADEurope & Central AsiaAndorraAndorra la Vella1.52180042.507500
AEMiddle East & North AfricaUnited Arab EmiratesAbu Dhabi54.37050024.476400
ARLatin America & Caribbean ArgentinaBuenos Aires-58.417300-34.611800
AMEurope & Central AsiaArmeniaYerevan44.50900040.159600
ASEast Asia & PacificAmerican SamoaPago Pago-170.691000-14.284600
AGLatin America & Caribbean Antigua and BarbudaSaint John's-61.84560017.117500
(198 more rows not shown)

This is helpful for instance in the context of Quarto presentations.

With this CSS override, we change every datatable table header in the notebook to bold/italic.

itables.options.css = """
.dataTable th {
    font-weight: bolder;
    font-style: italic;
}
"""

itables.show(df)
region country capital longitude latitude
code
AWLatin America & Caribbean ArubaOranjestad-70.01670012.516700
AFSouth AsiaAfghanistanKabul69.17610034.522800
AOSub-Saharan Africa AngolaLuanda13.242000-8.811550
ALEurope & Central AsiaAlbaniaTirane19.81720041.331700
ADEurope & Central AsiaAndorraAndorra la Vella1.52180042.507500
AEMiddle East & North AfricaUnited Arab EmiratesAbu Dhabi54.37050024.476400
ARLatin America & Caribbean ArgentinaBuenos Aires-58.417300-34.611800
AMEurope & Central AsiaArmeniaYerevan44.50900040.159600
ASEast Asia & PacificAmerican SamoaPago Pago-170.691000-14.284600
AGLatin America & Caribbean Antigua and BarbudaSaint John's-61.84560017.117500
(198 more rows not shown)

You might also want to alter the style of specific tables only. To do this, either pass CSS to the css argument of show (rather than to itables.options.css), or add a new class to the target tables and target that class in your CSS, as in the example below:

class_specific_css = ".table_with_monospace_font { font-family: courier, monospace }"

itables.show(
    df,
    css=class_specific_css,
    classes="display nowrap table_with_monospace_font",
)
region country capital longitude latitude
code
AWLatin America & Caribbean ArubaOranjestad-70.01670012.516700
AFSouth AsiaAfghanistanKabul69.17610034.522800
AOSub-Saharan Africa AngolaLuanda13.242000-8.811550
ALEurope & Central AsiaAlbaniaTirane19.81720041.331700
ADEurope & Central AsiaAndorraAndorra la Vella1.52180042.507500
AEMiddle East & North AfricaUnited Arab EmiratesAbu Dhabi54.37050024.476400
ARLatin America & Caribbean ArgentinaBuenos Aires-58.417300-34.611800
AMEurope & Central AsiaArmeniaYerevan44.50900040.159600
ASEast Asia & PacificAmerican SamoaPago Pago-170.691000-14.284600
AGLatin America & Caribbean Antigua and BarbudaSaint John's-61.84560017.117500
(198 more rows not shown)

Tip

You can also inject CSS with display(HTML(f"<style>{css}</style>")). This still works, but the CSS only takes effect once that specific cell’s output has been rendered by the notebook front-end - in VS Code, an output that is scrolled out of view may not be rendered until you scroll back to it, so the itables.options.css/css= approach above is more reliable.