If you want to arrange children sequentially without dealing with the complexities of flexible box and grid layouts (such as shrink and placement issues), consider using linear layout. This layout is inspired by linear layout in Android.
The default layout direction of a linear layout is vertical. You can also use Web's alignment properties such as align-items
, align-self
, and justify-content
with this layout. For the supported properties, please refer to the Reference section.
display: linear
To implement a linear layout, modify the display
property of the parent element to use a linear layout for its children.
A linear layout arranges elements along the main and cross axes, similar to a flexible box layout. The main axis refers to the direction in which elements are aligned, whereas the cross axis is perpendicular to it.
You can adjust the layout direction by altering the linear-direction
property of the parent container, akin to the flex-direction
property in flexible box layouts. By default, linear-direction
is set to column
.
To control the position of child elements along the main axis, use the justify-content
property. In the demo below, the main axis is vertical.
To align items within a container along the cross axis, apply align-items
to the container or align-self
to children.
In the example below, the cross axis is vertical, with align-items: center
used in the container to center children along this axis.
By adjusting the align-self
property on a child, you can override align-items
behavior, as shown with the first block below the parent element.
Please note that when the cross-axis size of the parent element (such as the width
when linear-direction: column
) is set, and if the size of the child element in this direction is not specified (or auto
), the children's size along the cross-axis will expand to fill the container.
Linear layout allows for the linear-weight
attribute, enabling adaptive sizing along the main axis based on assigned weight ratios and available space.
Set the linear-weight
for each child to define its size share along the main axis. The parent container adjusts every child's dimensions to fit proportions derived from their respective weight values relative to available space.
In the above example, linear-weight
specifies a scale value without unit representing the amount of space available for each child element along the main axis. The three child elements will have a 0.5 : 2 : 0.5 ratio of the main axis space.
Currently, the linear layout supports the following layout properties:
Specific CSS Properties
Alignment Properties
Other Properties
Other properties such as order
, aspect-ratio
, etc., are not listed individually here; for specific property support, refer to the API Reference.