🔹 Card-mod - Add css styles to any lovelace card

Naamfsdfdsfdsloos

I have 4 buttons with this code

          - type: horizontal-stack 
            cards:
              - type: "custom:stack-in-card"
                cards:   
                  - type: custom:mushroom-template-card
                    primary: Energie
                    icon: mdi:alpha-e-circle-outline
                    tap_action:
                      action: call-service
                      service: input_select.select_option
                      service_data:
                        entity_id: input_select.dashboard_view  # Helper entiteit
                        option: energie
                    icon_color: white
                    fill_container: true
                    card_mod:
                      style: | 
                        ha-card {
                          box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3) !important;
                          background-color: rgba({{ states("input_text.kleur_iconen")}},0.2) !important;
                        }
                  - type: custom:mushroom-chips-card
                    chips:
                      - type: template
                        entity: climate.ruimte1
                        icon: mdi:fan
                        icon_color: white
                        tap_action:
                          action: toggle
                        card_mod:
                          style: |
                            ha-card {
                              border: none !important;
                              box-shadow: none !important;
                            }

and I have 2 questions :slight_smile:

  1. how do I get more space between the 4 buttons ?
  2. how do I achieve that the white space of the button acts like a button ? so it looks like green and white are like 1 button ?

Dear All,

Like many others I am a bit lost in trying to do what I thought was a simple operation with card-mod, but I think shadow-root has confused the socks off me!

I have installed card-mod and used it successfuly to modify icon and text colours, really cool! But now I just want to reduce the top and bottom padding on an entities card to save some vertical space.

Here is the card image:

2024-11-26_18-54-50 Entities card for wind

Here is the YAML:

type: entities
entities:
  - entity: sensor.wind
    name: " "
    icon: mdi:windsock
    card_mod:
      style: |
        :host {
          --paper-item-icon-color: 
            {% if states('sensor.wind_speed_source') == 'main' %}
              green;
            {% elif states('sensor.wind_speed_source') == 'backup' %}
              orange;
            {% else %}
              gray;
            {% endif %} !important;
        }
        .entities-row {
          display: flex;
          flex-direction: row; /* Layout items side by side */
          align-items: center; /* Align the icon and value properly */
        }
        .info {
          display: none !important; /* Hide the name completely */
        }
        .entity__name {
          display: none !important; /* Hide the name completely */
        }
        .value {
          color: 
            {% set beaufort = states('sensor.wind_scale_beaufort') | int %}
            {% if 1 <= beaufort <= 2 %}
              green;
            {% elif 3 <= beaufort <= 4 %}
              yellow;
            {% elif 5 <= beaufort <= 6 %}
              orange;
            {% elif 7 <= beaufort <= 8 %}
              red;
            {% else %}
              purple;
            {% endif %} !important;
          white-space: nowrap; /* Prevent wrapping */
          overflow: hidden; /* Prevent overflow */
          text-overflow: ellipsis; /* Handle overflow gracefully */
          flex-grow: 1; /* Ensure value takes all available space */
        }
show_header_toggle: false

I have tried various ways to reach the padding-top and padding-bottom values but to no avail.

I can change the padding value (all 4) in the Firefox developer mode and that removes the padding so I know these are the styles I want to change. I only want to change padding-top and padding-bottom, not the left and right ones.

Here is the HTML:

Any help gratefully recieved.

Many thanks, Colin

Padding is at the card level, so you need to put your card_mod styles there:

type: entities
card_mod:
  style: |
    div#states.card-content {
      padding-top: 8px;
      padding-right: 4px;
      padding-bottom: 8px;
      padding-left: 4px;
    }
entities:

If you want the same padding for all 4 sides then just padding: 4px; would do for example.

Thank you Chris so much that works perfectly.

It was the div#states.card-content that I was missing, not sure I understand what div#states.card-content does exactly but thank you all the same.

Best regards, Colin

1 Like

Glad it worked!

This is really a self-taught learner trying to explain things here, so forgive me if I don’t quite get things 100% correct, or this is stuff you already know - but if you look at your screenshot, you have the <div id="states" class="card-content"> line highlighted. That’s the level where the padding values are applied for the entities card. (Your screenshot doesn’t show the CSS values for that level, displayed in the styles window in the inspector.)

