Charm Bracelet
Layout & Rules

Inheritance

Inherit styles in Lip Gloss to reuse and extend formatting.

Inherit Styles Seamlessly

Styles in Lip Gloss can inherit rules from other styles, allowing you to reuse and extend formatting efficiently. When inheriting, only unset rules on the receiving style are inherited, ensuring precise control over your terminal output.

Combine inheritance with colors, formatting, layout, and borders for consistent styling.

Inheritance Options

Lip Gloss provides a simple way to inherit styles:

Inherit unset rules from another style using the Inherit method:

var styleA = lipgloss.NewStyle().
    Foreground(lipgloss.Color("229")).
    Background(lipgloss.Color("63"))

// Only the background color will be inherited here, because the foreground
// color is already set:
var styleB = lipgloss.NewStyle().
    Foreground(lipgloss.Color("201")).
    Inherit(styleA)

How It Works

Inherit styles to streamline your terminal styling:

Define a Base Style

Create a style with properties like colors or formatting using lipgloss.NewStyle():

var styleA = lipgloss.NewStyle().Foreground(lipgloss.Color("229"))

Inherit Rules

Use Inherit to apply unset rules from the base style to a new style:

var styleB = lipgloss.NewStyle().
    Foreground(lipgloss.Color("201")).
    Inherit(styleA)
fmt.Println(styleB.Render("Inherited style!"))

Control Inheritance

Only unset properties are inherited, preserving any explicitly set rules in the receiving style for precise styling.

Inheritance ensures that only unset rules are copied, preventing unintended overrides and simplifying style management in complex layouts.

How is this guide?