Skip to content

Layouts

Spacer

Bit.BlazorUI

The purpose of the BitSpacer is to generate space between other components inside a flex container. Left on its own it is flexible: it swallows the leftover room of the container and pushes its neighbours apart, and Grow shares that room out between several spacers in proportion, while MinGap puts a floor under it so its neighbours never touch. Given Width, Height, Gap or Size it becomes a fixed space instead, in pixels, in any CSS length or on the spacing scale of the theme, on the horizontal or the vertical axis. The element it renders is empty and hidden from assistive technologies.

Usage

Every example is live. Open its code to see exactly what produced the component running underneath.

Basic

A spacer with no parameters is flexible: it takes every pixel the other children of the flex container did not need and pushes them apart. One spacer sends the items on either side of it to the two ends of the container, and several spacers split the leftover room evenly, which is how the title below ends up centered between the two buttons without a single alignment rule. Keep in mind that the spacer is a real child of the container, so in a container that also sets a CSS gap (a BitStack, for instance) that gap is added on both sides of it.
Title

Start
End

Fixed size

Width turns the spacer into a fixed gap of that many pixels, and Gap does the same with any CSS length, so design tokens, relative units and percentages all work (1rem, 5%, var(--my-gap)). A fixed spacer no longer grows. The space is created with a logical margin rather than a width, so it never shrinks when the container gets cramped and it follows the text direction of the page.

1rem
4ch
10%
end

Vertical

In a column layout the horizontal Width creates no visible space, so Height is the pixel parameter to reach for, and Vertical moves the axis-agnostic parameters - Gap, Size and MinGap - over to the vertical axis too. A flexible spacer needs neither: with no fixed size it always grows along the main axis of its container, which is why the last item below is pinned to the bottom.
Height 8
Height 32
Vertical Gap 3rem
Flexible
Bottom

Grow

Grow is the flex-grow factor of a flexible spacer and defaults to 1. When several spacers share a container, the leftover room is split between them in proportion to their factors, so a 1 and a 2 below give a one third / two thirds split. Any CSS flex-grow value is accepted, fractions included.
A
B
C

A
B
C

Minimum size

A flexible spacer collapses to nothing as soon as its siblings fill the container, which leaves them touching. MinGap puts a floor under it in any CSS length, so the gap survives even when there is no room left to share. Drag the resize handle at the bottom-right corner of the first box to squeeze it: the spacer with MinGap keeps its 2rem, the one without it disappears. It follows the axis of Vertical like Gap does, and since only a flexible spacer can collapse in the first place, a spacer that was given a fixed size ignores it.
MinGap
2rem

No
MinGap

Element

The spacer renders a div, which is not a valid child everywhere: a toolbar or a menu built as a list only accepts li elements. Element swaps the tag of the rendered root for any other one, so the spacer can join such a container without breaking its markup, while everything else about it (the space it generates, the classes and the styles) stays exactly the same.
  • Home
  • Products
  • Sign in

Size

Size is the fixed space picked from the spacing scale of the theme instead of being written out by hand, in the usual three steps: Small, Medium and Large. It is the parameter to reach for when the spacers of an application should stay on the rhythm of its theme, since the length it resolves to follows the spacing unit and the density of the current theme and changes along with them. It sits on the same axis rules as Gap - inline by default, block once Vertical is set - and the more specific Gap, Width and Height win over it.
Small
Medium
Large
End

Vertical Small
Vertical Large
End

Style & Class

The spacer renders a single empty element, so Style and Class land straight on it. Since the generated space is a margin, the element itself has no size along the main axis: give it a background and a min-width or a border, as the examples below do, to make it visible while laying a page out. It stretches along the cross axis of its container on its own, so such a decoration shows up even in a container that centers its items. Inline styles are appended after the ones the component generates, so they win over them. That element is also marked aria-hidden, so screen readers skip the empty box entirely unless an AriaLabel is given, which takes the marking back.
Style
Class
End

RTL

The fixed space is a logical inline-start margin, so it flips with the text direction on its own: mark the container as right-to-left and the same markup lays out mirrored, with the space created on the right-hand side of the spacer. Because the margin resolves against the direction of the spacer itself, Dir on a single spacer moves its space to its other side without touching the rest of the row: the two bottom rows are the same markup, and the tinted spacer sits after the gap in the first one and before it in the second. A flexible spacer is direction-agnostic and behaves identically either way.
شروع
میانه
پایان

Start
Middle

Start
Middle

API

Every parameter, public member, sub-class and enum this component exposes.

BitSpacer parameters

