class FontFamily(str, Enum):"""Font family options for controlling typography.""" SANS ="sans" SERIF ="serif" MONO ="mono"font_family = enums_to_simple_factory("font", [FontFamily], "Utilities for controlling the font family of an element.") # Font family utilities
test_typography_font_family_examples
def test_typography_font_family_examples():
Test font family utilities.
Exported source
def test_typography_font_family_examples():"""Test font family utilities."""assertstr(font_family.sans) =="font-sans"assertstr(font_family.serif) =="font-serif"assertstr(font_family.mono) =="font-mono"# Run the teststest_typography_font_family_examples()
Font Size
Utilities for controlling the font size of an element.
test_typography_font_size_examples
def test_typography_font_size_examples():
Test font size utilities.
Exported source
font_size = ScaledFactory('text', FONT_SIZE_CONFIG, "Utilities for controlling the font size of an element.") # Font size factory
Exported source
def test_typography_font_size_examples():"""Test font size utilities."""assertstr(font_size.xs) =="text-xs"assertstr(font_size.sm) =="text-sm"assertstr(font_size.base) =="text-base"assertstr(font_size.lg) =="text-lg"assertstr(font_size.xl) =="text-xl"assertstr(font_size._2xl) =="text-2xl"assertstr(font_size._3xl) =="text-3xl"assertstr(font_size._4xl) =="text-4xl"assertstr(font_size._5xl) =="text-5xl"assertstr(font_size._6xl) =="text-6xl"assertstr(font_size._7xl) =="text-7xl"assertstr(font_size._8xl) =="text-8xl"assertstr(font_size._9xl) =="text-9xl"# Run the teststest_typography_font_size_examples()
Font Smoothing
Utilities for controlling the font smoothing of an element:
test_typography_font_smoothing_examples
def test_typography_font_smoothing_examples():
Test font smoothing utilities.
Exported source
antialiased = SingleValueFactory("antialiased", "Applies antialiased font smoothing") # Antialiased font smoothingsubpixel_antialiased = SingleValueFactory("subpixel-antialiased", "Applies subpixel antialiased font smoothing") # Subpixel antialiased font smoothing
Exported source
def test_typography_font_smoothing_examples():"""Test font smoothing utilities."""assertstr(antialiased) =="antialiased"assertstr(subpixel_antialiased) =="subpixel-antialiased"# Run the teststest_typography_font_smoothing_examples()
Font Style
Utilities for controlling the style of text:
test_typography_font_style_examples
def test_typography_font_style_examples():
Test font style utilities.
Exported source
italic = SingleValueFactory("italic", "Sets font style to italic") # Italic font stylenot_italic = SingleValueFactory("not-italic", "Sets font style to normal") # Normal font style
Exported source
def test_typography_font_style_examples():"""Test font style utilities."""assertstr(italic) =="italic"assertstr(not_italic) =="not-italic"# Run the teststest_typography_font_style_examples()
Font Weight
Utilities for controlling the font weight of an element:
class FontWeight(str, Enum):"""Font weight options from thin to black.""" THIN ="thin" EXTRALIGHT ="extralight" LIGHT ="light" NORMAL ="normal" MEDIUM ="medium" SEMIBOLD ="semibold" BOLD ="bold" EXTRABOLD ="extrabold" BLACK ="black"font_weight = enums_to_simple_factory("font", [FontWeight], "Utilities for controlling the font weight of an element.") # Font weight utilities
test_typography_font_weight_examples
def test_typography_font_weight_examples():
Test font weight utilities.
Exported source
def test_typography_font_weight_examples():"""Test font weight utilities."""# Named weightsassertstr(font_weight.thin) =="font-thin"assertstr(font_weight.extralight) =="font-extralight"assertstr(font_weight.light) =="font-light"assertstr(font_weight.normal) =="font-normal"assertstr(font_weight.medium) =="font-medium"assertstr(font_weight.semibold) =="font-semibold"assertstr(font_weight.bold) =="font-bold"assertstr(font_weight.extrabold) =="font-extrabold"assertstr(font_weight.black) =="font-black"# Run the teststest_typography_font_weight_examples()
class FontStretch(str, Enum):"""Font stretch options for controlling font width.""" ULTRA_CONDENSED ="ultra-condensed" EXTRA_CONDENSED ="extra-condensed" CONDENSED ="condensed" SEMI_CONDENSED ="semi-condensed" NORMAL ="normal" SEMI_EXPANDED ="semi-expanded" EXPANDED ="expanded" EXTRA_EXPANDED="extra-expanded" ULRA_EXPANDED ="ultra-expanded"font_stretch = enums_to_simple_factory("font-stretch", [FontStretch], "Utilities for selecting the width of a font face.") # Font stretch utilities
test_typography_font_stretch_examples
def test_typography_font_stretch_examples():
Test font stretch utilities.
Exported source
def test_typography_font_stretch_examples():"""Test font stretch utilities."""assertstr(font_stretch.ultra_condensed) =="font-stretch-ultra-condensed"assertstr(font_stretch.extra_condensed) =="font-stretch-extra-condensed"assertstr(font_stretch.condensed) =="font-stretch-condensed"assertstr(font_stretch.semi_condensed) =="font-stretch-semi-condensed"assertstr(font_stretch.normal) =="font-stretch-normal"assertstr(font_stretch.semi_expanded) =="font-stretch-semi-expanded"assertstr(font_stretch.expanded) =="font-stretch-expanded"assertstr(font_stretch.extra_expanded) =="font-stretch-extra-expanded"assertstr(font_stretch.ultra_expanded) =="font-stretch-ultra-expanded"# Run the teststest_typography_font_stretch_examples()
def test_typography_spacing_examples():"""Test letter spacing utilities."""# Letter spacingassertstr(tracking.tighter) =="tracking-tighter"assertstr(tracking.tight) =="tracking-tight"assertstr(tracking.normal) =="tracking-normal"assertstr(tracking.wide) =="tracking-wide"assertstr(tracking.wider) =="tracking-wider"assertstr(tracking.widest) =="tracking-widest"assertstr(tracking("0.05em")) =="tracking-[0.05em]"assertstr(tracking("--custom-tracking")) =="tracking-(--custom-tracking)"# Run the teststest_typography_spacing_examples()
Line Clamp
Utilities for clamping text to a specific number of lines:
test_typography_line_clamp_examples
def test_typography_line_clamp_examples():
Test line clamp utilities.
Exported source
def test_typography_line_clamp_examples():"""Test line clamp utilities."""# Line clampassertstr(line_clamp.none) =="line-clamp-none"assertstr(line_clamp(1)) =="line-clamp-1"assertstr(line_clamp(2)) =="line-clamp-2"assertstr(line_clamp(3)) =="line-clamp-3"assertstr(line_clamp(4)) =="line-clamp-4"assertstr(line_clamp(5)) =="line-clamp-5"assertstr(line_clamp(6)) =="line-clamp-6"assertstr(line_clamp("[10]")) =="line-clamp-[10]"# Run the teststest_typography_line_clamp_examples()
Line Height
Utilities for controlling the leading (line height) of an element:
test_typography_line_height_examples
def test_typography_line_height_examples():
Test line height utilities.
Exported source
def test_typography_line_height_examples():"""Test line height utilities."""# Line heightassertstr(leading.none) =="leading-none"assertstr(leading(3)) =="leading-3"assertstr(leading(4)) =="leading-4"assertstr(leading(5)) =="leading-5"assertstr(leading(6)) =="leading-6"assertstr(leading(7)) =="leading-7"assertstr(leading(8)) =="leading-8"assertstr(leading(9)) =="leading-9"assertstr(leading(10)) =="leading-10"assertstr(leading("1.5")) =="leading-[1.5]"assertstr(leading("20px")) =="leading-[20px]"assertstr(leading("--custom-leading")) =="leading-(--custom-leading)"# Run the teststest_typography_line_height_examples()
List Style
Utilities for controlling list styles:
List Style Image
Utilities for controlling the marker images for list items:
ListImageFactory
def ListImageFactory():
Factory for list-style-image utilities.
ListImageUtility
def ListImageUtility( prefix:str, # The utility prefix (e.g., 'w' for width, 'p' for padding)):
Utility class for list-style-image with arbitrary value support.
test_typography_list_image_examples
def test_typography_list_image_examples():
Test list style image utilities.
Exported source
def test_typography_list_image_examples():"""Test list style image utilities."""assertstr(list_image.none) =="list-image-none"assertstr(list_image("url(/path/to/marker.svg)")) =="list-image-[url(/path/to/marker.svg)]"assertstr(list_image("--custom-marker")) =="list-image-(--custom-marker)"assertstr(list_image("url('star.png')")) =="list-image-[url('star.png')]"# Run the teststest_typography_list_image_examples()
class ListStylePosition(str, Enum):"""List marker position options.""" INSIDE ="inside" OUTSIDE ="outside"list_position = enums_to_simple_factory("list", [ListStylePosition], "Utilities for controlling the position of bullets and numbers in lists.") # List position factory
class ListStyleType(str, Enum):"""List marker type options.""" DISC ="disc" DECIMAL ="decimal" NONE ="none"list_style = enums_to_simple_factory("list", [ListStyleType], "Utilities for controlling the marker style of a list.") # List type factory
test_typography_list_styles_examples
def test_typography_list_styles_examples():
Test list styles utilities.
Exported source
def test_typography_list_styles_examples():"""Test list styles utilities."""# List stylesassertstr(list_position.inside) =="list-inside"assertstr(list_position.outside) =="list-outside"assertstr(list_style.disc) =="list-disc"assertstr(list_style.decimal) =="list-decimal"assertstr(list_style.none) =="list-none"# Run the teststest_typography_list_styles_examples()
class TextAlign(str, Enum):"""Text alignment options.""" LEFT ="left" CENTER ="center" RIGHT ="right" JUSTIFY ="justify" START ="start" end ="end"text_align = enums_to_simple_factory("text", [TextAlign], "Utilities for controlling the alignment of text.") # Text alignment factory
test_typography_text_alignment_examples
def test_typography_text_alignment_examples():
Test text alignment utilities.
Exported source
def test_typography_text_alignment_examples():"""Test text alignment utilities."""# Text alignmentassertstr(text_align.left) =="text-left"assertstr(text_align.center) =="text-center"assertstr(text_align.right) =="text-right"assertstr(text_align.justify) =="text-justify"assertstr(text_align.start) =="text-start"assertstr(text_align.end) =="text-end"# Run the teststest_typography_text_alignment_examples()
Text Color
Utilities for controlling the text color of an element.
test_typography_text_color_examples
def test_typography_text_color_examples():
Test text color utilities.
Exported source
def test_typography_text_color_examples():"""Test text color utilities."""# Standard colorsassertstr(text_color.red._500) =="text-red-500"assertstr(text_color.blue._300) =="text-blue-300"assertstr(text_color.green._600) =="text-green-600"# Test all 22 color familiesassertstr(text_color.red._500) =="text-red-500"assertstr(text_color.orange._500) =="text-orange-500"assertstr(text_color.amber._500) =="text-amber-500"assertstr(text_color.yellow._500) =="text-yellow-500"assertstr(text_color.lime._500) =="text-lime-500"assertstr(text_color.green._500) =="text-green-500"assertstr(text_color.emerald._500) =="text-emerald-500"assertstr(text_color.teal._500) =="text-teal-500"assertstr(text_color.cyan._500) =="text-cyan-500"assertstr(text_color.sky._500) =="text-sky-500"assertstr(text_color.blue._500) =="text-blue-500"assertstr(text_color.indigo._500) =="text-indigo-500"assertstr(text_color.violet._500) =="text-violet-500"assertstr(text_color.purple._500) =="text-purple-500"assertstr(text_color.fuchsia._500) =="text-fuchsia-500"assertstr(text_color.pink._500) =="text-pink-500"assertstr(text_color.rose._500) =="text-rose-500"assertstr(text_color.slate._500) =="text-slate-500"assertstr(text_color.gray._500) =="text-gray-500"assertstr(text_color.zinc._500) =="text-zinc-500"assertstr(text_color.neutral._500) =="text-neutral-500"assertstr(text_color.stone._500) =="text-stone-500"# Special colorsassertstr(text_color.transparent) =="text-transparent"assertstr(text_color.black) =="text-black"assertstr(text_color.white) =="text-white"assertstr(text_color.current) =="text-current"assertstr(text_color.inherit) =="text-inherit"# With opacityassertstr(text_color.red._500.opacity(50)) =="text-red-500/50"assertstr(text_color.blue._300.opacity(75)) =="text-blue-300/75"# Arbitrary valuesassertstr(text_color("#ff0000")) =="text-[#ff0000]"assertstr(text_color("rgb(255, 0, 0)")) =="text-[rgb(255, 0, 0)]"assertstr(text_color("--custom-color")) =="text-(--custom-color)"# Run the teststest_typography_text_color_examples()
Text Decoration
Text Decoration Line
Utilities for controlling the decoration of text:
Text Decoration Color
Utilities for controlling the color of text decorations:
Text Decoration Style
Utilities for controlling the style of text decorations:
Utilities for controlling the offset of a text underline:
test_typography_underline_offset_examples
def test_typography_underline_offset_examples():
Test underline offset utilities.
Exported source
def test_typography_underline_offset_examples():"""Test underline offset utilities."""# Underline offsetassertstr(underline_offset(0)) =="underline-offset-0"assertstr(underline_offset(1)) =="underline-offset-1"assertstr(underline_offset(2)) =="underline-offset-2"assertstr(underline_offset(4)) =="underline-offset-4"assertstr(underline_offset(8)) =="underline-offset-8"assertstr(underline_offset.auto) =="underline-offset-auto"assertstr(underline_offset.negative(1)) =="-underline-offset-1"assertstr(underline_offset.negative(2)) =="-underline-offset-2"assertstr(underline_offset("3px")) =="underline-offset-[3px]"# Run the teststest_typography_underline_offset_examples()
Text Transform
Utilities for controlling the capitalization of text:
test_typography_text_transform_examples
def test_typography_text_transform_examples():
Test text transform utilities.
Exported source
def test_typography_text_transform_examples():"""Test text transform utilities."""# Text transformassertstr(uppercase) =="uppercase"assertstr(lowercase) =="lowercase"assertstr(capitalize) =="capitalize"assertstr(normal_case) =="normal-case"# Run the teststest_typography_text_transform_examples()
Text Overflow
Utilities for controlling text overflow in an element:
test_typography_text_overflow_examples
def test_typography_text_overflow_examples():
Test text overflow utilities.
Exported source
def test_typography_text_overflow_examples():"""Test text overflow utilities."""# Text overflowassertstr(truncate) =="truncate"assertstr(text_ellipsis) =="text-ellipsis"assertstr(text_clip) =="text-clip"# Run the teststest_typography_text_overflow_examples()
Text Wrap
Utilities for controlling how text wraps within an element:
test_typography_text_wrap_examples
def test_typography_text_wrap_examples():
Test text wrap utilities.
Exported source
def test_typography_text_wrap_examples():"""Test text wrap utilities."""# Text wrapassertstr(text_wrap) =="text-wrap"assertstr(text_nowrap) =="text-nowrap"assertstr(text_balance) =="text-balance"assertstr(text_pretty) =="text-pretty"# Run the teststest_typography_text_wrap_examples()
Text Indent
Utilities for controlling text indentation:
test_typography_text_indent_examples
def test_typography_text_indent_examples():
Test text indent utilities.
Exported source
def test_typography_text_indent_examples():"""Test text indent utilities."""# Text indentassertstr(indent(0)) =="indent-0"assertstr(indent(4)) =="indent-4"assertstr(indent(8)) =="indent-8"assertstr(indent.px) =="indent-px"assertstr(indent.negative(4)) =="-indent-4"assertstr(indent("10px")) =="indent-[10px]"# Run the teststest_typography_text_indent_examples()
Vertical Align
Utilities for controlling vertical alignment:
test_typography_vertical_align_examples
def test_typography_vertical_align_examples():
Test vertical align utilities.
Exported source
def test_typography_vertical_align_examples():"""Test vertical align utilities."""# Vertical alignassertstr(align.baseline) =="align-baseline"assertstr(align.top) =="align-top"assertstr(align.middle) =="align-middle"assertstr(align.bottom) =="align-bottom"assertstr(align.text_top) =="align-text-top"assertstr(align.text_bottom) =="align-text-bottom"assertstr(align.sub) =="align-sub"assertstr(align.super) =="align-super"# Run the teststest_typography_vertical_align_examples()
White Space
Utilities for controlling an element’s white-space property:
test_typography_whitespace_examples
def test_typography_whitespace_examples():
Test whitespace utilities.
Exported source
def test_typography_whitespace_examples():"""Test whitespace utilities."""# White spaceassertstr(whitespace.normal) =="whitespace-normal"assertstr(whitespace.nowrap) =="whitespace-nowrap"assertstr(whitespace.pre) =="whitespace-pre"assertstr(whitespace.pre_line) =="whitespace-pre-line"assertstr(whitespace.pre_wrap) =="whitespace-pre-wrap"assertstr(whitespace.break_spaces) =="whitespace-break-spaces"# Run the teststest_typography_whitespace_examples()
Word Break
Utilities for controlling word breaks in an element:
test_typography_word_break_examples
def test_typography_word_break_examples():
Test word break utilities.
Exported source
def test_typography_word_break_examples():"""Test word break utilities."""# Word breakassertstr(break_normal) =="break-normal"assertstr(break_all) =="break-all"assertstr(break_keep) =="break-keep"# Run the teststest_typography_word_break_examples()
Overflow Wrap
Utilities for controlling line breaks within words:
test_typography_overflow_wrap_examples
def test_typography_overflow_wrap_examples():
Test overflow wrap utilities.
Exported source
def test_typography_overflow_wrap_examples():"""Test overflow wrap utilities."""# Overflow wrapassertstr(wrap_break_word) =="wrap-break-word"assertstr(wrap_anywhere) =="wrap-anywhere"assertstr(wrap_normal) =="wrap-normal"# Run the teststest_typography_overflow_wrap_examples()
Hyphens
Utilities for controlling how words should be hyphenated:
test_typography_hyphens_examples
def test_typography_hyphens_examples():
Test hyphens utilities.
Exported source
def test_typography_hyphens_examples():"""Test hyphens utilities."""# Hyphensassertstr(hyphens.none) =="hyphens-none"assertstr(hyphens.manual) =="hyphens-manual"assertstr(hyphens.auto) =="hyphens-auto"# Run the teststest_typography_hyphens_examples()
Content
Utilities for controlling the content of the before and after pseudo-elements:
ContentFactory
def ContentFactory():
Factory for content utilities for pseudo-elements.
ContentUtility
def ContentUtility( prefix:str, # The utility prefix (e.g., 'w' for width, 'p' for padding)):
Utility class for content with arbitrary value support.
test_typography_content_examples
def test_typography_content_examples():
Test content utilities for pseudo-elements.
Exported source
def test_typography_content_examples():"""Test content utilities for pseudo-elements."""assertstr(content.none) =="content-none"assertstr(content("'Hello'")) =="content-['Hello']"assertstr(content('"→"')) =='content-["→"]'assertstr(content("attr(data-label)")) =="content-[attr(data-label)]"assertstr(content("--custom-content")) =="content-(--custom-content)"assertstr(content("open-quote")) =="content-[open-quote]"assertstr(content("' (' attr(href) ')'")) =="content-[' (' attr(href) ')']"# Run the teststest_typography_content_examples()
Practical Examples
Let’s see how to use these typography utilities in real FastHTML components:
test_typography_fasthtml_examples
def test_typography_fasthtml_examples():
Test typography utilities in practical FastHTML component examples.
Exported source
def test_typography_fasthtml_examples():"""Test typography utilities in practical FastHTML component examples."""from fasthtml.common import H1, H2, P, Div, Span, A, Ul, Li, Blockquotefrom cjm_fasthtml_tailwind.utilities.sizing import max_wfrom cjm_fasthtml_tailwind.utilities.backgrounds import bgfrom cjm_fasthtml_tailwind.utilities.spacing import pfrom cjm_fasthtml_tailwind.utilities.borders import rounded, border, border_colorfrom cjm_fasthtml_tailwind.utilities.layout import display_twfrom cjm_fasthtml_tailwind.utilities.flexbox_and_grid import gap, grid_display# Heading with multiple typography utilities heading = H1("Welcome to Our Site", cls=combine_classes( font_size._4xl, font_weight.bold, text_color.gray._900, tracking.tight, leading(8) ) )assert"text-4xl"in heading.attrs['class']assert"font-bold"in heading.attrs['class']assert"text-gray-900"in heading.attrs['class']assert"tracking-tight"in heading.attrs['class']assert"leading-8"in heading.attrs['class']# Paragraph with text styling paragraph = P("This is a sample paragraph with various text styles applied.", cls=combine_classes( font_size.base, text_color.gray._600, leading(7), text_align.justify ) )assert"text-base"in paragraph.attrs['class']assert"text-gray-600"in paragraph.attrs['class']# Link with underline and hover effects link = A("Click here", href="#", cls=combine_classes( text_color.blue._600, underline, decoration_style.dotted, decoration_thickness(2), font_weight.medium ) )assert"text-blue-600"in link.attrs['class']assert"underline"in link.attrs['class']assert"decoration-dotted"in link.attrs['class']# Truncated text truncated = Div("This is a very long text that should be truncated with an ellipsis when it overflows the container width.", cls=combine_classes(truncate, max_w.xs) )assert"truncate"in truncated.attrs['class']# Multi-line clamp clamped = P("This is a longer paragraph that spans multiple lines. It will be clamped to show only 3 lines with an ellipsis at the end if the content exceeds that limit.", cls=str(line_clamp(3)) )assert clamped.attrs['class'] =="line-clamp-3"# Code block with monospace font code = Div("const greeting = 'Hello, World!';", cls=combine_classes( font_family.mono, font_size.sm, text_color.gray._800, whitespace.pre, bg.gray._100, p(4), rounded() ) )assert"font-mono"in code.attrs['class']assert"whitespace-pre"in code.attrs['class']# List with custom styles custom_list = Ul( Li("First item"), Li("Second item"), Li("Third item"), cls=combine_classes( list_style.disc, list_position.inside, text_color.gray._700 ) )assert"list-disc"in custom_list.attrs['class']assert"list-inside"in custom_list.attrs['class']# Text with transform transformed = Span("uppercase text", cls=combine_classes(uppercase, tracking.wider) )assert"uppercase"in transformed.attrs['class']assert"tracking-wider"in transformed.attrs['class']# Blockquote with indent quote = Blockquote("The only way to do great work is to love what you do.", cls=combine_classes( font_size.lg, italic, text_color.gray._600, indent(8), border.l._4, border_color.gray._300, p.l(4) ) )assert"text-lg"in quote.attrs['class']assert"italic"in quote.attrs['class']assert"indent-8"in quote.attrs['class']return Div( heading, paragraph, link, truncated, clamped, code, custom_list, transformed, quote, cls=combine_classes(grid_display, gap(5)) )# Run the teststest_typography_fasthtml_examples()
Welcome to Our Site
This is a sample paragraph with various text styles applied.
This is a very long text that should be truncated with an ellipsis when it overflows the container width.
This is a longer paragraph that spans multiple lines. It will be clamped to show only 3 lines with an ellipsis at the end if the content exceeds that limit.
const greeting = 'Hello, World!';
First item
Second item
Third item
uppercase text
The only way to do great work is to love what you do.