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

Tile card - adding a button:

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

code
  - type: custom:mod-card
    card_mod:
      style:
        hui-horizontal-stack-card:
          $: |
            div#root {
              display: grid;
            }
            div#root > * {
              grid-column-start: 1;
              grid-row-start: 1;
              display: block !important;
            }
            div#root > *:nth-child(2) > :first-child {
              --ha-card-border-width: 0px;
              --ha-card-background: rgba(0,0,0,0.0);
              width: calc(36px + 2 * 10px);
              display: block;
              margin-left: auto;
            }
        .: |
          ha-card {
            border: none;
          }
    card:
      type: horizontal-stack
      cards:
        - type: tile
          entity: fan.virtual_fan_kitchen_test
          features:
            - type: fan-speed
        - type: tile
          entity: light.bkomnata_ceiling
          name: ' '
          card_mod:
            style:
              ha-tile-info $: |
                .info .secondary {
                  display: none !important;
                }
              .: |
                mwc-ripple {
                  display: none;
                }
                ha-card {
                  height: auto !important;
                }
1 Like

I would like to know if the add-on is correctly installed since I have some errors at the top of the page:

Browser: Firefox 119

My configuration.yaml:

frontend:
  themes: !include_dir_merge_named themes
  extra_module_url:
    - /hacsfiles/lovelace-card-mod/card-mod.js

Iā€™m trying to modify this card because in mobile view, right side is too wide and left side too narrow, so the left part doesnā€™t show the whole name and the left have too much space.

This is the card:

type: entities
entities:
  - entity: input_datetime.cambio_luz_numero
    name: Luz NĆŗmero
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_cambio_numero') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
  - entity: input_datetime.limpiafondos_piscina
    name: Limpiafondos
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_limpiafondos_piscina') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
  - entity: input_datetime.rellenado_descalcificador
    name: Descalcificador
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_rellenado_descalcificador') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
  - entity: input_datetime.cepillos_de_dientes
    name: Cepillos de Dientes
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_cepillos_de_dientes') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
  - entity: input_datetime.filtro_frigorifico
    name: Filtro FrigorĆ­fico
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_filtro_frigorifico') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
  - entity: input_datetime.cambio_filtro_ventilacion
    icon: mdi:wind-power
    name: Filtro VentilaciĆ³n
    type: custom:secondaryinfo-entity-row
    secondary_info: |-
      <b style='color:red'> 
        {% set dt = states('input_datetime.aviso_cambio_filtro_ventilacion') | as_datetime %}
        {{ dt.strftime("%d-%m-%Y") }}
      </b>
title: Recordatorios

Iā€™ve tried to add this:

        card_mod:
          style:
            ha-time-input $ ha-base-time-input $:
              ha-textfield:
                $: |
                  .mdc-text-field {
                    height: 40px !important;
                  }
              ha-select $: |
                .mdc-select__anchor {
                  height: 40px !important;
                }

To try to modify width then, but I see no changes no matter what change I made.

Can you please help me?

Hi,
is it possible to adjust the height of the text? I would like to have the text a little higher:

type: custom:mushroom-template-card
            primary: Klima
            secondary: ''
            icon: mdi:home-thermometer-outline
            layout: vertical
            icon_color: blue
            tap_action:
              action: navigate
              navigation_path: media
            card_mod:
              style: |
                ha-card { 
                  background: var(--card-background-color);
                  width: 50px;
                  height: 80px !important;
                  border-radius: 30px;
                  margin-left: auto;
                  margin-right: 20px;
                  margin-bottom: 2px;
                  --card-primary-font-size: 9px;
                  --card-secondary-font-size: 10px;
                  --secondary-text-color: white;
                  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
                }

2023-11-02 00_25_07-Window

Thank you.

you could add a secondary text and make it the color of the background.
but your icon will go up too.

type: horizontal-stack
cards:
  - type: custom:mushroom-template-card
    primary: Klima
    secondary: '.'
    icon: mdi:home-thermometer-outline
    layout: vertical
    icon_color: blue
    tap_action:
      action: navigate
      navigation_path: media
    card_mod:
      style: |
        ha-card { 
          background: var(--card-background-color);
          width: 50px;
          height: 80px !important;
          border-radius: 30px;
          margin-left: auto;
          margin-right: 20px;
          margin-bottom: 2px;
          --card-primary-font-size: 9px;
          --card-secondary-font-size: 10px;
          --secondary-text-color: var(--card-background-color);
          box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
        }
1 Like

Trying to achieve something with the shopping list card. Iā€™d like it to be flex-wrap style, something like this:

I tried to add flex-wrap: wrap in several places, it didnā€™t seem to have any effect.
Also to wrap each item from the shopping list, along with its checkbox into something that looks like a ā€œchipā€. This will allow putting a shopping list in a wider card while saving space (having more items visible before scrolling will be needed).

I used most of the card_mod tricks from here:

But it still didnā€™t help me get to the flex design I wantedā€¦

