Files
textual/docs/styles/border.md
Rodrigo Girão Serrão 559f976f78 Add more consistent phrasing.
Related issues: #2107
2023-03-23 11:04:17 +00:00

4.0 KiB

Border

The border style enables the drawing of a box around a widget.

!!! note

Due to a Textual limitation, [`border`](./border.md) and [`outline`](./outline.md) cannot coexist in the same edge of a widget.

Syntax

--8<-- "docs/snippets/syntax_block_start.md" border: [<border>] [<color>] [<percentage>];

border-top: [<border>] [<color>] [<percentage>]; border-right: [<border>] [<color> [<percentage>]]; border-bottom: [<border>] [<color> [<percentage>]]; border-left: [<border>] [<color> [<percentage>]]; --8<-- "docs/snippets/syntax_block_end.md"

The border style accepts an optional <border> that sets the visual style of the widget border, an optional <color> to set the color of the border, and an optional <percentage> to specify the color transparency.

Borders may also be set individually for the four edges of a widget with the border-top, border-right, border-bottom and border-left rules.

Multiple edge rules

If multiple border styles target the same edge, the last style that targets a specific edge is the one that is applied to that edge. For example, consider the CSS below:

Static {
    border-top: dashed red;
    border: solid green;  /* overrides the border-top rule above */
    /* Change the border but just for the bottom edge: */
    border-bottom: double blue;
}

The CSS snippet above will add a solid green border around Static widgets, except for the bottom edge, which will be double blue.

Defaults

If <color> is specified but <border> is not, it defaults to "solid". If <border> is specified but <color>is not, it defaults to green (RGB color "#00FF00"). If <percentage> is not specified it defaults to 100%.

Border command

The textual CLI has a subcommand which will let you explore the various border types interactively:

textual borders

Alternatively, you can see the examples below.

Examples

Basic usage

This examples shows three widgets with different border styles.

=== "Output"

```{.textual path="docs/examples/styles/border.py"}
```

=== "border.py"

```python
--8<-- "docs/examples/styles/border.py"
```

=== "border.css"

```sass hl_lines="4 10 16"
--8<-- "docs/examples/styles/border.css"
```

All border types

The next example shows a grid with all the available border types.

=== "Output"

```{.textual path="docs/examples/styles/border_all.py"}
```

=== "border_all.py"

```py hl_lines="2 6 10 14 18 22 26 30 34 38 42 46 50 54 58"
--8<-- "docs/examples/styles/border_all.py"
```

=== "border_all.css"

```sass
--8<-- "docs/examples/styles/border_all.css"
```

Borders and outlines

--8<-- "docs/snippets/border_vs_outline_example.md"

CSS

/* Set a heavy white border */
border: heavy white;

/* Set a red border on the left */
border-left: outer red;

/* Set a rounded orange border, 50% transparency. */
border: round orange 50%;

Python

# Set a heavy white border
widget.styles.border = ("heavy", "white")

# Set a red border on the left
widget.styles.border_left = ("outer", "red")

See also

  • box-sizing to specify how to account for the border in a widget's dimensions.
  • outline to add an outline around the content of a widget.