File: max_width.md

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (61 lines) | stat: -rw-r--r-- 1,502 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Max-width

The `max-width` style sets a maximum width for a widget.

## Syntax

--8<-- "docs/snippets/syntax_block_start.md"
max-width: <a href="../../css_types/scalar">&lt;scalar&gt;</a>;
--8<-- "docs/snippets/syntax_block_end.md"

The `max-width` style accepts a [`<scalar>`](../css_types/scalar.md) that defines an upper bound for the [`width`](./width.md) of a widget.
That is, the width of a widget is never allowed to exceed `max-width`.

## Example

The example below shows some placeholders that were defined to span horizontally from the left edge of the terminal to the right edge.
Then, we set `max-width` individually on each placeholder.

=== "Output"

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

=== "max_width.py"

    ```py
    --8<-- "docs/examples/styles/max_width.py"
    ```

=== "max_width.tcss"

    ```css hl_lines="12 16 20 24"
    --8<-- "docs/examples/styles/max_width.tcss"
    ```

    1. This won't affect the placeholder because its width is less than the maximum width.

## CSS

```css
/* Set the maximum width to 10 rows */
max-width: 10;

/* Set the maximum width to 25% of the viewport width */
max-width: 25vw;
```

## Python

```python
# Set the maximum width to 10 rows
widget.styles.max_width = 10

# Set the maximum width to 25% of the viewport width
widget.styles.max_width = "25vw"
```

## See also

 - [`min-width`](./min_width.md) to set a lower bound on the width of a widget.
 - [`width`](./width.md) to set the width of a widget.