Any assistance will be highly appreciatedā€¦

Check out my guide linked in my profile. Specifically made for mushroom with card mod :slight_smile:

    mushroom-state-info {
      position: relative;
      bottom: 8px;
    }
2 Likes

Thought I would share how to use YAML anchors and card_mod as itā€™s not immediately obvious, well it wasnā€™t for me! I wanted to colour the icons on a glance card to be dependant on the value.

Putting a card_mod section on every item is a pain so you can use an Anchor to repeat a code section.

my.colors:
  - card_mod: &ref_0
      style: |
        state-badge {
        {% set val = (states(config.entity)|int) %}
        {% if val > 70 %}
        color: red;
        {% elif val > 45 %}
        color: green;
        {% else %}
        color: orange
        {% endif %}
        }
show_name: true
show_icon: true
show_state: true
type: glance
entities:
  - entity: sensor.study_humidity
    name: Study
    card_mod: *ref_0
  - entity: sensor.garage_humidity
    name: Garage
    card_mod: *ref_0
  - entity: sensor.office_humidity
    name: Office
    card_mod: *ref_0
  - entity: sensor.bedroom_humidity
    name: Bedroom
    card_mod: *ref_0
  - entity: sensor.caravan_humidity
    name: Caravan
    card_mod: *ref_0
  - entity: sensor.kitchen_humidity
    name: Kitchen
    card_mod: *ref_0
  - entity: sensor.outside_humidity
    name: Outside
    card_mod: *ref_0
  - entity: sensor.utility_humidity
    name: Utility
    card_mod: *ref_0
  - entity: sensor.bathroom_humidity
    name: Bathroom
    card_mod: *ref_0
  - entity: sensor.workshop_humidity
    name: Workshop
    card_mod: *ref_0
  - entity: sensor.living_room_humidity
    name: Living Rm
    card_mod: *ref_0
  - entity: sensor.guest_bedroom_humidity
    name: Guest Bed
    card_mod: *ref_0
title: Humidity
state_color: true

Sometimes, for a reason I donā€™t understand, HA will expand the *ref_0 to the Anchor values rather than leave it as *ref_0, which kinda defeats the objective but mostly it leaves as is. This means you can change the code/colours without having to do it to every entity.

May help someone else.

Hi, does anyone know, how to change the Tile-Background color?

When I select a color within the default options of the card,
Then the Tile Card does have a corresponding color when I hover the mouse ofer it:

image

I have implemented CardMod on some tiles, where the Icon Color should change based on the selected value.
For those cards, the background color of the tile is always the default ā€œgreyā€

image

this is my current card-mod config for the above card:

card_mod:
  style: |
    .tile {
       {% if is_state('select.openwb_lademodus', 'PV-Laden') %}
          --tile-color:var(--green-color); 
       {% elif is_state('select.openwb_lademodus', 'Sofortladen') %}
          --tile-color:var(--red-color); 
       {% elif is_state('select.openwb_lademodus', 'Standby') %}
          --tile-color:var(--blue-color); 
       {% elif is_state('select.openwb_lademodus', 'Min+PV-Laden') %}
          --tile-color:var(--lime-color); 
       {% elif is_state('select.openwb_lademodus', 'Stop') %}
          --tile-color:var(--grey-color); 
       {% endif %}
      }

Example:
Untitled video - Made with Clipchamp - 2023-11-03T132440.190

card_mod:
  style: 
    mwc-ripple$: |
      .mdc-ripple-surface::after {
        background-color: yellow !important;
        opacity: 0.5 !important;
      }
      .mdc-ripple-surface--hover::before {
        background-color: blue !important;
        opacity: 0.2 !important;
      }

The ::after is the most important one to change as this is on the click. The hover::before is with a mouse hover so isnt so important for mobile at least :slight_smile:

Opacity not needed if you only want to change the color.

Being very new to home assistant and being overwhelmed on how to add custom css with card-mod can i just ask what does the ā€˜$ā€™ represent in the code. I am trying to add a simple bottom border to my horizontal stack card but cant for the life of me work out how to do it as its inside a shadow dom and i keep seeing references to this ā€˜$ā€™ in a selector.

card_mod:
  style: |
    mwc-ripple$: |
      .mdc-ripple-surface::after {
        background-color: yellow !important;
        opacity: 0.5 !important;
      }
      .mdc-ripple-surface--hover::before {
        background-color: blue !important;
        opacity: 0.2 !important;
      }

$ means the object is in a shadow root of another so here is the example:
image
i cant refer to .mdc-ripple-surface without entering the shadow root of mwc-ripple first.

Thanks for this, i think i am slowly getting my head around it. Just out of curiosity how would i target the container vertical which is part of a shadow root below. Its a mushroom template card. On a side note what is the selector you would need to use if you were going through more than 1 shadow root to target an element

Thanks for your help, appreciate it

Should be fairly simple.

You can ignore mushroom-card because you arent going into its shadow root. So you only need to access the shadow root of mushroom-state-item

