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

Hi everyone,

trying to use some of Ildarā€™s great glance code to modify my card but I cannot get it to work with my card.

type: custom:auto-entities
card:
  type: grid
  columns: 4
  square: false
  title: Xiaomi Battery
card_param: cards
sort:
  method: friendly_name
filter:
  template: |-
    {% set ns = namespace(list=[]) %}  

        {% for item in states.sensor | selectattr('entity_id', 'search', 'xiaomi.*battery', ignorecase=True) %}
          {% set ns.list = ns.list + [ item ] %}
        {% endfor %}
      
      {% for item in ns.list -%}
      {{
        {
          "type": "glance",
          "card_mod": [
            {
            "style":
                $: |
                  .card-header {
                    color: red  !important;
                    }
            }
            ],
          "entities": [ 
            {
            "entity": item.entity_id,
            "name": (area_name(item.entity_id).split()[0][:2] + (area_name(item.entity_id).split()[1][:2]) if area_name(item.entity_id).split()|length > 1 else area_name(item.entity_id)[:4]),
            }
            ],
          "show_icon": false,
        }
      }},
      {%- endfor %}

Would it need to go into the for loop?

How is that related to card-mod? )

I modified my post to show the card mode attempt, sorry for that.
I think it belongs in the for loop but I am not sure if the placement is wrong or syntax.

Cannot test it now, but this is obviously wrong.
ā€œcard_modā€ is not a list.
Also, you cannot use ā€œ|ā€ here.
This should be presented as json.
Go here: link - > a link about ā€œhow to quickly create templatesā€

I will search for more info in your link, thank you! Looks interesting once again :slight_smile:

EDIT:
Okay, found your info

                  'style': ':host { {%- if states(config.entity) == "unknown" -%}
                                    --paper-item-icon-color: brown;
                                    {%- elif states(config.entity) | float <= states("input_number.battery_level_critical") | float -%}
                                    --paper-item-icon-color: red;
                                    {%- elif states(config.entity) | float <= states("input_number.battery_level_warning") | float -%}
                                    --paper-item-icon-color: rgb(250,218,67);
                                    {%- else -%}
                                    --paper-item-icon-color: green;
                                    {%- endif %} }'
                } }},

Will try to apply it

Wrong)
Try this:

"card_mod": {
  "style": "ha-card {color: red}"
},

Ah, that works.
But I was trying to do it for the title and I think that should be .card-header and there it was not working for me

"card_mod": {
  "style": ".card-header {color: red}"
},

1st create a working card-mod style w/o auto-entities.
Then make it json for auto-entites.

That I already tried:

type: glance
title: Colored title
card_mod:
  style:
    $: |
      .card-header {
        color: red  !important;
      }
entities:
  - entity: sensor.xiaomi_dining_room_battery

Same as

card_mod:
  style:
    '$': 'card-header { color: red  !important; }'

and as experienced user you now create a json)

If the $ is important, then it should be

          "card_mod": {
            "style": {
              "$": "card-header { color: red !important; }"
            }
          },

which does nothing.
But ha-card worked without the $, yet card-header will not. So still stuck :frowning:

Also tried

          "card_mod": {
            "style": {
              "'$'": ".card-header { color: red !important; }"
            }
          },

just in caseā€¦

EDIT2:
Since I also wanted to add your line breaking, I tried a different property of the card but that also failed.

          "card_mod": {
            "style": {
              ".entities": {
                ".entity": ".name { text-overflow: unset !important; white-space: unset !important; }"
                }
            }              
          },

I think the json ison itself is formatted correctly. So I am assuming my mistake is elsewhere, more general :frowning:

Check this:

filter:
  template: |-
    {% for item in states.sun -%}
    {{
      {
        "type": "glance",
        "title": "xxx",
        "card_mod": {
          "style": {
            "$": ".card-header { color: red  !important;}",
            ".": "ha-card {color: cyan}"
          }          
        },
        "entities": [ 
          {"entity": item.entity_id}
        ],
        "show_icon": false,
      }
    }},
    {%- endfor %}

ŠøŠ·Š¾Š±Ń€Š°Š¶ŠµŠ½ŠøŠµ

