Modifiers

Styled

Every Tailwind-named style method on the Styled trait, grouped by category with its signature.

FNStyled
Usage
use crownui::prelude::*;
 
fn card(title: &'static str, body: &'static str) -> impl View {
    vstack((text(title).text_size(17.0).font_semibold(), text(body).text_color(rgb(0x64748b))))
        .gap(6.0)
        .p(16.0)
        .w_full()
        .rounded(14.0)
        .bg(WHITE)
        .border(1.0)
        .border_color(rgba(0x0f172a14))
        .shadow(Shadow::sm())
}
Signature
pub trait Styled: Sized {
    fn modifier(self, modifier: Modifier) -> Self;
    // every other method is built on modifier and listed below
}
 
impl<K: Kind> Styled for Element<K> { .. }
impl Styled for VariantStyle { .. }

Styled is the trait behind every style method. Its methods are Tailwind class names that take literal values in logical pixels, or signals and bind closures for values that change.

Every widget returns an Element<K>, so every widget has these methods. VariantStyle implements Styled too, which is why the closure in .hover(|style| style.bg(..)) uses the same names. The Listeners and Semantic traits are implemented for every Styled type, so event and accessibility methods chain the same way.

Each call appends a modifier. Modifiers apply in order after the widget's own defaults, so the last call for a property wins. Read the styling guide for a walkthrough and the layout guide for how the layout methods combine.

Value types

Methods that take impl IntoProp<T> accept a literal or a reactive source.

f32impl IntoProp<f32>

An f32, f64, i32 or u32 literal, a Signal<f32>, a Memo<f32> or bind(move |rt| ..) returning f32.

Colorimpl IntoProp<Color>

A Color such as rgb(0x3b82f6), rgba(0x3b82f680) or WHITE, or a signal, memo or bind returning one.

boolimpl IntoProp<bool>

A bool, or a signal, memo or bind returning one.

Vectorimpl IntoProp<Vector>

A Vector::new(x, y), or a signal, memo or bind returning one.

Materialimpl IntoProp<Material>

A Material such as Material::thin(), or a reactive source of one.

A bound value updates that single property when its inputs change. The component does not run again. Methods that take a plain f32, u16, Shadow, CursorIcon or Vec<GridTrack> take literals only.

The modifier method

modifierfn modifier(self, modifier: Modifier) -> Selfrequired

Appends a raw Modifier. Implementors provide this one method and get every other method for free. Component libraries use it with Modifier::Style(StyleProp::..) for properties that have no named method.

Padding

MethodSignatureTailwind
pfn p(self, impl IntoProp<f32>) -> Selfp-*, every edge
pxfn px(self, impl IntoProp<f32>) -> Selfpx-*, left and right
pyfn py(self, impl IntoProp<f32>) -> Selfpy-*, top and bottom
ptfn pt(self, impl IntoProp<f32>) -> Selfpt-*
prfn pr(self, impl IntoProp<f32>) -> Selfpr-*
pbfn pb(self, impl IntoProp<f32>) -> Selfpb-*
plfn pl(self, impl IntoProp<f32>) -> Selfpl-*
text("Padded").p(16.0).px(24.0) // 16 top and bottom, 24 left and right

Margin

MethodSignatureTailwind
mfn m(self, impl IntoProp<f32>) -> Selfm-*, every edge
mxfn mx(self, impl IntoProp<f32>) -> Selfmx-*
myfn my(self, impl IntoProp<f32>) -> Selfmy-*
mtfn mt(self, impl IntoProp<f32>) -> Selfmt-*
mrfn mr(self, impl IntoProp<f32>) -> Selfmr-*
mbfn mb(self, impl IntoProp<f32>) -> Selfmb-*
mlfn ml(self, impl IntoProp<f32>) -> Selfml-*

Margins accept negative values, which pull a box over its neighbor.

Size

MethodSignatureTailwind
wfn w(self, impl IntoProp<f32>) -> Selfw-*
hfn h(self, impl IntoProp<f32>) -> Selfh-*
sizefn size(self, f32) -> Selfsize-*, width and height
min_wfn min_w(self, impl IntoProp<f32>) -> Selfmin-w-*
min_hfn min_h(self, impl IntoProp<f32>) -> Selfmin-h-*
max_wfn max_w(self, impl IntoProp<f32>) -> Selfmax-w-*
max_hfn max_h(self, impl IntoProp<f32>) -> Selfmax-h-*
w_fullfn w_full(self) -> Selfw-full
h_fullfn h_full(self) -> Selfh-full
w_fracfn w_frac(self, f32) -> Selfw-1/2 and friends, as a fraction
h_fracfn h_frac(self, f32) -> Selfh-1/2 and friends, as a fraction
aspectfn aspect(self, impl IntoProp<f32>) -> Selfaspect-*, width divided by height
vstack(()).w_frac(0.5).aspect(16.0 / 9.0).bg(rgb(0x0f172a))

Flex