card_mod:
  style:
    mushroom-state-item$: |
      .container {
        outline: red solid 1px !important;
      }

But if you were accessing multiple shadow roots you would just do this:

card_mod:
  style:
    element1$:
      element2$: |
        final {
          color: red;
        }

Just an example not actually applying to anything.

Thank you so much for this, you have explained it really well and i think i actually get it.

One final thing, in the above example where i target mushroom-state-item$, how do i then style anything above it in the DOM tree for example ha-card? As mushroom-state-item$ is directly underneath the style in the yaml code if that makes sense.

Sure. There is a built in function for that in card mod.

So if you wanted to do both it would be with the .: |

This essentially brings you back to style: | but just keep in mind the indentation of this as it needs to be put in the same place as the other things you first put after style:

card_mod:
  style:
    mushroom-state-item$: |
      .container {
        outline: red solid 1px !important;
      }
    .: |
      ha-card {
        background: blue !important;
      }

Thats fab thank you so much for your kind help. Youā€™ve made it sound so easy :grinning: it now makes sense.

I lied this is my final question it just came to my mind, is there a way say in a horizontal stack where you have 4 cards that you want to apply all the same styles to without having to use the card_mod parameter on every card?

Yes you can use yaml anchors like this. But often when you then save and reopen the full card mod code is added rather than the anchor. But if you just want to save copying the same code a bunch of times you can.


Code before a save and reopen.

type: horizontal-stack
cards:
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod: &test
      style: |
        ha-card {
          background: blue;
        }
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod: *test
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod: *test

Code after a save and reopen:

type: horizontal-stack
cards:
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod:
      style: |
        ha-card {
          background: blue;
        }
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod:
      style: |
        ha-card {
          background: blue;
        }
  - type: tile
    entity: sensor.average_gas_kwh
    card_mod:
      style: |
        ha-card {
          background: blue;
        }

If this way that it works is not a good solution for you, i would look into the decluttering card :slight_smile:

Hi,
I am trying to customise custom bar card. Looks like I am on the right track, but canā€™t get the indicator to move. I have swapped solid color bar with image and got stuck.
here is the code:

- type: custom:bar-card
  entities:
    - entity: input_number.bs9000_sled
  style:
    left: 32.5%
    top: 50.5%
    width: 10%
  direction: up
  height: 600px
  width: 100%
  positions:
    icon: 'off'
    indicator: inside
    name: 'off'
    value: 'off'
    animation: 'on'
    speed: 5
 card_mod:
   style: |
     ha-card {
     }
     .card-content {
     padding: 0 0 0 0px;
     box-shadow: none;
     }
     ha-card {
     background: transparent;
     }
     bar-card-card {
       color: transparent;
       margin: 0px;
     }
     bar-card-background {
       background-color: transparent;
     }
     bar-card-indicator {
       background-image: url('/local/img/bs9000-sled.png');
       background-repeat: no-repeat;
       background-size: 110px 110px
       position: relative;
     }
     bar-card-value {
       margin-right: 0px;
       margin-left: 0px;
       margin-top: 0px;
       margin-bottom: 0px;
     }
     bar-card-backgroundbar {
       background: transparent;
     }
     bar-card-currentbar {
       background: transparent;
       background-repeat: no-repeat;
       background-image: url('/local/img/bs9000-sled.png');
       background-size: 110px 110px
     }

It renders fine:

TIA!

First i would post a version of this that doesnt immediately have bunch of errors due to indentation or duplicate statements:
image
after fixing the duplicate:
image
after fixing first indentation:
image

here is that fixed code.

Code
type: custom:bar-card
entities:
  - entity: input_number.bs9000_sled
style:
  left: 32.5%
  top: 50.5%
  direction: up
  height: 600px
  width: 100%
positions:
  icon: 'off'
  indicator: inside
  name: 'off'
  value: 'off'
  animation: 'on'
  speed: 5
card_mod:
 style: |
   .card-content {
     padding: 0 0 0 0px;
     box-shadow: none;
   }
   ha-card {
     background: transparent;
   }
   bar-card-card {
     color: transparent;
     margin: 0px;
   }
   bar-card-background {
     background-color: transparent;
   }
   bar-card-indicator {
     background-image: url('/local/img/bs9000-sled.png');
     background-repeat: no-repeat;
     background-size: 110px 110px
     position: relative;
   }
   bar-card-value {
     margin-right: 0px;
     margin-left: 0px;
     margin-top: 0px;
     margin-bottom: 0px;
   }
   bar-card-backgroundbar {
     background: transparent;
   }
   bar-card-currentbar {
     background: transparent;
     background-repeat: no-repeat;
     background-image: url('/local/img/bs9000-sled.png');
     background-size: 110px 110px
   }

you should be able to easily move it just by using top, bottom, right, left. you can even use negative values, and i am pretty sure the position: relative; that you have on it isnt even required.

    bar-card-indicator {
      position: relative;
      top: 10px;
    }