Insert a multi-element container.
Inserts an invisible container into your app that can be used to hold multiple elements. This allows you to, for example, insert multiple elements into your app out of order.
To add elements to the returned container, you can use the with notation (preferred) or just call commands directly on the returned object. See examples below.
Function signature[source] | |
---|---|
st.container(*, border=None, key=None, width="stretch", height="content", horizontal=False, horizontal_alignment="left", vertical_alignment="top", gap="small") | |
Parameters | |
border (bool or None) | Whether to show a border around the container. If None (default), a border is shown if the container is set to a fixed height and not shown otherwise. |
key (str or None) | An optional string to give this container a stable identity. Additionally, if key is provided, it will be used as CSS class name prefixed with st-key-. |
width ("stretch" or int) | The width of the container. This can be one of the following:
|
height ("content", "stretch", or int) | The height of the container. This can be one of the following:
Note Use scrolling containers sparingly. If you use scrolling containers, avoid heights that exceed 500 pixels. Otherwise, the scroll surface of the container might cover the majority of the screen on mobile devices, which makes it hard to scroll the rest of the app. |
horizontal (bool) | Whether to use horizontal flexbox layout. If this is False (default), the container's elements are laid out vertically. If this is True, the container's elements are laid out horizontally and will overflow to the next line if they don't fit within the container's width. |
horizontal_alignment ("left", "center", "right", or "distribute") | The horizontal alignment of the elements inside the container. This can be one of the following:
|
vertical_alignment ("top", "center", "bottom", or "distribute") | The vertical alignment of the elements inside the container. This can be one of the following:
|
gap ("small", "medium", "large", or None) | The minimum gap size between the elements inside the container. This can be one of the following:
The rem unit is relative to the theme.baseFontSize configuration option. The minimum gap applies to both the vertical and horizontal gaps between the elements. Elements may have larger gaps in one direction if you use a distributed horizontal alignment or fixed height. |
Examples
Example 1: Inserting elements using ``with`` notation
You can use the with statement to insert any element into a container.
Example 2: Inserting elements out of order
When you create a container, its position in the app remains fixed and you can add elements to it at any time. This allows you to insert elements out of order in your app. You can also write to the container by calling commands directly on the container object.
Example 3: Grid layout with columns and containers
You can create a grid with a fixed number of elements per row by using columns and containers.
Example 4: Vertically scrolling container
You can create a vertically scrolling container by setting a fixed height.
Example 5: Horizontal container
You can create a row of widgets using a horizontal container. Use horizontal_alignment to specify the alignment of the elements.
Still have questions?
Our forums are full of helpful information and Streamlit experts.