| name | gpui-layout-and-style |
| description | Layout and styling in GPUI. Use when styling components, layout systems, or CSS-like properties. |
Overview
GPUI provides CSS-like styling with Rust type safety.
Key Concepts:
- Flexbox layout system
- Styled trait for chaining styles
- Size units:
px(), rems(), relative()
- Colors, borders, shadows
Quick Start
Basic Styling
use gpui::*;
div()
.w(px(200.))
.h(px(100.))
.bg(rgb(0x2196F3))
.text_color(rgb(0xFFFFFF))
.rounded(px(8.))
.p(px(16.))
.child("Styled content")
Flexbox Layout
div()
.flex()
.flex_row()
.gap(px(8.))
.items_center()
.justify_between()
.children([
div().child("Item 1"),
div().child("Item 2"),
div().child("Item 3"),
])
Size Units
div()
.w(px(200.))
.h(rems(10.))
.w(relative(0.5))
.min_w(px(100.))
.max_w(px(400.))
Common Patterns
Centered Content
div()
.flex()
.items_center()
.justify_center()
.size_full()
.child("Centered")
Card Layout
div()
.w(px(300.))
.bg(cx.theme().surface)
.rounded(px(8.))
.shadow_md()
.p(px(16.))
.gap(px(12.))
.flex()
.flex_col()
.child(heading())
.child(content())
Responsive Spacing
div()
.p(px(16.))
.px(px(20.))
.py(px(12.))
.pt(px(8.))
.gap(px(8.))
Styling Methods
Dimensions
.w(px(200.))
.h(px(100.))
.size(px(200.))
.min_w(px(100.))
.max_w(px(400.))
Colors
.bg(rgb(0x2196F3))
.text_color(rgb(0xFFFFFF))
.border_color(rgb(0x000000))
Borders
.border(px(1.))
.rounded(px(8.))
.rounded_t(px(8.))
.border_color(rgb(0x000000))
Spacing
.p(px(16.))
.m(px(8.))
.gap(px(8.))
Flexbox
.flex()
.flex_row()
.flex_col()
.items_center()
.justify_between()
.flex_grow()
Theme Integration
div()
.bg(cx.theme().surface)
.text_color(cx.theme().foreground)
.border_color(cx.theme().border)
.when(is_hovered, |el| {
el.bg(cx.theme().hover)
})
Conditional Styling
div()
.when(is_active, |el| {
el.bg(cx.theme().primary)
})
.when_some(optional_color, |el, color| {
el.bg(color)
})
Reference Documentation
- Complete Guide: See reference.md
- All styling methods
- Layout strategies
- Theming, responsive design