I’m no CSS expert, but I believe id’s are denoted by a hash (#), and classes by a dot (.). So your card_mod code is basically saying 'apply these padding values to the div with the id states and class card-content.

I believe the div tag is just the way an HTML document is divided into sections.

I’m not seeing any card_mod code in your YAML - isn’t this really a question for the browser_mod thread? I would think you’re more likely to get help there if so.

1 Like

Thanks Chris that does help, I couldn’t see the sense of the # and the . now I can!

Best regards, Colin

How to reuse a card-mod code
(these questions are asked rather often)

There are several ways:
– yaml-anchors
– include
– secret
– macros
– decluttering-card
– card-mod theme
– external js

Note: examples below are synthetic and may be not optimal, but they give a general idea of re-use.

1. yaml-anchors
Only can be used in yaml-mode dashboards.
Anchors are one file-wide: if you have same repeated code in 2 files - you will have to declare same anchor in both files.

type: entities
entities:
  - entity: sun.sun
    card_mod: &ref_card_mod
      style: |
        :host { color: red; }
  - entity: sun.sun
    card_mod: *ref_card_mod
  - entity: sun.sun
    card_mod: *ref_card_mod

2. include
Only can be used in yaml-mode dashboards.

type: entities
entities:
  - entity: sun.sun
    card_mod: !include ...../style.yaml

where “style.yaml” contains

  style: |
    :host { color: red; }

Note: one imported file may contain one snippet - you cannot place several definitions like

style: |
  :host { color: red; }

style: |
  :host { color: green; }

style: |
  :host { color: blue; }

and then select a particular snippet.

Also, you can kind of “pass a variable” to the imported code:

type: entities
entities:
  - entity: sun.sun
    SUPER_COLOR: red #up to you how to name this option
    card_mod: !include ...../style.yaml
style: |
  :host { color: {{ config.SUPER_COLOR }}; }

3. secret
Only can be used in yaml-mode dashboards.

type: entities
entities:
  - entity: sun.sun
    card_mod: !secret card_mod_red_row

where “secrets.yaml” contains

card_mod_red_row:
  style: |
    :host { color: red; }

Note: one secrets file may contain several snippets like

card_mod_red_row:
  style: |
    :host { color: red; }

card_mod_green_row:
  style: |
    :host { color: green; }

card_mod_blue_row:
  style: |
    :host { color: blue; }

Although it is more convenient than using “include” (where each imported file must contain one snippet) - I would not recommend using secrets for not-secret data, wrong approach.


4. macros
Can be used both in yaml-mode & storage-mode dashboards.

type: entities
entities:
  - entity: sun.sun
card_mod:
  style: |
    ha-card {
      color: red;
    }
    {% from 'card_mod_macros.jinja' import MY_SUPER_HEADER -%}
    {{ MY_SUPER_HEADER() }}

where “card_mod_macros.jinja” contains

{% macro MY_SUPER_HEADER() -%}
  .card-content {
    color: red;
    ... other styles
  }
{%- endmacro %}

And ofc you can pass variables into a macros:

type: entities
entities:
  - entity: sun.sun
card_mod:
  style: |
    ha-card {
      color: red;
    }
    {% from 'card_mod_macros.jinja' import MY_SUPER_HEADER -%}
    {{ MY_SUPER_HEADER('magenta') }}
{% macro MY_SUPER_HEADER(COLOR) -%}
  .card-header {
    color: {{ COLOR }};
    ... other styles
  }
{%- endmacro %}

Note: one jinja-file may contain several macros like

{% macro card_mod_colored_state(COLOR) -%}
  ...
{%- endmacro %}

{% macro card_mod_colored_icon(COLOR) -%}
  ...
{%- endmacro %}

{% macro card_mod_colored_name(COLOR) -%}
  ...
{%- endmacro %}

5. decluttering-card
Can be used both in yaml-mode & storage-mode dashboards. There are some complexities in case of storage-mode dashboards (not to be described here, check Docs & other info for decluttering-card).
Assume you want to have card-modded Entities card in many places:

type: entities
title: ...
entities: ...
card_mod: ...

Create a decluttering template for this Entity card:

my_template:
  card:
    type: entities
    ... use input variables to define options
    card_mod: ...you may use input variable here like a particular color

and use this template where it is needed:

type: vertical-stack
cards:
  - ...
  - type: custom:decluttering-card
    template: my_template
    variables:
      - ...

You can pass a card-mod code as an input variable:

my_template:
  default:
    - STYLE: 'ha-card { color: red; }'
  card:
    type: entities
    ...
    card_mod:
      style: '[[STYLE]]'
type: vertical-stack
cards:
  - ...
  - type: custom:decluttering-card
    template: my_template
    variables:
      - STYLE: 'ha-card { color: cyan; }'
      - ...

to replace a default red color by a cyan color.
Also, you can pass an additional card-mod code as an input variable:

my_template:
  default:
    - STYLE: ''
  card:
    type: entities
    ...
    card_mod:
      style: |
        ha-card {
          color: red;
        }
        [[STYLE]]
type: vertical-stack
cards:
  - ...
  - type: custom:decluttering-card
    template: my_template
    variables:
      - STYLE: '.card-header { color: cyan; }'
      - ...

to add additional styles.
These were simple examples with string-like styles; in case of dictionary-like styles & styles with jinja templates there are more peculiarities which are beyond this topic.


6. card-mod theme
Traditional card-modding is about styling a particular card / row / other element of a particular card.
Card-mod theme provides “global” styles like:
– all Entities cards
– all rows with toggles
(not to mention other UI elements not covered by traditional card-modding like “view layout”, “sidebar”, “header”, “more-info dialog”, …)
Also, it is possible to mass-style SOME elements like:
– some Entities cards
– some rows with toggles
by using classes:

type: entities
card_mod:
  class: yellow_card

where “yellow_card” class is defined as

card-mod-card-yaml: |
  .: |
    ha-card.yellow_card {
      ... add your styles
    }

Check Docs for card-mod about defining themes.


7. External js
Can be used to style all UI elements of a particular type/class.
Particular examples will be not provided, it is not about card-mod & an off-topic here.

4 Likes

is it possible to add a background color to one heading badge icon?

Starting point:
1st post → link at the bottom → Heading

i know your fantastic overview and checked this out but i couldn´t find a answer to my question. I want a background of a badge in the heading title.

that is why a said it is a starting point… Find a post about Heading.

Map card - some animations

m1

code
type: map
entities:
  - entity: ...
card_mod:
  style:
    ha-map $:
      ha-entity-marker $: |
        .marker {
          outline-style: solid;
          outline-width: 1px;
          outline-color: var(--accent-color);
          animation: resizing_outline 1s linear infinite;
        } 
        @keyframes resizing_outline {
          0% {
            outline-offset: 0;
          }
          25% {
            outline-offset: 5px;
          }
          50% {
            outline-offset: 10px;
          }
          75% {
            outline-offset: 15px;
          }
          100% {
            outline-offset: 20px;
          }
        }

m2

code
type: map
entities:
  - entity: ...
card_mod:
  style:
    ha-map $:
      ha-icon $: |
        ha-svg-icon {
          animation: resizing 1s linear infinite;
        }
        @keyframes resizing {
          0% {
            transform: scale(0.3,0.3);
          }
          25% {
            transform: scale(0.7,0.7);
          }
          50% {
            transform: scale(1,1);
          }
          75% {
            transform: scale(1.15,1.15);
          }
          100% {
            transform: scale(1.3,1.3);
          }
        }
6 Likes

that is working for me:

        .badges hui-heading-badge:nth-child(4) hui-entity-heading-badge $: |
          ha-state-icon {
            --mdc-icon-size: 20px;
            color: white;
            background: #1c1c1c !important;
            border-radius: 25px;
            padding: 10px;
          }

nice animations yes! always fun to customize the map.

let’s not use a target-lock…

btw, the shorthand for the outline also works:

  - type: map
    card_mod:
      style:
        ha-map $:
          ha-entity-marker $: |
            .marker {
              outline: solid 1px var(--warning-color);
              animation: resizing_outline 1s linear infinite;
            }
            @keyframes resizing_outline {
              0% {outline-offset: 0;}
              25% {outline-offset: 5px;}
              50% {outline-offset: 10px;}
              75% {outline-offset: 15px;}
              100% {outline-offset: 20px;}
            }

let’s see if we can add this to card-mod theming

Correct, and shorthand can include an offset too, just wanted to make it more “accessible” for people))

