Class Spacer


public class Spacer extends StyledElement<Spacer>
A flexible spacer element that expands to fill available space in a flex container.

Spacer uses flex-grow: 1 to consume all available space along the main axis of its parent flex container. This is useful for pushing elements apart, creating dynamic layouts, or filling remaining space.

Common Use Cases:

  • Push items to opposite ends of a container
  • Create equal spacing between groups of items
  • Fill remaining vertical space in a column layout
  • Center content with flexible margins

Usage:


 // Push logo and menu to opposite ends
 HStack navbar = new HStack();
 navbar.addElement(logo);
 navbar.addElement(new Spacer());
 navbar.addElement(menuItems);

 // Push footer to bottom of page
 VStack page = new VStack();
 page.setHeight("100vh");
 page.addElement(header);
 page.addElement(content);
 page.addElement(new Spacer());
 page.addElement(footer);

 // Create three-column layout with spacers
 HStack layout = new HStack();
 layout.addElement(leftPanel);
 layout.addElement(new Spacer());
 layout.addElement(centerPanel);
 layout.addElement(new Spacer());
 layout.addElement(rightPanel);

 // Fixed-size spacer (16 pixels)
 layout.addElement(new Spacer(16));
 
Since:
2025
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Spacer

      public Spacer()
      Constructs a flexible spacer that expands to fill available space.

      The spacer uses flex-grow: 1 to consume all available space along the main axis of its parent flex container.

    • Spacer

      public Spacer(int size)
      Constructs a fixed-size spacer with the specified size in pixels.

      This creates a non-flexible spacer that maintains a fixed size. The size applies to both width and height, but only the dimension along the main axis of the parent flex container will be visible.

      Parameters:
      size - the fixed size in pixels
    • Spacer

      public Spacer(String size)
      Constructs a fixed-size spacer with the specified CSS size value.

      This creates a non-flexible spacer that maintains a fixed size. The size applies to both width and height, but only the dimension along the main axis of the parent flex container will be visible.

      Parameters:
      size - the fixed size (e.g., "1rem", "16px", "2em")
    • Spacer

      public Spacer(String width, String height)
      Constructs a fixed-size spacer with separate width and height values.

      This is useful when you need different horizontal and vertical spacing, or when the spacer is used in contexts where both dimensions matter.

      Parameters:
      width - the width value (e.g., "100px", "auto")
      height - the height value (e.g., "50px", "2rem")