1 Like

Fascinating. I adopted one of your other glance examples (the multirow names) and took me 10 min to get it right.

Now I will try to figure out how to mod a grid card (well, auto-entites with template and grid). Just want to modifiy the grid cardā€™s title :slight_smile:

type: custom:auto-entities
card:
  type: grid
  columns: 4
  title: Xiaomi Battery
card_param: cards
filter:
  template: |-
...

I have started down the rabbit hole, but before I get stuck:
is the card type custom:card-mod needed for grid cards or was that just an example of yours?
I thought that card-mod was kind of available without making it a card-mod card.
From one of your excellent example posts

type: custom:mod-card
card:
  type: grid
  title: some title
  cards:

EDIT:
Baby steps. Glance card is being modded but the title of the grid itself is not being changed yet.
But at least no error message.
Or does the type: custom:mod-card have to be directly above the grid card rather than turning it all into a card-mod?

type: custom:mod-card
card:
  type: custom:auto-entities
  card:
    type: grid
    columns: 4
    square: false
    title: Xiaomi Battery
    card_mod:
      style:
        ':first-child $': |
          .card-header {
            color: red !important;
          } 
  card_param: cards
  filter:
    template: |-
...#and then the rest of the template I posted previously

Is there a way of removing the rectangles from the mushroom-person-card so I only get the circles?

image

This is what I have:

type: horizontal-stack
cards:
  - type: custom:stack-in-card
    cards:
      - type: custom:mushroom-person-card
        entity: person.ignacio_martinez
        icon_type: entity-picture
        name: Ignacio
        primary_info: none
        secondary_info: none
        fill_container: true
        tap_action:
          action: navigate
          navigation_path: '#ignacio'
        hold_action:
          action: call-service
          service: script.unmute_ignacios_pixel_5
          data: {}
          target: {}
        card_mod:
          style:
            mushroom-shape-avatar$: |
              .picture {
                display: flex;
                border-radius: 50%;
                {% if states('switch.ignacio') == 'on' %}
                  animation: pinggreen 2s infinite;
                {% else %}
                  animation: pingred 2s infinite;
                {% endif %}
              }
              @keyframes pinggreen {
                0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-green), 0.9);}
                100% {box-shadow: 0 0 5px 5px transparent;}
              } 
              @keyframes pingred {
                0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-red), 0.9);}
              .container {
                --icon-size: 80px !important;
              }
              ha-card {
                background-color: transparent;
                box-shadow: none;
              }
  - type: custom:stack-in-card
    cards:
      - type: custom:mushroom-person-card
        entity: person.ignacio_martinez
        icon_type: entity-picture
        name: Ignacio
        primary_info: none
        secondary_info: none
        fill_container: true
        tap_action:
          action: navigate
          navigation_path: '#ignacio'
        hold_action:
          action: call-service
          service: script.unmute_ignacios_pixel_5
          data: {}
          target: {}
        card_mod:
          style:
            mushroom-shape-avatar$: |
              .picture {
                display: flex;
                border-radius: 50%;
                {% if states('switch.ignacio') == 'on' %}
                  animation: pinggreen 2s infinite;
                {% else %}
                  animation: pingred 2s infinite;
                {% endif %}
              }
              @keyframes pinggreen {
                0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-green), 0.9);}
                100% {box-shadow: 0 0 5px 5px transparent;}
              } 
              @keyframes pingred {
                0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-red), 0.9);}
              .container {
                --icon-size: 80px !important;
              }
              ha-card {
                background-color: transparent;
                box-shadow: none;
              }

Thanks for the help!

Iā€™ll like to chnage vertical slider style in more-info light dialog.
Can i do that with card-mod ?

Tried this, but not working:


hold_action:
	action: more-info
