šŸ”¹ Card-mod - Add css styles to any lovelace card

So, is this better?

card_mod: style:
              card_mod:
                style:
                  hui-generic-entity-row:
                    $: |
                      state-badge {
                        {% if is_state(config.entity,'open') %}
                          --card-mod-icon: phu:blind-tilt-open;   color: green;
                        {% elif is_state(config.entity,'closed') %}
                          --card-mod-icon: phu:blind-tilt-closed; color: beige;
                        {% else %}
                          --card-mod-icon: mdi:help-circle;       color: red;
                        {% endif %}
                      }
                    .: |
                      div.entity:nth-child(2) state-badge {
                        {% set percentage = states(config.entities[0].entity)|int(0) %}
                        {% set r, g, b = 0, 0, 0 %}
                        {% if (percentage <= 51) %} {% set r = 255 %}
                          {% set g = (5.0 * percentage) | round | int %}
                        {% else %}
                          {% set g = 255 %} {% set r = (505 - 4.89 * percentage) | round | int %}
                        {% endif %}
                        color: {{ "#%0x" | format( r * 0x10000 + g * 0x100 + b * 0x1 ) }};
                      }

Probably like this:

              card_mod:
                style:
                  hui-generic-entity-row:
                    $: |
                      state-badge { 
                        {% if is_state('cover.[[blind]]','open') %} 
                          --card-mod-icon: phu:blind-tilt-open;
                          color: green;
                        {% elif is_state('cover.[[blind]]','closed') %} 
                          --card-mod-icon: phu:blind-tilt-closed;
                          color: beige;
                        {% else %} 
                          --card-mod-icon: mdi:help-circle;
                          color: red;
                        {% endif %}
                      }
                    .: |
                      div.entity:nth-child(2) state-badge {
                        {% set percentage = states(config.entities[0].entity)|int(0) %}
                        {% set r, g, b = 0, 0, 0 %}
                        {% if (percentage <= 51) %}
                          {% set r = 255 %}
                          {% set g = (5.0 * percentage) | round | int %}
                        {% else %}
                          {% set g = 255 %}
                          {% set r = (505 - 4.89 * percentage) | round | int %}
                        {% endif %}
                        color: {{ "#%0x" | format( r * 0x10000 + g * 0x100 + b * 0x1 ) }};
                      }
2 Likes

Is this still up to date? In my case it successfully changes the colors but not the font-sizeā€¦ Thank you, a lot, for helping!

In general - yes. Ofc you need to add ā€œcard_modā€ keyword. And suggest to remove ā€œha-cardā€, no need in fact (but will work anyway).
If smth does not work - check with Code Inspector. Some DOM paths should be fixed probably.

This is my code:

  - type: custom:mod-card
    card:
      type: vertical-stack
      cards:
        - show_current: true
          show_forecast: true
          type: weather-forecast
          entity: weather.spieshome
          forecast_type: hourly
          name: ' '
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 18px;
                --card-secondary-font-size: 14px;
                --card-primary-font-weight: normal;
                background: none;
                border: none;
              }
              .name-state .name {
                color: orange;
                font-size: 20px;
              }
              .name-state .state {
                color: red;
                font-size: 10px;
              }
              .temp-attribute .temp {
                color: green;
                font-size: 10px;
              }
              .temp-attribute .temp span {
                color: blue;
                font-size: 30px;
              }
              .temp-attribute .attribute {
                color: cyan;
                font-size: 20px;
              }
              .forecast .temp {
                color: orange;
              }
              .forecast .templow {
                color: blue;
              }
              .forecast div {
                color: magenta;
              }
        - show_current: false
          show_forecast: true
          type: weather-forecast
          entity: weather.spieshome
          forecast_type: daily
          name: ' '
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 18px;
                --card-secondary-font-size: 14px;
                --card-primary-font-weight: normal;
                background: none;
                border: none;
              }
    card_mod:
      style: |
        ha-card {
          background: #2A282D;
          ha-card-box-shadow: 'inset 0px 0px 0px 0px var(--divider-color)';
          border: 1px solid #37373D;
        }

It does change color bot not font size. Same when removing

              ha-card {
                --card-primary-font-size: 18px;
                --card-secondary-font-size: 14px;
                --card-primary-font-weight: normal;
                background: none;
                border: none;
              }

Any idea?

Same idea: check with code inspector as was suggested.

Iā€™m trying to change the absolute positioning of a button within custom:expander-card and canā€™t figure out the correct path for card-mod.

JS Path