MethodSignatureTailwind
flex_rowfn flex_row(self) -> Selfflex-row
flex_colfn flex_col(self) -> Selfflex-col, the default
flex_row_reversefn flex_row_reverse(self) -> Selfflex-row-reverse
flex_col_reversefn flex_col_reverse(self) -> Selfflex-col-reverse
flex_wrapfn flex_wrap(self) -> Selfflex-wrap
growfn grow(self, impl IntoProp<f32>) -> Selfgrow, grow-*
shrinkfn shrink(self, impl IntoProp<f32>) -> Selfshrink, shrink-* (default 1)
basisfn basis(self, impl IntoProp<f32>) -> Selfbasis-*
flex_1fn flex_1(self) -> Selfflex-1: grow(1.0).shrink(1.0).basis(0.0)
hstack((text("Label").shrink(0.0), text_field(value, "Value").flex_1()))

Gap

MethodSignatureTailwind
gapfn gap(self, impl IntoProp<f32>) -> Selfgap-*, rows and columns
gap_xfn gap_x(self, impl IntoProp<f32>) -> Selfgap-x-*, between columns
gap_yfn gap_y(self, impl IntoProp<f32>) -> Selfgap-y-*, between rows

Alignment

MethodSignatureTailwind
items_startfn items_start(self) -> Selfitems-start
items_centerfn items_center(self) -> Selfitems-center
items_endfn items_end(self) -> Selfitems-end
items_stretchfn items_stretch(self) -> Selfitems-stretch
items_baselinefn items_baseline(self) -> Selfitems-baseline
self_startfn self_start(self) -> Selfself-start
self_centerfn self_center(self) -> Selfself-center
self_endfn self_end(self) -> Selfself-end
self_stretchfn self_stretch(self) -> Selfself-stretch
justify_startfn justify_start(self) -> Selfjustify-start
justify_centerfn justify_center(self) -> Selfjustify-center
justify_endfn justify_end(self) -> Selfjustify-end
justify_betweenfn justify_between(self) -> Selfjustify-between
justify_aroundfn justify_around(self) -> Selfjustify-around
justify_evenlyfn justify_evenly(self) -> Selfjustify-evenly

Without items_*, children stretch across the cross axis.

Grid

MethodSignatureTailwind
gridfn grid(self) -> Selfgrid
grid_colsfn grid_cols(self, u16) -> Selfgrid-cols-*, equal columns
grid_template_colsfn grid_template_cols(self, Vec<GridTrack>) -> Selfgrid-template-columns
col_spanfn col_span(self, u16) -> Selfcol-span-*
row_spanfn row_span(self, u16) -> Selfrow-span-*

grid_cols and grid_template_cols also switch the box to grid layout, so .grid() is only needed for a grid with no explicit columns. GridTrack has three variants: Length(f32) in logical pixels, Fraction(f32) for a share of the free space like 1fr, and Auto to fit the content.

vstack((text("A"), text("B"), text("C")))
    .grid_template_cols(vec![GridTrack::Length(120.0), GridTrack::Fraction(1.0), GridTrack::Auto])
    .gap(8.0)

Position

MethodSignatureTailwind
absolutefn absolute(self) -> Selfabsolute
relativefn relative(self) -> Selfrelative, the default
insetfn inset(self, impl IntoProp<f32>) -> Selfinset-*, every edge
topfn top(self, impl IntoProp<f32>) -> Selftop-*
rightfn right(self, impl IntoProp<f32>) -> Selfright-*
bottomfn bottom(self, impl IntoProp<f32>) -> Selfbottom-*
leftfn left(self, impl IntoProp<f32>) -> Selfleft-*
zfn z(self, i16) -> Selfz-*, paint order among siblings
top_layerfn top_layer(self) -> SelfCSS top layer

top_layer paints the node above every other node in the window and lets it escape the clips of its ancestors. Popovers and menus use it.

Display and overflow

MethodSignatureTailwind
display_nonefn display_none(self) -> Selfhidden (display: none), takes no space
hiddenfn hidden(self, impl IntoProp<bool>) -> Selfinvisible, keeps its space
overflow_hiddenfn overflow_hidden(self) -> Selfoverflow-hidden, clips children
overflow_scrollfn overflow_scroll(self) -> Selfoverflow-scroll, makes a scroll container
text("Unsaved changes").hidden(bind(move |rt| !dirty.get(rt)))

Background, border and corners

MethodSignatureTailwind
bgfn bg(self, impl IntoProp<Color>) -> Selfbg-*
borderfn border(self, impl IntoProp<f32>) -> Selfborder-* (width)
border_colorfn border_color(self, impl IntoProp<Color>) -> Selfborder-* (color)
ringfn ring(self, f32, impl IntoProp<Color>) -> Selfring-*, outside the border
outlinefn outline(self, f32, Color) -> Selfoutline-*, the same as ring
roundedfn rounded(self, impl IntoProp<f32>) -> Selfrounded-*, every corner
rounded_tfn rounded_t(self, impl IntoProp<f32>) -> Selfrounded-t-*
rounded_rfn rounded_r(self, impl IntoProp<f32>) -> Selfrounded-r-*
rounded_bfn rounded_b(self, impl IntoProp<f32>) -> Selfrounded-b-*
rounded_lfn rounded_l(self, impl IntoProp<f32>) -> Selfrounded-l-*
rounded_tlfn rounded_tl(self, impl IntoProp<f32>) -> Selfrounded-tl-*
rounded_trfn rounded_tr(self, impl IntoProp<f32>) -> Selfrounded-tr-*
rounded_brfn rounded_br(self, impl IntoProp<f32>) -> Selfrounded-br-*
rounded_blfn rounded_bl(self, impl IntoProp<f32>) -> Selfrounded-bl-*