name: AJUSTAR
card_mod:
	style:
	   body>home-assistant$ha-more-info-dialog$ha-dialog>div>ha-more-info-info$div>div>more-info-content$more-info-light$div.controls>ha-state-control-light-brightness$ha-control-slider: |
           {
                max-height: 200px; /*	
	       }

Dont fully understand why you are using any stack-in-card in the first place. There is only 1 card in each stack-in-card.

But this should do it:

type: horizontal-stack
cards:
  - type: custom:mushroom-person-card
    entity: person.ignacio_martinez
    icon_type: entity-picture
    name: Ignacio
    primary_info: none
    secondary_info: none
    fill_container: true
    tap_action:
      action: navigate
      navigation_path: '#ignacio'
    hold_action:
      action: call-service
      service: script.unmute_ignacios_pixel_5
      data: {}
      target: {}
    card_mod:
      style:
        mushroom-shape-avatar$: |
          .picture {
            display: flex;
            border-radius: 50%;
            {% if states('switch.ignacio') == 'on' %}
              animation: pinggreen 2s infinite;
            {% else %}
              animation: pingred 2s infinite;
            {% endif %}
          }
          @keyframes pinggreen {
            0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-green), 0.9);}
            100% {box-shadow: 0 0 5px 5px transparent;}
          } 
          @keyframes pingred {
            0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-red), 0.9);}
          .container {
            --icon-size: 80px !important;
          }
        .: |
          ha-card {
            background-color: transparent;
            box-shadow: none;
            width: fit-content !important;
          }
  - type: custom:mushroom-person-card
    entity: person.ignacio_martinez
    icon_type: entity-picture
    name: Ignacio
    primary_info: none
    secondary_info: none
    fill_container: true
    tap_action:
      action: navigate
      navigation_path: '#ignacio'
    hold_action:
      action: call-service
      service: script.unmute_ignacios_pixel_5
      data: {}
      target: {}
    card_mod:
      style:
        mushroom-shape-avatar$: |
          .picture {
            display: flex;
            border-radius: 50%;
            {% if states('switch.ignacio') == 'on' %}
              animation: pinggreen 2s infinite;
            {% else %}
              animation: pingred 2s infinite;
            {% endif %}
          }
          @keyframes pinggreen {
            0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-green), 0.9);}
            100% {box-shadow: 0 0 5px 5px transparent;}
          } 
          @keyframes pingred {
            0% {box-shadow: 0 0 5px 3px rgba(var(--rgb-red), 0.9);}
          .container {
            --icon-size: 80px !important;
          }
        .: |
          ha-card {
            background-color: transparent;
            box-shadow: none;
            width: fit-content !important;
          }

Thanks! That was pure ignorance + copy and paste. Iā€™m a slow learner :slight_smile:

One more @dimitri.landerloos . The background is transparent but I can see the border of the box as light gray. How do I make that transparent too?

          ha-card {
            background-color: transparent;
            box-shadow: none;
            width: fit-content !important;
            border: none !important;
          }
1 Like

@dimitri.landerloos Iā€™m trying to apply this logic to custom:popup-card but is not working.

title: Ignacio
size: normal
type: custom:popup-card
entity: person.ignacio_martinez
style: |
  ha-card {
    background-color: transparent;
    box-shadow: none;
    width: fit-content !important;
    border: none !important;
  }
cards:
  - type: custom:bubble-card
    card_type: separator
    name: Map
    icon: mdi:map-marker-account
dismissable: true

What am I missing?

Still trying to make a card work and now need to change some elements.
So, I got .name working but nothing else :smile: .
So line wrappibg is working.

But how do I change .entites.no-header and .entities itself?
I tried

"card_mod": {
  "style": {
    ".entities": {
      "$": ".no-header { padding: 0px 0px 0px }",
      ".entity": {
        "$": ".name { text-overflow: unset !important; white-space: unset !important; margin-bottom: 0px; padding: 0px; display: flex }",
      }
    }
  }
},

and also

"card_mod": {
  "style": {
    ".entities.no-header": {
      "$": "{padding-top: 0px}",
    },
    ".entities": {
      "$": "{padding: 0px 0px 0px}",
      ".entity": {
        "$": ".name { text-overflow: unset !important; white-space: unset !important; margin-bottom: 0px; padding: 0px; display: flex }",
      }
    }
  }
},

Or could all this hierarchy be avoided by always doing

"card_mod": {
  "style": {
    ".": "entities {}",
    ".": "entities.no-header{}",
    ".": "entities.entity.name{}",
    }
}

?
So basically just starting each div with ".": ?