Name Type Default value Description
Element string? null Gets or sets the custom html element used for the root node, "div" by default. This is what lets a spacer sit in a container that only accepts specific children, such as an "li" inside a list-based toolbar or menu, without breaking the markup of that container.
Gap string? null Gets or sets the fixed amount of space the spacer generates, using any CSS length value (for example "1rem", "5%", or "var(--my-gap)"). The space is created along the inline (horizontal) axis, or along the block (vertical) axis when Vertical is set. It takes precedence over Width, Height and Size on the axis it targets, and like them it turns the spacer from a flexible one into a fixed one, so Grow and MinGap no longer apply.
Grow string? null Gets or sets how much of the leftover space of the container this spacer takes compared to its siblings (the CSS flex-grow factor), 1 by default. Two spacers with the factors "1" and "2" split the leftover space of the container one third to two thirds. It is ignored as soon as the spacer is given a fixed size through Gap, Width, Height or Size.
Height int? null Gets or sets the fixed amount of space the spacer generates along the block (vertical) axis, in pixels. This is the parameter to reach for inside a vertical (column) layout, where the inline-axis Width creates no visible space. Setting it turns the spacer from a flexible one into a fixed one, so Grow and MinGap no longer apply.
MinGap string? null Gets or sets the smallest amount of space a flexible spacer keeps when the container runs out of room, using any CSS length value. It applies to the inline (horizontal) axis, or to the block (vertical) axis when Vertical is set. It only concerns a flexible spacer, so it is ignored as soon as the spacer is given a fixed size through Gap, Width, Height or Size.
Size BitSize? null Gets or sets the fixed amount of space the spacer generates, picked from the spacing scale of the theme, so the resulting length follows the spacing unit and the density of the current theme instead of being hardcoded. It is created along the inline (horizontal) axis, or along the block (vertical) axis when Vertical is set, and like the other fixed sizes it turns the spacer from a flexible one into a fixed one, so Grow and MinGap no longer apply. Gap, Width and Height are more specific, so they take precedence over it.
Vertical bool false Gets or sets a value indicating whether Gap, Size and MinGap apply to the block (vertical) axis instead of the inline (horizontal) one. A flexible spacer does not need this parameter: it always grows along the main axis of its container, whichever that is.
Width int? null Gets or sets the fixed amount of space the spacer generates along the inline (horizontal) axis, in pixels. The space follows the text direction of the spacer, so in a right-to-left context it is created on the right side. Setting it turns the spacer from a flexible one into a fixed one, so Grow and MinGap no longer apply.

BitComponentBase parameters

Name Type Default value Description
AriaLabel string? null Gets or sets the accessible label for the component, used by assistive technologies.
Class string? null Gets or sets the CSS class name(s) to apply to the rendered element.
Dir BitDir? null Gets or sets the text directionality for the component's content.
ForceAnimation bool false Gets or sets a value indicating whether the component's animations play at their full duration even when reduced motion is requested.
HtmlAttributes Dictionary<string, object> new Dictionary<string, object>() Captures additional HTML attributes to be applied to the rendered element, in addition to the component's parameters.
Id string? null Gets or sets the unique identifier for the component's root element.
IsEnabled bool true Gets or sets a value indicating whether the component is enabled and can respond to user interaction.
Style string? null Gets or sets the CSS style string to apply to the rendered element.
TabIndex string? null Gets or sets the tab order index for the component when navigating with the keyboard.
Visibility BitVisibility BitVisibility.Visible Gets or sets the visibility state (visible, hidden, or collapsed) of the component.

BitComponentBase public members

Name Type Default value Description
UniqueId Guid Guid.NewGuid() Gets the readonly unique identifier for the component's root element, assigned when the component instance is constructed.
RootElement ElementReference Gets the reference to the root HTML element associated with this component.

BitSize enum

Name Value Description
Small 0 The small size.
Medium 1 The medium size.
Large 2 The large size.

BitVisibility enum

Name Value Description
Visible 0 The content of the component is visible.
Hidden 1 The content of the component is hidden, but the space it takes on the page remains (visibility:hidden).
Collapsed 2 The component is hidden (display:none).

BitDir enum

Name Value Description
Ltr 0 Ltr (left to right) is to be used for languages that are written from the left to the right (like English).
Rtl 1 Rtl (right to left) is to be used for languages that are written from the right to the left (like Arabic).
Auto 2 Auto lets the user agent decide. It uses a basic algorithm as it parses the characters inside the element until it finds a character with a strong directionality, then applies that directionality to the whole element.

Feedback

Found a mistake, a gap, or something that could be clearer? Every page and every component is one click from its source.