Horizontal Layout
Horizontal layouts support common UI patterns like navigation bars and button groups. The Elements horizontal layout system uses CSS Flexbox to create one-dimensional layouts that adapt to content and container constraints.
Deep dive into flexbox with the MDN Flexbox guide and this visual Flexbox Froggy game.
Understanding Horizontal Layout
Horizontal flexbox layouts distribute space along a single axis while maintaining alignment control. Unlike grid layouts that manage both dimensions, horizontal layouts focus on arranging items in a row with configurable spacing and alignment.
When to Use Horizontal Layout
Choose horizontal layout (row) for:
- Navigation bars: Logo left, menu items right
- Button groups: Actions that belong together
- Toolbars: Icon buttons with consistent spacing
- Form controls: Label and input on the same line
- Card layouts: Side-by-side content cards
- Media objects: Image with text alongside
- Split layouts: Content on one side, actions on the other
Note: horizontal layouts work well for components that need to respond to available space. Items can wrap, stretch, or maintain fixed sizes while the layout handles spacing automatically.
Basic Setup
Set nve-layout="row" on a container element to create a horizontal layout:
<!-- Simple horizontal layout -->
<nav nve-layout="row">
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
<!-- With gap spacing -->
<div nve-layout="row gap:md">
<nve-button>Save</nve-button>
<nve-button>Cancel</nve-button>
</div>
<!-- With alignment -->
<header nve-layout="row align:vertical-center align:space-between">
<nve-logo></nve-logo>
<h1>Menu items</h1>
</header>
Alignment Options
Horizontal layouts support alignment along both axes:
Primary Axis (Horizontal)
align:left- Items start from the left (default)align:horizontal-center- Items centered horizontallyalign:right- Items aligned to the rightalign:space-between- Items spread with space betweenalign:space-around- Items spread with space aroundalign:space-evenly- Items spread with even spacing
Cross Axis (Vertical)
align:top- Items aligned to topalign:vertical-center- Items centered verticallyalign:bottom- Items aligned to bottomalign:vertical-stretch- Items stretch to container height
Combined Alignment
align:center- Center both horizontally and verticallyalign:stretch- Stretch in both dimensions- You can combine many values:
align:vertical-center align:right
Special Options
align:wrap- Allow items to wrap to many lines when needed
Alignment mental model: think of horizontal layouts as having a main axis (left-to-right) and a cross axis (top-to-bottom). This helps predict how alignment values behave.
Horizontal Layout Examples
<section nve-layout="row align:...">
Align Left
Align Horizontal Center
Align Right
Align Vertical Center
Align Center
Align Vertical Center & Right
Align Left & Bottom
Align Horizontal Center & Bottom
Align Right & Bottom
Align Space Around
Align Space Between
Align Space Evenly
Align Stretch Horizontal
Align Stretch Vertical
Align Full Stretch
Align Wrap
Best Practices
- Use semantic HTML: Apply layouts to meaningful elements (
<nav>,<header>,<section>) - Let flexbox do the work: Avoid fixed widths unless necessary
- Consider mobile: Horizontal layouts may need to stack vertically on small screens
- Use gap for spacing: More maintainable than margins on individual items
- Combine alignments thoughtfully: Test how items behave with different content lengths
Accessibility note: ensure logical tab order when using visual reordering with flexbox. Screen readers follow DOM order, not visual order.
Next Steps
- Explore vertical layouts for stacked content
- Learn about grid layouts for two-dimensional control
- Understand gap and padding spacing options