The curve of every corner in a window is set once with AppConfig::corner_shape(CornerShape::Squircle). The default is CornerShape::Round.

text("Selected").px(10.0).py(4.0).rounded(999.0).ring(2.0, rgb(0x3b82f6))

Effects

MethodSignatureTailwind
shadowfn shadow(self, Shadow) -> Selfshadow-*
shadow_nonefn shadow_none(self) -> Selfshadow-none
opacityfn opacity(self, impl IntoProp<f32>) -> Selfopacity-*, a fraction from 0 to 1
backdrop_blurfn backdrop_blur(self, impl IntoProp<f32>) -> Selfbackdrop-blur-*, blurs what is behind
materialfn material(self, impl IntoProp<Material>) -> SelfCrownOS glass rendered by the compositor

Shadow has four presets, Shadow::sm(), Shadow::md(), Shadow::lg() and Shadow::xl(), and a constructor, Shadow::new(color, blur, offset). Material has Material::thin() and Material::thick(). When the compositor does not offer materials, the node is drawn with an in-app backdrop blur.

vstack(text("Glass"))
    .p(20.0)
    .rounded(20.0)
    .bg(rgba(0xffffff26))
    .backdrop_blur(24.0)
    .shadow(Shadow::xl())

Transforms

MethodSignatureTailwind
translatefn translate(self, impl IntoProp<Vector>) -> Selftranslate-* on both axes
translate_xfn translate_x(self, impl IntoProp<f32>) -> Selftranslate-x-*
translate_yfn translate_y(self, impl IntoProp<f32>) -> Selftranslate-y-*
scalefn scale(self, impl IntoProp<f32>) -> Selfscale-*, as a factor
scale_xfn scale_x(self, impl IntoProp<f32>) -> Selfscale-x-*
scale_yfn scale_y(self, impl IntoProp<f32>) -> Selfscale-y-*
rotatefn rotate(self, impl IntoProp<f32>) -> Selfrotate-*, in degrees
originfn origin(self, f32, f32) -> Selforigin-*, as a fraction of the size

Transforms do not change layout. translate_x and translate_y each set the whole translation, so the later one replaces the earlier one. Use translate(Vector::new(x, y)) to move on both axes.

Text

MethodSignatureTailwind
text_sizefn text_size(self, impl IntoProp<f32>) -> Selftext-* (size)
text_colorfn text_color(self, impl IntoProp<Color>) -> Selftext-* (color)
text_blurfn text_blur(self, impl IntoProp<f32>) -> Selfblur-* on the glyphs only
font_weightfn font_weight(self, u16) -> Selffont-* (weight), 100 to 900
font_mediumfn font_medium(self) -> Selffont-medium
font_semiboldfn font_semibold(self) -> Selffont-semibold
font_boldfn font_bold(self) -> Selffont-bold
font_familyfn font_family(self, impl Into<String>) -> Selffont-* (family)
italicfn italic(self) -> Selfitalic
leadingfn leading(self, impl IntoProp<f32>) -> Selfleading-*, a multiple of the font size
trackingfn tracking(self, impl IntoProp<f32>) -> Selftracking-*, letter spacing
text_leftfn text_left(self) -> Selftext-left (start)
text_centerfn text_center(self) -> Selftext-center
text_rightfn text_right(self) -> Selftext-right (end)
text_justifyfn text_justify(self) -> Selftext-justify
truncatefn truncate(self) -> Selftruncate, one line with an ellipsis
line_clampfn line_clamp(self, u32) -> Selfline-clamp-*

Text properties inherit. Set them on a container and every text inside picks them up unless it sets its own.

Interaction

MethodSignatureTailwind
cursorfn cursor(self, CursorIcon) -> Selfcursor-*
pointer_events_nonefn pointer_events_none(self) -> Selfpointer-events-none
pointer_events_autofn pointer_events_auto(self) -> Selfpointer-events-auto

CursorIcon has Default, Pointer, Text, Crosshair, Move, Grab, Grabbing, NotAllowed, Wait, Progress, EwResize, NsResize, NeswResize, NwseResize, ColResize and RowResize.

What each group costs

Layout methods (padding, margin, size, flex, gap, alignment, grid, position, display_none) rerun layout for the affected part of the tree when a bound value changes. Paint methods (background, border, ring, corners, shadow) skip layout and re-record the repaint boundary that holds the node. Opacity, transforms and backdrop blur also skip layout, and the render thread only composes the frame again. hidden, z and top_layer skip layout as well. Text methods reshape the text and rerun layout, because the text size can change. overflow_hidden and overflow_scroll touch both layout and paint.

The same split decides what a variant accepts. Variants take paint, transform and text properties and ignore layout properties. See variants.