File: box_sizing.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 (64 lines) | stat: -rw-r--r-- 2,014 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
62
63
64
# Box-sizing

The `box-sizing` style determines how the width and height of a widget are calculated.

## Syntax

```
box-sizing: border-box | content-box;
```

### Values

| Value                  | Description                                                                                                                                                             |
|------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `border-box` (default) | Padding and border are included in the width and height. If you add padding and/or border the widget will not change in size, but you will have less space for content. |
| `content-box`          | Padding and border will increase the size of the widget, leaving the content area unaffected.                                                                           |

## Example

Both widgets in this example have the same height (5).
The top widget has `box-sizing: border-box` which means that padding and border reduce the space for content.
The bottom widget has `box-sizing: content-box` which increases the size of the widget to compensate for padding and border.

=== "Output"

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

=== "box_sizing.py"

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

=== "box_sizing.tcss"

    ```css hl_lines="2 6"
    --8<-- "docs/examples/styles/box_sizing.tcss"
    ```

## CSS

```css
/* Set box sizing to border-box (default) */
box-sizing: border-box;

/* Set box sizing to content-box */
box-sizing: content-box;
```

## Python

```python
# Set box sizing to border-box (default)
widget.box_sizing = "border-box"

# Set box sizing to content-box
widget.box_sizing = "content-box"
```

## See also

 - [`border`](./border.md) to add a border around a widget.
 - [`padding`](./padding.md) to add spacing around the content of a widget.