| name | gpui-styling |
| description | Styling GPUI elements with Tailwind-like utility methods. Use when applying styles, layouts, colors, spacing, or creating responsive designs. |
GPUI Styling
This skill covers styling GPUI elements using Tailwind-like utility methods.
Overview
GPUI provides chainable methods similar to Tailwind CSS:
- Sizing:
.w(), .h(), .size_full()
- Flexbox:
.flex(), .flex_col(), .items_center(), .justify_center()
- Spacing:
.p(), .m(), .gap()
- Colors:
.bg(), .text_color(), .border_color()
- Borders:
.border(), .rounded()
Sizing
Width and Height
use gpui::*;
div()
.w(px(200.0))
.h(px(100.0))
.w_full()
.h_full()
.size_full()
Relative Units
div()
.w(relative(0.5))
.h(relative(1.0))
Min/Max Sizes
div()
.min_w(px(100.0))
.max_w(px(500.0))
.min_h(px(50.0))
.max_h(px(300.0))
Flexbox Layout
Flex Direction
div()
.flex()
.flex_row()
.flex_col()
div()
.v_flex()
.h_flex()
Alignment
div()
.flex()
.items_start()
.items_center()
.items_end()
.items_stretch()
.justify_start()
.justify_center()
.justify_end()
.justify_between()
.justify_around()
Flex Properties
div()
.flex_1()
.flex_grow()
.flex_shrink()
.flex_none()
.flex_wrap()
Spacing
Padding
div()
.p(px(16.0))
.p_1()
.p_2()
.p_4()
.p_6()
.p_8()
.px(px(16.0))
.py(px(16.0))
.px_4()
.py_2()
.pt(px(8.0))
.pr(px(8.0))
.pb(px(8.0))
.pl(px(8.0))
Margin
div()
.m(px(16.0))
.m_4()
.mx(px(16.0))
.my(px(16.0))
.mx_4()
.mt(px(8.0))
.mr(px(8.0))
.mb(px(8.0))
.ml(px(8.0))
Gap
div()
.flex()
.flex_col()
.gap(px(16.0))
.gap_1()
.gap_2()
.gap_4()
.gap_6()
Colors
Background Colors
use gpui::*;
div()
.bg(rgb(0x3b82f6))
.bg(rgb(59, 130, 246))
.bg(rgba(59, 130, 246, 0.5))
.bg(hsla(0.6, 0.8, 0.6, 1.0))
Text Colors
div()
.text_color(rgb(0xffffff))
.text_color(rgba(255, 255, 255, 0.9))
Border Colors
div()
.border_color(rgb(0x4a4a4a))
Borders
Border Width
div()
.border()
.border_1()
.border_2()
.border_t()
.border_r()
.border_b()
.border_l()
Border Radius
div()
.rounded(px(4.0))
.rounded_sm()
.rounded_md()
.rounded_lg()
.rounded_full()
Typography
Font Sizes
div()
.text_xs()
.text_sm()
.text_base()
.text_lg()
.text_xl()
.text_2xl()
.text_3xl()
Font Weight
div()
.font_normal()
.font_medium()
.font_semibold()
.font_bold()
Text Alignment
div()
.text_left()
.text_center()
.text_right()
Cursor
div()
.cursor_pointer()
.cursor_default()
.cursor_text()
Hover States
div()
.bg(rgb(0x3b82f6))
.hover(|style| {
style.bg(rgb(0x2563eb))
})
Visibility
div()
.visible()
.invisible()
Common Patterns
Card Component
div()
.p_6()
.bg(rgb(0x1a1a1a))
.rounded_lg()
.border_1()
.border_color(rgb(0x2a2a2a))
.flex()
.flex_col()
.gap_4()
Button
div()
.px_6()
.py_3()
.bg(rgb(0x3b82f6))
.text_color(rgb(0xffffff))
.rounded_md()
.cursor_pointer()
.font_semibold()
.hover(|style| {
style.bg(rgb(0x2563eb))
})
Input Field
div()
.w_full()
.px_3()
.py_2()
.bg(rgb(0x1a1a1a))
.border_1()
.border_color(rgb(0x4a4a4a))
.rounded_md()
.text_base()
Navbar
div()
.w_full()
.h(px(64.0))
.px_6()
.bg(rgb(0x1a1a1a))
.flex()
.flex_row()
.items_center()
.justify_between()
.border_b_1()
.border_color(rgb(0x2a2a2a))
Centered Container
div()
.flex()
.items_center()
.justify_center()
.size_full()
Grid Layout
div()
.flex()
.flex_wrap()
.gap_4()
.children((0..12).map(|i| {
div()
.w(px(200.0))
.h(px(150.0))
.bg(rgb(0x3b82f6))
.rounded_md()
}))
Color Utilities
Common Colors
rgb(0x1a1a1a)
rgb(0x2a2a2a)
rgb(0x4a4a4a)
rgb(0x9ca3af)
rgb(0xf3f4f6)
rgb(0x3b82f6)
rgb(0x2563eb)
rgb(0x60a5fa)
rgb(0x22c55e)
rgb(0x16a34a)
rgb(0xef4444)
rgb(0xdc2626)
rgb(0xf59e0b)
HSLA Colors
hsla(0.0, 0.0, 0.1, 1.0)
hsla(0.6, 0.8, 0.5, 1.0)
hsla(0.33, 0.7, 0.5, 1.0)
hsla(0.0, 0.8, 0.6, 1.0)
hsla(0.15, 0.9, 0.5, 1.0)
Theme System
Constant Colors
pub const BG_PRIMARY: Hsla = hsla(0.0, 0.0, 0.05, 1.0);
pub const BG_SECONDARY: Hsla = hsla(0.0, 0.0, 0.1, 1.0);
pub const TEXT_PRIMARY: Hsla = hsla(0.0, 0.0, 0.9, 1.0);
pub const TEXT_SECONDARY: Hsla = hsla(0.0, 0.0, 0.6, 1.0);
pub const ACCENT: Hsla = hsla(0.6, 0.8, 0.5, 1.0);
div()
.bg(theme::BG_PRIMARY)
.text_color(theme::TEXT_PRIMARY)
Best Practices
- Use spacing scale: Stick to
.p_1(), .p_2(), .p_4(), etc. for consistency
- Define color constants: Create a theme module with color constants
- Use HSLA for theming: Easier to adjust lightness/saturation
- Combine utilities: Chain methods for concise styling
- Extract common styles: Create reusable components for repeated patterns
Summary
- Use
.size_full() for full width/height
- Use
.v_flex() for vertical layouts, .h_flex() for horizontal
- Use spacing utilities
.p_4(), .gap_2() for consistent spacing
- Use
.bg(), .text_color() for colors
- Chain methods for concise styling
- Use
.hover() for interactive states
References