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

I am actually baffled that not a single person has the answer. Are you and I (and a very very few guys on github) the only ones in the entire community that use position: sticky ? I really find that hard to believe. I asked everywhere, but 99,9% of the people that saw the post just ignore it or give a workaround with `position: fixed’, which is still working but as mentioned, it is a workaround and not really a solution.

I also do not quite understand why it is no longer working whereas position: fixed, position: absolute and position: relative still work fine.

Before homeassistant 2024.8


After

Since homeassistant 2024.8 don’t work tile card.

features:
  - type: custom:service-call
    entries:
      - type: button
        entity_id: switch.extractor_bano_l1
        tap_action:
          action: call-service
          service: switch.turn_on
          target:
            entity_id:
              - switch.extractor_bano_l1
          confirmation: false
        label: Marcha
        icon: mdi:fan
        styles: |-
          :host {
            --color: {{ 'green' if
            is_state('switch.extractor_bano_l1', 'on') else 'grey'}}
            }
      - type: button
        entity_id: switch.extractor_bano_l1
        tap_action:
          action: call-service
          service: switch.turn_off
          target:
            entity_id:
              - switch.extractor_bano_l1
        label: Paro
        icon: mdi:power-standby
        styles: |-
          :host {
            --color: {{ 'red' if
            is_state('switch.extractor_bano_l1', 'off') else 'grey'}}
            }
type: tile
entity: switch.extractor_bano_l1
color: green
state_content:
  - state
  - last-changed
card_mod:
  style:
    .: |
      ha-tile-icon {
       --tile-color:{{ 'green' if
      is_state('switch.extractor_bano_l1', 'on') 
      else 'red'
      if is_state('switch.extractor_bano_l1', 'off')
      else 'grey'}}
      }
    ha-tile-info$: |
      .secondary {
       visibility: visible;
      }
      .secondary:after {
       visibility: visible;
       white-space: pre-wrap !important;
       content: '                                                                                                                                                     ' 
                "Voltage: {{(states("sensor.extractor_bano_voltage"))|replace('.',',')}} V" '                                                                                           '
                "Potencia: {{(states("sensor.extractor_bano_power"))|replace('.',',')}} W"    
      } 
      }

For the height issue, try incorporating auto height on the info div into your code (I think it’s now hard coded at a set number of pixels after 2024.08):

    ha-tile-info$: |
      .info {
        height: auto;
      }
    ha-tile-info $ div.info: |
      .secondary {
       visibility: visible;
      }
      .secondary:after {
       visibility: visible;
       white-space: pre-wrap !important;
       content: '                                                                                                                                                ' 
                "Voltage: {{(states("sensor.extractor_bano_voltage"))|replace('.',',')}} V" '                                                                                           '
                "Potencia: {{(states("sensor.extractor_bano_power"))|replace('.',',')}} W"    
       }

Can’t really help with the icon colors as I never use this myself.

Hi,

image

I want to have more space between my icon and state number, but I can’t get it to work.

This is what I have now, it doesn’t work:

type: glance
show_name: false
show_icon: true
show_state: true
title: Bureau
entities:

  • entity: sensor.esp32_c3mini_bme280_temperature
    name: Temperature
  • entity: sensor.esp32_c3mini_bme280_humidity
    name: Humidity
  • entity: sensor.esp32_c3mini_bme280_pressure
    name: Pressure
    columns: 3
    layout_options:
    grid_columns: 4
    grid_rows: 2.87
    card_mod:
    style: |
    ha-card {
    font-size: 1.5em;
    –mdc-icon-size: 40px;
    }
    .entity__icon {
    margin-bottom: 1em;
    }

No work. :pensive:

did you read up on the fact it has something to do with position: sticky not working nicely with the overflow: hidden or, any of hidden , auto or scroll ? Most articles I found advise to set overflow: clip .

those styles are set to which card originally?

If you would need them on the individual mushroom-light-cards, then yes, set them in the template. cant use options as standalone option in the filter when you use a templates filter

I never tried anything like that, but, maybe you can also set the font to the stack-in-card, why not try it. It seems a generic mod, not state dependent, so you should probably be able to set it to all children,

you cant set it to auto-entities, as that is no card itself, the first card to be able to use card-mod on is the layout-card on your config

first create a hard-coded minimal card, of that structure, and find the element you need to modify. then work your way back to the more complete auto-entities creation

If I use this in a entities card, the title is red and the rest blue, like it should be.
But when I try this in a glance card, the title stays white, can someone help me?
Also, I want to change the padding and margin of the title and this doesn’t work.

card_mod:
style: |
.card-header {
color: red !important;
}
ha-card {
color: blue;
}

I can only say it works for me.

All I can suggest is that you look in your browser’s developer tools (F12 in Chrome/ Firefox) and see what the CSS is actually doing - you’re using quite complex CSS there, so maybe a chance to actually control what it does in the details.

I have already found the solution

features:
  - type: custom:service-call
    entries:
      - type: button
        entity_id: switch.extractor_bano_l1
        tap_action:
          action: call-service
          service: switch.turn_on
          target:
            entity_id:
              - switch.extractor_bano_l1
          confirmation: false
        label: Marcha
        icon: mdi:fan
        styles: |-
          :host {
            --color: {{ 'green' if
            is_state('switch.extractor_bano_l1', 'on') else 'grey'}}
            }
      - type: button
        entity_id: switch.extractor_bano_l1
        tap_action:
          action: call-service
          service: switch.turn_off
          target:
            entity_id:
              - switch.extractor_bano_l1
        label: Paro
        icon: mdi:power-standby
        styles: |-
          :host {
            --color: {{ 'red' if
            is_state('switch.extractor_bano_l1', 'off') else 'grey'}}
            }
type: tile
entity: switch.extractor_bano_l1
color: green
hide_state: false
state_content:
  - state
  - last_changed
card_mod:
  style:
    .: |
      ha-tile-icon {
       --tile-color:{{ 'green' if
      is_state('switch.extractor_bano_l1', 'on') 
      else 'red'
      if is_state('switch.extractor_bano_l1', 'off')
      else 'grey'}}
      }
      ha-tile-info {
        height: 65px;
      }
    ha-tile-info$: |
      .primary {
       visibility: visible; 
      }
      .secondary {
       visibility: visible;
      }
      .secondary:after {
       visibility: visible;
       position: absolute;
       left: 0px;
       top: 20px;
       white-space: pre-wrap !important;
       content: '                                                                                                                                ' 
       "Voltage: {{(states("sensor.extractor_bano_voltage")) | replace('.',',')}} V "                                                                                                 
       "Potencia: {{(states("sensor.extractor_bano_power"))|replace('.',',')}} W "    
       }

Hi @Mariusthvdb :slight_smile:

I have the hardcoded card already, that’s where I copied the styles from. I just didn’t know how to get them into the template format - that’s very confusing to me.

Didn’t realize that auto-entities doesn’t create its own ha-card. But thinking about it…of course not.

I wrapped the entire thing in a custom:mod-card. That seems to work.

Thank you very much for the input! This makes it a bit easier, plus all the styles (also for the parent card) are now in one place, which is also nice.

1 Like

It’s best if you post your code as preformatted text - makes it easier to help:
image

card_mod:
  style: |
    .card-header {
      color: red !important;
    }
    ha-card {
      color: blue;
    }

Your problem has to do with the shadow DOM. I suggest you read up on this in the official documentation to card_mod: DOM Navigation.

In your case it would be:

card_mod:
  style:
    .: |
      ha-card {
        color: blue;
      }
    $: |
      .card-header {
        color: red !important;
      }

image

1 Like

hi guys! I’m new to CSS and for now I was able to solve a lots of customizing questions by googling and searching topics, but this question in partoicular is a nightmare for me, I have an old weather radar card, and I’m trying to adjust size of it and customize a progressbar without success, and thats because all thet element are in shadow-root. can you please help me to reach out those element and chage properties?

I tried:

        card_mod:
          style:
            weather-radar-card $ ha-card $ div: |
              mapid {
                height: 400px !important;
              }
              progress-bar {
                height: 2px!important;
                background: orange;
              }

but it didnt work

thank you, it works. But now I want to change the position of my icons and I can’t figure out how to target them with the css, I tried everything.

First of all, you need to rework your CSS selectors - check this ressource.
You are targeting ids, these need to be selected using a #. Plus, for the second ruleset you left out a part of the id (progress-bar instead of div-progress-bar).
This was never going to work.

Regarding the mod_card specifics:
<ha-card> is your starting point, not <weather-radar-card>. And if you look closely, you’ll see that everything else is within ha-card, without a shadow-root in between. The shadow root below ha-card is a sibling of the cards your trying to target, not a parent.

So you should be able to just do the following:

card_mod:
  style: |
    #mapid {
      your styles
    }
    #div-progress-bar{
      your styles
    }

I haven’t tested this; give it a try and let me know if it works.

you’ll need to keep navigating through the DOM. You can see in your dev tools, that if you want to target the state-badge, you’ll need to get through the shadow-root just below div.entity
image

knowing that, you can do the following:

card_mod:
  style:
    .: |
      ha-card {
        color: blue;
      }
    $: |
      .card-header {
        color: red !important;
      }
    .entity $: |
      state-badge {
        margin-left: -50px !important;
      }

image

If you read through the documentation and familiarize yourself with the html structure of your cards, you should be able to figure this out on your own.

It took me a bit to get it sorted out as well, but once I reviewed the documentation in more detail and compared to the html, it all started to make sense.

1 Like

Apologies if this is a super silly or previously answered question:

Is there any way to add CSS formating to:

  • Sections within section view? (i.e. the individual panes that hold the cards)
  • “card wrappers” such as horizontal stack or vertical stack?
    Basically, anyhting that doesn’t respond to the “ha-card” default styling is still some kind of voodoo to me.

I’d like to try out making the individual sections on the dashboard more tangible (give them a drop shadow, a background or transparency, while making the invidual buttons rather flat.

On a similar note, i can’t seem to add a border / drop shadow / bg color to a custom:multiple-entity-row card. The “styles:” line for this card only affects the tiny part around the rightmost value, not the entire “row”. I should be able to solve that with card_mod right?

Got another issue I can’t seem to fix…

I have a piece of code which gives my elements a certain width so I can view it properly on my mobile phone. After updating it seems to have broken something, can’t figure out what exactly.

It’s about the first card_mod piece, giving the elements a certain width (43/27/15/15), but it seems to completely ignore the code now so all elements show the same width, making the most left element unreadable.

Does somebody have a clue?

type: custom:vertical-stack-in-card
mode: vertical
cards:
  - type: custom:mod-card
    card_mod:
      style: |
        hui-horizontal-stack-card:
          $:
            div#root > :first-child {
              width: 43%;
              flex: auto; 
            }
            div#root > :nth-child(2) {
              width: 27%;
              flex: auto; 
            }
            div#root > :nth-child(3) {
              width: 15%;
              flex: auto; 
            }
            div#root > :last-child {
              width: 15%;
              flex: auto;
              display: flex;
              justify-content: flex-end;
            }
    card:
      type: horizontal-stack
      cards:
        - type: custom:mushroom-template-card
          entity: sensor.e208_battery_autonomy
          tap_action:
            action: more-info
          picture: /local/car/e208.png
          primary: |
            Radius: {{ states('sensor.e208_battery_autonomy') | int }}km
          secondary: |
            Stand: {{ states('sensor.e208_mileage') | int }}km
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 0.7rem;
                --card-secondary-font-size: 0.7rem;
                --ha-card-border-width: 0px
              }
        - type: custom:mushroom-template-card
          entity: sensor.e208_battery_level
          tap_action:
            action: more-info
          primary: |
            {{ states('sensor.e208_battery_level') |int }}%
          icon: |
            {% if states('sensor.e208_charging_status') == 'Disconnected' %}
              mdi:car-battery
            {% elif states('sensor.e208_charging_status') == 'InProgress' %}
              mdi:ev-plug-type2
            {% elif states('sensor.e208_charging_status') == 'Finished' %}
              mdi:flag-checkered
            {% else %}
              mdi:progress-question
            {% endif %}
          icon_color: >
            {% if (states('sensor.e208_battery_level') | int >= 60 and
            states('sensor.e208_charging_status') == 'Disconnected') or
            states('sensor.e208_charging_status') == 'Finished' %}
              green
            {% elif states('sensor.e208_battery_level') | int >= 40  and
            states('sensor.e208_charging_status') == 'Disconnected' %}
              orange
            {% elif states('sensor.e208_charging_status') != 'Disconnected' %}
              blue
            {% else %}
              red
            {% endif %}
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 0.7rem;
                --card-secondary-font-size: 0.7rem;
                --ha-card-border-width: 0px
              }
        - type: custom:mushroom-template-card
          entity: switch.e208_clim
          tap_action:
            action: toggle
          icon: |
            {% set switch = states('switch.e208_clim') %}
            {% if switch == 'on' %}
                mdi:car-defrost-front
            {% else %}
                mdi:air-conditioner
            {% endif %}
          icon_color: |
            {% if states('switch.e208_clim') == 'on' %}
              blue
            {% else %}
              grey
            {% endif %}  
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 0.7rem;
                --card-secondary-font-size: 0.7rem;
                --ha-card-border-width: 0px
              }
        - type: custom:mushroom-template-card
          entity: input_boolean.car_details
          icon: |
            {% if is_state('input_boolean.car_details', 'off') %}
                mdi:plus
              {% else %}
                mdi:minus
              {% endif %}
          icon_color: |
            {% if is_state('input_boolean.car_details', 'off') %}
                green
              {% else %}
                red
              {% endif %}
          card_mod:
            style: |
              ha-card {
                --card-primary-font-size: 0.7rem;
                --card-secondary-font-size: 0.7rem;
                --ha-card-border-width: 0px
              }
  - type: custom:vertical-stack-in-card
    horizontal: true
    card_mod:
      style: |
        ha-card {
          --ha-card-border-width: 0px
        }
    cards:
      - type: conditional
        conditions:
          - entity: sensor.e208_charging_status
            state: InProgress
        card:
          type: custom:vertical-stack-in-card
          mode: vertical
          cards:
            - type: custom:mushroom-template-card
              primary: >
                {% set t =
                states.sensor.peugeot_e208.attributes["energy"][0]["charging"]["remaining_time"]
                %} {% set t = t | replace('PT', '') %} {% set s = t.split('H')
                if 'H' in t else ['0', t] %} {% set h = s[0] | int(0) %} {% set
                m = s[1] | replace('M', '') | int(0) %} Het opladen is klaar
                rond {{ (now() + timedelta(hours=h,
                minutes=m)).strftime('%H:%M') }}, het gaat momenteel: {{
                states.sensor.peugeot_e208.attributes["energy"][0]["charging"]["charging_mode"]
                | lower }} ({{
                states.sensor.peugeot_e208.attributes["energy"][0]["charging"]["charging_rate"]
                }})
              card_mod:
                style: |
                  ha-card {
                    --card-primary-font-size: 0.7rem;
                    --card-secondary-font-size: 0.7rem;
                    --ha-card-border-width: 0px
                  }

Would anyone know how to recreate the hover and click effects on Tile cards on Mushroom cards?
ScreenRecording2024-08-23at14.52.10-ezgif.com-video-to-gif-converter-2

Need some help if possible:

since the recent badges change i seem to not be able to use card-mod with badges.

This was my old code, how would i make the badge images bigger

Thanks

entity: person.ryan
name: null
card_mod:
  style: |
    :host {
      --ha-label-badge-size: 120px;
      --ha-label-badge-title-width: 120px;
    color: white;
    --label-badge-text-color: red;
    --label-badge-red: #1c1c1c;
    --label-badge-background-color: yellow;
    --ha-label-badge-label-color: white;
    }