layout-card

repository·master·Indexed 22 days ago

https://github.com/thomasloven/lovelace-layout-card

A Home Assistant Lovelace plugin (version 2.4.7) providing advanced control over card placement and arrangement. It offers specialized layout engines including Masonry, Horizontal, Vertical, and Grid, and can be used as a view type or a container card. Includes utility cards like layout-break for forcing flow breaks and gap-card for inserting empty space, as well as support for CSS Grid, media queries for responsive design, and integration with auto-entities.

Tokens
6.2K
Snippets
9
Records
30
Agent score
79%

What's inside layout-card

  1. Use Vertical layout

    master

    The Vertical layout places cards into the same column as the previous card, effectively stacking them vertically within columns.

    To control placement, use the view_layout option on individual cards:

    • column: Specifies which column to place the card in. Subsequent cards will follow into the same column.

    A layout-break card can be used to force the next card to start in the next column.

  2. Use Horizontal layout

    master

    The Horizontal layout places cards into columns sequentially, looping back to the first column when the maximum number of columns is reached.

    To control placement, use the view_layout option on individual cards:

    • column: Specifies which column to place the card in. Subsequent cards will follow into the next column.

    A layout-break card can be used to force the next card to start in the first column.

  3. Use Masonry layout

    master

    The Masonry layout imitates the default Home Assistant Lovelace layout. Cards are assigned a height based on their content. One height unit is approximately 50 pixels.

    When placing cards:

    1. The layout attempts to place the card in the first column that has a total height of less than the min_height threshold.
    2. If no such column exists, it places the card in the shortest column.

    Specific Masonry option:

    • min_height: The minimum number of card height units in a column before the next column is considered (default: 5).
    layout_type: masonry
  4. Use Grid layout for full CSS Grid control

    master

    The Grid layout provides complete control by leveraging CSS Grid. It is recommended for complex layouts or when you need variable-sized columns.

    Layout Configuration (Container): Accepts any CSS Grid Container property starting with grid-, as well as grid, place-items, and place-content. It also supports margin, padding, and height.

    Card Configuration (Items): Accepts any CSS Grid Item property starting with grid-, as well as place-self via the view_layout key.

    Responsive Design: Use the mediaquery option within the layout configuration to define different grid settings for different screen sizes. Only the first matching rule is applied.

    Example Configuration:

    type: custom:grid-layout
    layout:
      grid-template-columns: auto 30px 25%
      grid-template-rows: auto
      grid-template-areas: |
        "header header header"
        "main . sidebar"
        "footer footer footer"
      mediaquery:
        "(max-width: 600px)":
          grid-template-columns: 100%
          grid-template-areas: |
            "header"
            "sidebar"
            "main"
            "footer"
        "(max-width: 800px)":
          grid-template-columns: 50% 50%
          grid-template-areas: |
            "header sidebar"
            "main main"
            "footer footer"
    cards:
      - type: entities
        view_layout:
          grid-area: header
      - type: entities
        view_layout:
          grid-area: footer
      - type: entities
        view_layout:
          grid-area: sidebar
      - type: entities
        view_layout:
          grid-area: main
  5. Configure View Layouts

    master

    You can change how cards are arranged in a view by selecting a specific layout type. layout-card provides four new view layouts:

    • custom:masonry-layout: Masonry layout.
    • custom:horizontal-layout: Horizontal layout.
    • custom:vertical-layout: Vertical layout.
    • custom:grid-layout: Grid layout.

    These can be configured via the GUI or directly in your Lovelace YAML configuration using the type and layout keys.

    views:
      - title: Home
        type: custom:masonry-layout
        layout:
          width: 300
          max_cols: 10
        cards: ...
  6. Install layout-card in Home Assistant

    master

    To install layout-card, follow the instructions in the Lovelace Plugins guide. Once installed, you can apply it to a Lovelace view via the Dashboard GUI:

    1. Go to a Lovelace view and select Edit Dashboard.
    2. Click the pencil symbol next to the view name to open view properties.
    3. Select a layout type (e.g., Masonry (layout-card)) from the View type dropdown list.
    4. Click Save.
  7. Use the horizontal layout for card arrangement

    master

    The horizontal-layout allows you to arrange cards in a horizontal sequence across columns. It uses a round-robin distribution logic where cards are placed into columns one by one.

    To control which column a specific card is placed in, you can use the column property within the view_layout configuration of an individual card. This allows you to override the default sequential placement and force a card into a specific column index (1-based).

  8. Use layout_type to select a layout

    master

    The layout_type option defines the underlying Home Assistant view type used to arrange the cards.

    • Standard types: Use names like masonry, grid, horizontal, or vertical. The card internally ensures these are treated as layout elements (e.g., masonry becomes hui-masonry-view or similar depending on context).
    • Custom types: If you have a custom layout element, you can use the custom: prefix (e.g., custom:my-special-layout). The card will strip the custom: prefix and append -layout to resolve the element name.
  9. Show or hide cards based on media queries

    master

    You can control the visibility of individual cards within a grid-layout using the view_layout.show.mediaquery configuration. If the provided media query matches the current viewport, the card is shown; otherwise, it is hidden (unless the Lovelace dashboard is in editMode).

    This is useful for creating responsive dashboards where certain cards only appear on mobile or desktop views.

  10. Configure layout-card via the UI editor

    master

    The layout-card-editor is a UI component used within Home Assistant's Lovelace editor to visually configure layout-card settings. It provides two main tabs for configuration:

    1. Layout Tab: Allows you to select the layout_type (e.g., masonry, sidebar, panel, or other options provided by LAYOUT_CARD_SELECTOR_OPTIONS) and define specific layout object options.
    2. Cards Tab: Allows you to manage the collection of cards contained within the layout. You can:
      • Add Cards: Use the card picker to add new cards to the layout.
      • Edit Cards: Select an existing card to edit its configuration using the standard Home Assistant card editor (hui-card-element-editor).
      • Reorder Cards: Move cards up or down in the stack.
      • Delete Cards: Remove a card from the layout.
      • Toggle Editor Mode: Switch between the visual GUI editor and the YAML code editor for individual cards.

    Note: If the entities parameter is set in the layout-card configuration, manual card selection in the editor is disabled.

  11. Install and use lovelace-layout-card

    master

    The lovelace-layout-card package provides several custom layout elements for Home Assistant Lovelace dashboards. By importing the package (or installing it via your package manager), the following custom elements are registered in the browser environment:

    • layout-card: The primary card used to wrap other cards in a specific layout.
    • layout-break: A card used to force a layout break (e.g., in a horizontal layout).
    • masonry-layout: A masonry-style layout.
    • horizontal-layout: A layout that arranges cards horizontally.
    • vertical-layout: A layout that arranges cards vertically.
    • grid-layout: A layout based on a grid system.

    Note: The package also includes patches for hui-card-element-editor and hui-view-editor to enhance the dashboard editing experience.

  12. Integrate layout-card with auto-entities

    master

    You can use layout-card as a wrapper for cards generated by auto-entities or entity-filter. This allows you to dynamically populate a specific layout (like Masonry) with a filtered list of entities.

    To do this, set the card option in your auto-entities configuration to a custom:layout-card and specify the layout_type.

    - type: 'custom:auto-entities'
      filter:
        include:
          - domain: light
            options:
              type: light
          - domain: sensor
      exclude: []
      card:
        type: 'custom:layout-card'
        cards: []
        layout_type: masonry