document.querySelector(ā€œbody > home-assistantā€).shadowRoot.querySelector(ā€œhome-assistant-mainā€).shadowRoot.querySelector(ā€œha-drawer > partial-panel-resolver > ha-panel-lovelaceā€).shadowRoot.querySelector(ā€œhui-rootā€).shadowRoot.querySelector(ā€œ#view > hui-view > grid-layoutā€).shadowRoot.querySelector(ā€œ#root > stack-in-card:nth-child(2)ā€).shadowRoot.querySelector(ā€œha-card > div > hui-vertical-stack-cardā€).shadowRoot.querySelector(ā€œ#root > expander-cardā€).shadowRoot.querySelector(ā€œha-card > div > buttonā€)

Card-mod-helper

ā€œbody>home-assistant$home-assistant-main$ha-drawer>partial-panel-resolver>ha-panel-lovelace$hui-root$#view>hui-view>grid-layout$#root>stack-in-card:nth-child(2)$ha-card>div>hui-vertical-stack-card$#root>expander-card$ha-card>div>buttonā€

Iā€™d like to set right: 50% to the .header-overlay class.

Iā€™ve tried the below and multiple other variations.

    card_mod:
      style: |
        ha-card > div.title-card-header-overlay > button {
          right: 50% !important;
          }

Try to just use button instead. You might need to set the position to absolute or relative as well depending on your usecase.

card_mod:
  style: |
    button {
      position: relative;
      right: 50%;
    }
1 Like

Thanks for your help. Unfortunately that doesnā€™t work.

No styles are shown in the card-mod dom either:

Is it possible to make an entities card show an input_text with like break (to ensure the entire content is shown)?
Or maybe even as an automatically scrolling text in case of overflow?

I want to change the mdc-checkboxes from ToDo-List from square to round ones, for this i want to edit the border-radius to 64px. It works within the Browser-Tool, but if i add this to Card-Mod, it won`t work. Has someone an idea why?

card_mod:
  style: |
    .mdc-checkbox__background {
    border-radius: 64px !important;
    }

HA 2 week user - trying to modify icon colour based on temperature - below not working for this one entity (it works for all if I place the code at the top of my card flush left - but I want it to work on this entity only dealing with temp) - any ideas ? (I can save the code but the icon colour doesnt change on the entity) - thanks - Julian

type: entities
entities:
  - entity: sensor.ac_loads
  - entity: sensor.backup_state
  - entity: sensor.battery_load
  - entity: sensor.battery_percent
  - entity: sensor.battery_remaining
  - entity: sensor.battery_temp
  - entity: sensor.battery_voltage
  - entity: sensor.192_168_3_230_package_id_0_temperature
    name: ProxMox PackID 0 - CPU Temp
    card_mod:
      style: |
        ha-card {
          {% if states('sensor.192_168_3_230_package_id_0_temperature') > '40' %}
          --card-mod-icon-color: red;
          {% elif states('sensor.192_168_3_230_package_id_0_temperature') < '30' %}
          --card-mod-icon-color: blue;
          {% endif %}
        } 

Comparing strings here.
Guess what is bigger: ā€œbananasā€ or ā€œappleā€.
Need to convert to a number first:

if states('sensor.192_168_3_230_package_id_0_temperature')|float(default=0) > 40

Besides, do not use ā€œha-cardā€ on a row level, use ā€œ:hostā€

thanks kindly - it works fine now with:

ā€œ:hostā€

Even with your wrong template ? )

oddly yes - I even changed the numbers for a test and saw the colour change accordingly - but what you suggested makes sense - Iā€™ll improve my code to that - thanks again

Compare ā€œ100ā€ & ā€œ19ā€ then )))

1 Like

ah yes - good point - that failed - corrected with below as per your suggestion:

  - entity: sensor.192_168_3_230_package_id_0_temperature
    name: ProxMox PackID 0 - CPU Temp
    card_mod:
      style: |
        :host {
          {% if states('sensor.192_168_3_230_package_id_0_temperature')|float(default=0) > 50 %}
          --card-mod-icon-color: red;
          {% elif states('sensor.192_168_3_230_package_id_0_temperature')|float(default=0) < 40 %}
          --card-mod-icon-color: green;
          {% endif %}
        }
1 Like

I want to switch from lovelace resource (normal installation via HACS) to frontend module by adding it as a package.
Is there a way of checking if the card-mod was loaded as lovelace resource or as frontend module?

I added a yaml file to my packages folder using

frontend:
  extra_module_url:
    - /hacsfiles/lovelace-card-mod/card-mod.js

but I donā€™t know how to check if that worked and if anything changed. It is still listed as a resource under dashboards.

Well if itā€™s listed, it was successful ā€¦