This “changing radius” animation can be useful to highlight some event; so the animation should be switched ON if some conditions are met - try to put a corr. logic in a theme.

yes, like

{% set activity = states('sensor.' ~ id ~ '_activity') %}
{%- elif activity in ['Automotive','Cycling','Walking'] %}

actually a great idea

would be something like

  - type: map
    card_mod:
      style:
        ha-map $:
          ha-entity-marker $: |
            .marker {
              {% set id = config.entity.entity_id.split('.')[1] %}
              {% set activity = states('sensor.' ~ id ~ '_activity') %}
              outline: solid 1px var(--warning-color);
              {% if activity in ['Automotive','Cycling','Walking'] %}
              animation: resizing_outline 1s linear infinite;
              {% endif %}
            }

not sure if the config.entity is picked up correctly though. it works if I add it for a person directly

cannot use this var
It will work if

type: map
entity: ... #fictitious option for themeing only
entities:
  ...

and if you have several in “entities” - the style must be applied to a particular marker (which is = “entity”)

yep, thx. will add some anchors then. but the mod needs to change in that case to the actual entity and not the map card

or, try the other style:

  - type: map
    card_mod:
      style:
        ha-map$: |
          {% for entity in config.entities %}
          {% set id = config.entity.entity_id.split('.')[1] %}
          {% set activity = states('sensor.' ~ id ~ '_activity') %}
            {% if activity not in ['Automotive','Cycling','Walking'] %}
              div:has(> ha-entity-marker[entity-id="{{ entity }}"]) {
                animation: resizing_outline 1s linear infinite;
              }
            {% endif %}
          {% endfor %}

but need to pop in the .marker element there
hmm, not that obvious I am afraid
either in the individual entity under the entities:, or in the for loop on the map entities…

Since you introduced this method of styling the entity in config.entities, could you please help me out going a bit deep to the .marker and set the outline, as Ildar explained above? I can not find the right syntax to go deeper in that ha-entity-marker $: |

setting it globally to the map card works , the challenge lies in applying it to either a single entity, or the for loop. Both need a different path

please have a look if you can find some moment