Skip to main content

px()

Helper for providing a CSS length value in pixels.

Usage

Source

px(x)

For certain parameters, a length value is required. Examples include the setting of font sizes (e.g., in style.text()) and thicknesses of lines (e.g., in style.borders()). Setting a length in pixels with px() allows for an absolute definition of size as opposed to the analogous helper function pct().

Parameters

x: float
The integer or float value to format as a string (e.g., "12px") for some arguments that can take values as units of pixels.

Returns

str
The value as a CSS length in pixels (e.g., "12px").

Examples

The px() helper turns a number into a CSS length in pixels.

from great_tables import px

px(12)
'12px'

Here, px() gives the font size of the num column’s values and the thickness of the line under the char column’s values.

from great_tables import GT, exibble, loc, style

(
    GT(exibble[["num", "char"]].head(3))
    .tab_style(style=style.text(size=px(18)), locations=loc.body(columns="num"))
    .tab_style(
        style=style.borders(sides="bottom", color="steelblue", weight=px(3)),
        locations=loc.body(columns="char"),
    )
)
num char
0.1111 apricot
2.222 banana
33.33 coconut