How to set different background color/image for cards in lovelace using card_mod?

Please help me to figure out what am I doing wrong. I want to have different colors for each card, but seems that last style applies to all previous cards.

image

I notice you’re using custom html cards. Have you tried doing this with a standard HA card, just to see what happens? Maybe a tile card?

I don’t know how to style standard HA cards, this is why I used custom html. Do you have some example?

Card mod works in much the same way (though tile cards have quite a lot of built-in options):

      - type: tile
        entity: light.study_spotlights
        icon: mdi:track-light
        vertical: false
        show_entity_picture: false
        hide_state: true
        color: '#becbf1'
        tap_action:
          action: toggle
        name: Study
        card_mod:
          style: |
            ha-card { 
              {% if is_state('light.study_spotlights', 'on') %}
              --ha-card-background: #364366;
            {% endif %} 
              } 

Why use --ha-card-background and not background?

With only 1 card, works ok, but I need 3 html cards with different background color.

This is my view configuration:

  • last HTML card overrides css for previous
  • markdown cards work ok, keep individual css settings

This is the output:

@thomasloven is this a bug or what am I doing wrong?

This is not a card-mod issue IMO.

The issue is actually the way the html-card is designed. If you stack the cards inside individual stacks, the card-mod works fine. The cards do not appear to me to be ::slotted() when in an individual stack.

type: custom:stack-in-card
cards:
  - type: custom:html-card
    title: HTML card
    content: >
      Sun state: <b>[[sun.sun]]</b>, elevation:
      [[sun.sun.attributes.elevation]]</br>
    card_mod:
      style: |
        ha-card {
          background: pink !important;
          color: black;
          }
  - type: custom:stack-in-card
    cards:
     - type: custom:html-card
       title: HTML card
       content: >
        Sun state: <b>[[sun.sun]]</b>, elevation:
        [[sun.sun.attributes.elevation]]</br>
       card_mod:
        style: |
          ha-card {
          background: green !important;
          }
                 
  - type: custom:stack-in-card
    cards:
     - type: custom:html-card
       title: HTML card
       content: >
        Sun state: <b>[[sun.sun]]</b>, elevation:
        [[sun.sun.attributes.elevation]]</br>
       card_mod:
        style: |
          ha-card {
          background: teal !important;
          }

1 Like

Another option…

Others may have better suggestions.

type: custom:vertical-stack-in-card
cards:
  - type: custom:html-card
    title: HTML card
    content: |
      Sun state: <b>[[sun.sun]]</b>, elevation: [[sun.sun.attributes.elevation]]
  - type: custom:html-card
    title: HTML card
    content: |
      Sun state: <b>[[sun.sun]]</b>, elevation: [[sun.sun.attributes.elevation]]
  - type: custom:html-card
    title: HTML card
    content: |
      Sun state: <b>[[sun.sun]]</b>, elevation: [[sun.sun.attributes.elevation]]
card_mod:
      style: |
        ha-card {
          color: black;
          border-width: 0;
          }
        :host{
           --ha-card-background: dodgerblue !important;

              }  
        :nth-child(2){
           --ha-card-background: green !important;
           color: yellow;
              }  
        :nth-child(3){
         --ha-card-background: teal !important;
         
1 Like

html-card is not using shadowDOM, so the styling is “leaky”. I think the workarounds above are the best solution.