Skip to content

Container

A container with a reusable style.

ui.container

Basic

let t = ui.theme();
let style = Style::column()
.w_fill()
.p(14)
.gap(2)
.rounded(12)
.border(1, t.accent)
.bg(t.surface)
.shadow(12, 2, t.shadow);
ui.container(style).children(|ui| {
ui.text("Custom styled container").font_bold();
ui.text("p(14) · rounded(12) · border · shadow").text_sm().muted();
});

Theme shorthands

let padded = Style::row().p(12);
ui.row().p(16).gap(16).children(|ui| {
ui.container(padded).bordered().rounded_md().children(|ui| {
ui.text("Bordered");
});
ui.container(padded).bg_muted().rounded_md().children(|ui| {
ui.text("Muted");
});
ui.container(padded).bg_surface().rounded_md().shadow_md().children(|ui| {
ui.text("Shadow");
});
});

Aspect ratio: height follows width

// The same width at three ratios: only the height differs.
let muted = ui.theme().muted;
ui.row().gap(16).items_end().children(|ui| {
for (label, ratio) in
[("16 : 9", 16.0 / 9.0), ("4 : 3", 4.0 / 3.0), ("1 : 1", 1.0)]
{
ui.column().gap(6).items_center().children(|ui| {
ui.row()
.w(112)
.aspect_ratio(ratio)
.rounded_md()
.bg(muted)
.children(|_| {});
ui.text(label).text_sm().muted();
});
}
});

API reference

SettingDescription
.style(style: Style)Replaces the whole style. Later methods adjust it.
.rounded_sm()The theme's radius_sm.
.rounded_md()The theme's control radius.
.rounded_lg()The theme's radius_lg.
.rounded_full()Fully rounded ends.
.bordered()A 1 logical pixel border in the theme's border color.
.bg_muted()Fills with the theme's muted color, for quiet areas.
.bg_surface()Fills with the theme's surface color, as cards and popups do.
.shadow_sm()A small theme-colored shadow.
.shadow_md()A medium theme-colored shadow, as on cards.
.shadow_lg()A large theme-colored shadow, as on popovers and dialogs.
.text_color(color: Color)The color of text and icons inside, unless they set their own.
.muted()Gives text and icons inside the muted_foreground color.
.text_sm()Gives text inside the theme's small font size.
.text_size(size: impl Length)The font size of text inside, in logical pixels.
.disabled(disabled: bool)Disables every widget inside when true: dimmed, no pointer or keyboard, skipped by Tab.
.when(condition: bool, f: impl FnOnce(Self)Applies f when condition is true.
.when_some(value: Option<T>, f: impl FnOnce(Self, T)Applies f to the value when there is one.
.hover(f: impl FnOnce(Style)The look while the pointer is over the container or a widget inside. f may change the background, border, radius, shadow, ring or opacity, eased over the theme's motion duration. Call it after the other style methods. Scroll areas and overlays ignore it.
.active(f: impl FnOnce(Style)The look while the primary button is held on the container or a widget inside, on top of hover.
.focus_within(f: impl FnOnce(Style)The look while a control inside has the keyboard focus.
.children(f: impl FnOnce(&mut Ui<'a>)Places the container with the children declared by f, and returns the result of f.
.children_with_response(f: impl FnOnce(&mut Ui<'a>)Like children, also returning the container's interaction result. It is hovered and pressed while the pointer is over it or a widget inside. A click on a widget inside belongs to that widget.
.w(width: impl Into<Sizing>)Sets the width rule.
.h(height: impl Into<Sizing>)Sets the height rule.
.size(width: impl Into<Sizing>, height: impl Into<Sizing>)Sets the width and height rules.
.w_fill()Takes the remaining width (Sizing::FILL).
.h_fill()Takes the remaining height.
.fill()Takes the remaining width and height.
.min_w(size: impl Length)Sets the minimum width, which overrides the width rule and content.
.max_w(size: impl Length)Sets the maximum width, wrapping text to it. min_w wins.
.min_h(size: impl Length)Sets the minimum height, which overrides the height rule and content.
.max_h(size: impl Length)Sets the maximum height. min_h wins over it.
.p(padding: impl Length)Sets all padding.
.px(padding: impl Length)Sets the horizontal padding.
.py(padding: impl Length)Sets the vertical padding.
.pt(padding: impl Length)Sets the top padding.
.pr(padding: impl Length)Sets the right padding.
.pb(padding: impl Length)Sets the bottom padding.
.pl(padding: impl Length)Sets the left padding.
.padding(padding: Edges)Sets each side's padding.
.gap(gap: impl Length)Sets the spacing between children and between wrapped lines.
.justify_start()Places children at the start (default).
.justify_center()Centers children along the layout direction.
.justify_end()Places children at the end.
.justify_between()Spreads free space between children (Align::Between).
.justify_around()Spreads free space around children (Align::Around).
.justify_evenly()Spreads free space evenly (Align::Evenly).
.items_baseline()Aligns text baselines in a row (Align::Baseline).
.self_start()Aligns itself to the start across the layout (top in a stack).
.self_center()Centers itself across the layout (vertically in a stack).
.self_end()Aligns itself to the end across the layout (bottom in a stack).
.self_stretch()Stretches itself across the layout (vertically in a stack) if its size there is not fixed.
.justify_self_start()Aligns itself left in a stack. Ignored elsewhere.
.justify_self_center()Centers itself horizontally in a stack. Ignored elsewhere.
.justify_self_end()Aligns itself right in a stack. Ignored elsewhere.
.aspect_ratio(ratio: f32)Sets the width to height ratio.
.gap_x(gap: impl Length)Sets the horizontal gap, overriding gap.
.gap_y(gap: impl Length)Sets the vertical gap, overriding gap.
.layout_priority(priority: f32)Sets the priority for keeping width when a row is too narrow.
.col_span(columns: u32)Spans columns columns of its grid.
.offset(x: impl Length, y: impl Length)Shifts the element right and down, or back with negative values. Nothing else moves.
.items_start()Aligns children to the start across the layout (default for columns).
.items_center()Centers children across the layout (default for rows).
.items_end()Aligns children to the end across the layout.
.items_stretch()Stretches children across the layout unless their size there is fixed.
.center()Centers children both ways.
.flex_row()Lays out horizontally.
.flex_col()Lays out vertically.
.flex_wrap()Wraps a full row.
.bg(color: Color)Sets the background color.
.rounded(radius: impl Length)Sets the corner radius. Above half the height, the ends are fully round.
.border(width: impl Length, color: Color)Sets a border drawn inside the edges.
.overflow_hidden()Clips children beyond the node's bounds.
.shadow(blur: impl Length, offset: impl Length, color: Color)Sets a shadow.
.ring(width: impl Length, color: Color)Sets the focus ring outside the border.
.opacity(opacity: f32)Sets the subtree opacity (0–1).