Display, position, overflow, z-index and other layout utilities for Tailwind CSS
Display Utilities
Tailwind CSS provides utilities for controlling the display box type of an element.
test_layout_display_examples
def test_layout_display_examples():
Test display utilities with various values.
Exported source
DISPLAY_VALUES = { # Display utilities for general display types (not flex/grid/table)"inline": "inline","block": "block", "inline-block": "inline-block","flow-root": "flow-root","contents": "contents","list-item": "list-item","hidden": "hidden","none": "none"}display_tw = SimpleFactory(DISPLAY_VALUES, "Display utilities for controlling the display box type of an element") # The display factory for general display types
Exported source
def test_layout_display_examples():"""Test display utilities with various values."""# Test display utilities with dot notationassertstr(display_tw.block) =="block"assertstr(display_tw.inline) =="inline"assertstr(display_tw.hidden) =="hidden"assertstr(display_tw.inline_block) =="inline-block"assertstr(display_tw.flow_root) =="flow-root"assertstr(display_tw.contents) =="contents"assertstr(display_tw.list_item) =="list-item"assertstr(display_tw.none) =="none"# Run the teststest_layout_display_examples()
Position Utilities
Control how an element is positioned in the document.
test_layout_position_examples
def test_layout_position_examples():
Test position utilities.
Exported source
POSITION_VALUES = { # Position utilities"static": "static","fixed": "fixed","absolute": "absolute","relative": "relative","sticky": "sticky"}position = SimpleFactory(POSITION_VALUES, "Position utilities for controlling how an element is positioned in the document") # The position factory
Exported source
def test_layout_position_examples():"""Test position utilities."""# Test position utilities with dot notationassertstr(position.static) =="static"assertstr(position.relative) =="relative"assertstr(position.absolute) =="absolute"assertstr(position.fixed) =="fixed"assertstr(position.sticky) =="sticky"# Run the teststest_layout_position_examples()
Inset Utilities (Top/Right/Bottom/Left)
Control the placement of positioned elements.
InsetDirectionalFactory
def InsetDirectionalFactory( prefix:str, # The base prefix ('inset') config:ScaleConfig, # Configuration defining valid scales and values):
Special factory for inset utilities that use hyphenated directions.
test_layout_inset_examples
def test_layout_inset_examples():
Test inset utilities for positioning elements.
Exported source
# Inset utilities (top, right, bottom, left)inset = InsetDirectionalFactory("inset", INSET_CONFIG) # The inset factory for positioning# Individual direction utilities don't need special handlingtop = ScaledFactory("top", INSET_CONFIG, "Top position utilities for controlling vertical placement")right = ScaledFactory("right", INSET_CONFIG, "Right position utilities for controlling horizontal placement")bottom = ScaledFactory("bottom", INSET_CONFIG, "Bottom position utilities for controlling vertical placement")left = ScaledFactory("left", INSET_CONFIG, "Left position utilities for controlling horizontal placement")start = ScaledFactory("start", INSET_CONFIG, "Logical start position utilities (left in LTR, right in RTL)")end = ScaledFactory("end", INSET_CONFIG, "Logical end position utilities (right in LTR, left in RTL)")
Exported source
def test_layout_inset_examples():"""Test inset utilities for positioning elements."""# Test inset utilitiesassertstr(inset(0)) =="inset-0"assertstr(inset(4)) =="inset-4"assertstr(inset("1/2")) =="inset-1/2"assertstr(inset.auto) =="inset-auto"assertstr(inset.full) =="inset-full"assertstr(inset.negative(4)) =="-inset-4"# Test directional insetassertstr(inset.x(4)) =="inset-x-4"assertstr(inset.y(8)) =="inset-y-8"# Test individual directionsassertstr(top(0)) =="top-0"assertstr(right(4)) =="right-4"assertstr(bottom.auto) =="bottom-auto"assertstr(left.negative(2)) =="-left-2"# Run the teststest_layout_inset_examples()
Overflow Utilities
Control how an element handles content that is too large for the container.
OverflowFactory
def OverflowFactory():
Factory for overflow utilities with directional support.
overflow = OverflowFactory() # The overflow factory
Exported source
def test_layout_overflow_examples():"""Test overflow utilities for content handling."""# Test overflow utilitiesassertstr(overflow.auto) =="overflow-auto"assertstr(overflow.hidden) =="overflow-hidden"assertstr(overflow.visible) =="overflow-visible"# Directional overflow utilitiesassertstr(overflow.x.auto) =="overflow-x-auto"assertstr(overflow.y.scroll) =="overflow-y-scroll"assertstr(overflow.x.visible) =="overflow-x-visible"assertstr(overflow.y.visible) =="overflow-y-visible"assertstr(overflow.y.clip) =="overflow-y-clip"# Run the teststest_layout_overflow_examples()
Z-Index Utilities
Control the stack order of an element.
test_layout_z_index_examples
def test_layout_z_index_examples():
Test z-index utilities for stack ordering.
Exported source
Z_INDEX_CONFIG = ScaleConfig( # Z-index configuration numeric=True, # Support numeric values 0-50 decimals=False, fractions=False, named=None, special={"auto": "auto" }, negative=True# Support negative z-index)# Create z-index factoryz = ScaledFactory("z", Z_INDEX_CONFIG, "Z-index utilities for controlling the stack order of an element") # The z-index factory
Exported source
def test_layout_z_index_examples():"""Test z-index utilities for stack ordering."""# Test z-index utilitiesassertstr(z(0)) =="z-0"assertstr(z(10)) =="z-10"assertstr(z(20)) =="z-20"assertstr(z(50)) =="z-50"assertstr(z.auto) =="z-auto"assertstr(z.negative(10)) =="-z-10"assertstr(z("999")) =="z-[999]"# (string number without units)assertstr(z("[999]")) =="z-[999]"# (explicit arbitrary value)# Run the teststest_layout_z_index_examples()
Float Utilities
Control the wrapping of content around an element.
Exported source
# Float utilitiesFLOAT_VALUES = {"right": "float-right","left": "float-left","start": "float-start","end": "float-end","none": "float-none"}float_tw = SimpleFactory(FLOAT_VALUES, "Float utilities for controlling the wrapping of content around an element") # Renamed to avoid conflict with Python's float
Clear Utilities
Control the wrapping behavior of an element after floating elements.
test_layout_float_clear_examples
def test_layout_float_clear_examples():
Test float and clear utilities for content wrapping.
Exported source
# Clear utilitiesCLEAR_VALUES = {"left": "clear-left","right": "clear-right","both": "clear-both","start": "clear-start","end": "clear-end","none": "clear-none"}# Create clear factoryclear = SimpleFactory(CLEAR_VALUES, "Clear utilities for controlling wrapping behavior after floating elements") # The clear factory
Exported source
def test_layout_float_clear_examples():"""Test float and clear utilities for content wrapping."""# Test float utilitiesassertstr(float_tw.right) =="float-right"assertstr(float_tw.left) =="float-left"assertstr(float_tw.start) =="float-start"assertstr(float_tw.end) =="float-end"assertstr(float_tw.none) =="float-none"# Test clear utilitiesassertstr(clear.left) =="clear-left"assertstr(clear.right) =="clear-right"assertstr(clear.both) =="clear-both"assertstr(clear.start) =="clear-start"assertstr(clear.end) =="clear-end"assertstr(clear.none) =="clear-none"# Run the teststest_layout_float_clear_examples()
Object Fit Utilities
Control how a replaced element’s content should be resized.
Exported source
# Object fit utilitiesOBJECT_FIT_VALUES = {"contain": "object-contain","cover": "object-cover","fill": "object-fill","none": "object-none","scale-down": "object-scale-down"}# Create object fit factoryobject_fit = SimpleFactory(OBJECT_FIT_VALUES, "Object fit utilities for controlling how replaced element content should be resized") # The object fit factory
Object Position Utilities
Control how a replaced element’s content should be positioned within its container.
Factory for object position with both fixed and custom values.
Exported source
# Object position utilities - combines fixed positions with custom value supportOBJECT_POSITION_VALUES = {"top-left": "object-top-left","top": "object-top","top-right": "object-top-right","left": "object-left","center": "object-center","right": "object-right","bottom-left": "object-bottom-left","bottom": "object-bottom","bottom-right": "object-bottom-right"}
test_layout_object_examples
def test_layout_object_examples():
Test object fit and position utilities.
Exported source
# Create object position factoryobject_position = ObjectPositionFactory( OBJECT_POSITION_VALUES, "Object position utilities for controlling content positioning within its container") # The object position factory
Exported source
def test_layout_object_examples():"""Test object fit and position utilities."""# Test object fit utilitiesassertstr(object_fit.contain) =="object-contain"assertstr(object_fit.cover) =="object-cover"assertstr(object_fit.fill) =="object-fill"assertstr(object_fit.none) =="object-none"assertstr(object_fit.scale_down) =="object-scale-down"# Test object position utilities with dot notationassertstr(object_position.center) =="object-center"assertstr(object_position.top) =="object-top"assertstr(object_position.bottom_right) =="object-bottom-right"assertstr(object_position("50% 25%")) =="object-[50% 25%]"assertstr(object_position("--custom-position")) =="object-(--custom-position)"# Run the teststest_layout_object_examples()
Visibility Utilities
Control the visibility of an element.
Exported source
# Visibility utilitiesVISIBILITY_VALUES = {"visible": "visible","invisible": "invisible","collapse": "collapse"}# Create visibility factoryvisibility = SimpleFactory(VISIBILITY_VALUES, "Visibility utilities for controlling the visibility of an element") # The visibility factory
Box Sizing Utilities
Control how the browser should calculate an element’s total size.
test_layout_visibility_examples
def test_layout_visibility_examples():
Test visibility and box sizing utilities.
Exported source
# Box sizing utilitiesBOX_SIZING_VALUES = {"border": "box-border","content": "box-content"}# Create box sizing factorybox = SimpleFactory(BOX_SIZING_VALUES, "Box sizing utilities for controlling how the browser calculates element size") # The box sizing factory
Exported source
def test_layout_visibility_examples():"""Test visibility and box sizing utilities."""# Test visibility utilitiesassertstr(visibility.visible) =="visible"assertstr(visibility.invisible) =="invisible"assertstr(visibility.collapse) =="collapse"# Test box sizing utilitiesassertstr(box.border) =="box-border"assertstr(box.content) =="box-content"# Run the teststest_layout_visibility_examples()
Isolation Utilities
Control whether an element should explicitly create a new stacking context.
Exported source
# Isolation utilitiesISOLATION_VALUES = {"isolate": "isolate","auto": "isolation-auto"}# Create isolation factoryisolation = SimpleFactory(ISOLATION_VALUES, "Isolation utilities for creating a new stacking context") # The isolation factory
Factory for aspect ratio with both fixed and custom values.
Exported source
# Aspect ratio utilities - fixed values with custom ratio supportASPECT_RATIO_VALUES = {"auto": "aspect-auto","square": "aspect-square","video": "aspect-video"}
test_layout_aspect_columns_examples
def test_layout_aspect_columns_examples():
Test aspect ratio and columns utilities.
Exported source
# Create aspect ratio factoryaspect = AspectRatioFactory(ASPECT_RATIO_VALUES, "Aspect ratio utilities for controlling element proportions") # The aspect ratio factory
Exported source
def test_layout_aspect_columns_examples():"""Test aspect ratio and columns utilities."""# Test aspect ratio utilities with dot notationassertstr(aspect.auto) =="aspect-auto"assertstr(aspect.square) =="aspect-square"assertstr(aspect.video) =="aspect-video"assertstr(aspect("16/9")) =="aspect-16/9"assertstr(aspect("4/3")) =="aspect-4/3"assertstr(aspect("--custom")) =="aspect-(--custom)"# Run the teststest_layout_aspect_columns_examples()
Columns Utilities
Control the number of columns within an element.
test_layout_columns_examples
def test_layout_columns_examples():
Test columns utilities.
Exported source
COLUMNS_CONFIG = ScaleConfig( # Columns configuration with container sizes numeric=True, # Support columns-1 through columns-12 decimals=False, fractions=False, named=CONTAINER_SCALES, # Use all container scales (3xs through 7xl) special={"auto": "auto" }, negative=False)# Create columns factorycolumns = ScaledFactory("columns", COLUMNS_CONFIG, "Columns utilities for controlling the number of columns within an element") # The columns factory
Exported source
def test_layout_columns_examples():"""Test columns utilities."""# Test columns utilitiesassertstr(columns(1)) =="columns-1"assertstr(columns(2)) =="columns-2"assertstr(columns(3)) =="columns-3"assertstr(columns.auto) =="columns-auto"assertstr(columns.xs) =="columns-xs"assertstr(columns.sm) =="columns-sm"assertstr(columns.lg) =="columns-lg"assertstr(columns._3xl) =="columns-3xl"assertstr(columns._4xl) =="columns-4xl"assertstr(columns._5xl) =="columns-5xl"assertstr(columns._6xl) =="columns-6xl"assertstr(columns._7xl) =="columns-7xl"# Run the teststest_layout_columns_examples()
Break Utilities
Control column and page breaks.
BreakFactory
def BreakFactory():
Factory for break utilities with before, after, and inside sub-factories.
Test layout utilities with modifiers for conditional styling.
Exported source
def test_layout_modifier_examples():"""Test layout utilities with modifiers for conditional styling."""# Test display utilities with modifiers (now supported!)assertstr(display_tw.hidden) =="hidden"assertstr(display_tw.block) =="block"assertstr(display_tw.hidden.hover) =="hover:hidden"assertstr(display_tw.block.md) =="md:block"assertstr(display_tw.none.dark) =="dark:none"# Test position utilities with modifiers (now supported!)assertstr(position.fixed) =="fixed"assertstr(position.absolute.hover) =="hover:absolute"assertstr(position.relative.lg) =="lg:relative"assertstr(position.sticky.md.dark) =="dark:md:sticky"# Test inset utilities with modifiers (these already supported modifiers)assertstr(inset(0).hover) =="hover:inset-0"assertstr(top(4).md) =="md:top-4"assertstr(bottom.auto.lg) =="lg:bottom-auto"assertstr(left.negative(2).hover) =="hover:-left-2"# Test z-index with modifiersassertstr(z(10).hover) =="hover:z-10"assertstr(z(50).lg) =="lg:z-50"assertstr(z.auto.dark) =="dark:z-auto"# Test overflow utilities with modifiers (now supported!)assertstr(overflow.hidden) =="overflow-hidden"assertstr(overflow.auto.hover) =="hover:overflow-auto"assertstr(overflow.x.scroll.md) =="md:overflow-x-scroll"assertstr(overflow.y.hidden.dark) =="dark:overflow-y-hidden"# Test float utilities with modifiers (now supported!)assertstr(float_tw.right) =="float-right"assertstr(float_tw.left.hover) =="hover:float-left"assertstr(float_tw.none.lg) =="lg:float-none"# Test clear utilities with modifiers (now supported!)assertstr(clear.both) =="clear-both"assertstr(clear.left.md) =="md:clear-left"assertstr(clear.none.hover) =="hover:clear-none"# Test responsive insetassertstr(inset.x(4).sm) =="sm:inset-x-4"assertstr(inset.y(8).md) =="md:inset-y-8"# Test group/peer modifiersassertstr(z(20).group("hover")) =="group-hover:z-20"assertstr(position.fixed.group("focus")) =="group-focus:fixed"assertstr(display_tw.none.peer("checked")) =="peer-checked:none"# Test arbitrary modifiersassertstr(top(0).aria("expanded")) =="aria-expanded:top-0"assertstr(position.absolute.data("open")) =="data-[open]:absolute"assertstr(display_tw.block.has(":checked")) =="has-[:checked]:block"# Run the teststest_layout_modifier_examples()
Helper Functions
Convenient functions for common layout patterns:
center_absolute
def center_absolute()->str: # Combined CSS classes for centering an element
Center an absolutely positioned element.
stack_context
def stack_context( z_value:int=10, # The z-index value for the stacking context)->str: # Combined CSS classes for creating a stacking context
Create a stacking context with z-index.
sticky_top
def sticky_top( offset:Union=0, # Top offset value (e.g., 0, 4, '1rem'))->str: # Combined CSS classes for sticky positioning
Make element sticky at top with optional offset.
full_bleed
def full_bleed()->str: # Combined CSS classes for full-bleed layout
Make element break out of container constraints.
test_layout_helper_examples
def test_layout_helper_examples():
Test helper functions for common layout patterns.
Exported source
def test_layout_helper_examples():"""Test helper functions for common layout patterns."""# Test helper functionsassert center_absolute() =="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2"assert stack_context(20) =="relative z-20"assert sticky_top(4) =="sticky top-4"assert full_bleed() =="relative left-1/2 right-1/2 -mx-[50vw] w-screen"# Run the teststest_layout_helper_examples()