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

Hi guys,

I am a bit lost here and canā€™t find a solution.

I want to target this specific div element and the p inside it, the other elements should not be affected. Is this even possible? I want to add to the div element

display: flex; align-items: center;

and to p

color: red; margin: 0 10px;

Screenshot 2024-12-03 000544

I tried something like

card_mod:
  style:
    ha-markdown $ ha-markdown-element: |
      div {
        display: flex; 
        align-items: center;
      }
    ha-markdown $ ha-markdown-element div: |
      p {
        color: red; 
        margin: 0 10px;
      }

But this affects all p elements.

Would you mind sharing this?
It really looks awesome!

Also the scene part how does that look when you expand it?

o that is nice indeed. Im always fighting with those rgba colors when Im using theme variablesā€¦ didnt know this functionality existed at all, never saw it in HA .

can we also use that with color names or hex values?

rgb(from green r g b / 1)

or

rgb(from '#98878f' r g b / 1)

? up to now, Iā€™ve always added a dedicated variable primary-rgb color to the themes

primary-rgb: 152,135,143

so I can use that in the rgba() syntax. this function now is much prettier!

edit

yes we canā€¦ wow

            @keyframes resizing_outline {
              0% {outline-offset: 0;outline-color: rgb(from '#98878f' r g b / 1);}
              25% {outline-offset: 5px}
              50% {outline-offset: 10px}
              75% {outline-offset: 15px}
              100% {outline-offset: 20px;outline-color: rgb(from red r g b / 0.5);}
            }

it does require the spaces between r g b, I tested rgb but that simply returns the border color.

o man this css is really so cool, the effects are simply amazing, adding the transparency on the border-color makes it even more attractive:

            @keyframes resizing_outline {
              0% {outline-offset: 0;outline-color: rgb(from var(--warning-color) r g b / 1);}
              25% {outline-offset: 5px}
              50% {outline-offset: 10px}
              75% {outline-offset: 15px}
              100% {outline-offset: 20px;outline-color: rgb(from var(--alert-color) r g b / 0.5);border-color: transparent;}
            }

perfect!
got something to do today :wink:

for the badge modders among us:
Ive just found that we can use the variable var(--badge-color) in our modifications:

type: custom:mod-card
card:
  type: custom:hui-entity-badge
  entity: sensor.next_alarm
  color: var(--alert-color) # this will be inherited by the mod below when using var(--badge-color)
  tap_action:
    action: navigate
    navigation_path: /ui-instellingen/wekker_instellingen
visibility:
  - condition: state
    entity: binary_sensor.wekker_voor_morgen
    state: 'on'
card_mod:
  style:
    hui-entity-badge:
      $:
        ha-badge:
          $: |
            .badge {
              border: 2px solid var(--badge-color) !important;
            }

and it takes the color set in the main badge config options. In the above card that is var(--alert-color)

SchermĀ­afbeelding 2024-12-03 om 08.32.52

this is also an option, using theme variable:

type: custom:mod-card
card:
  type: custom:hui-entity-badge
  entity: alarm_control_panel.ha_main_alarm
card_mod:
  style:
    hui-entity-badge:
      $:
        ha-badge:
          $: |
            {% set state = states(config.card.entity) %}
            .badge {
              border: 2px solid var(--state-alarm_control_panel-{{state}}-color) !important;
            }

The result of this code is, that the button is always white. Where am I going wrong ?
Also I want a little border around when it is whiteā€¦ How do I achieve this ?

 card_mod:
                                style:
                                  mushroom-state-info$: |
                                    .container {
                                      margin: -0px;
                                      }  
                                    .primary {
                                      padding: 0px 0px 0px 0px;
                                                }
                                  mushroom-shape-icon$: |
                                    .shape {
                                    margin-top: 0px;
                                    }
                                  .: |
                                    ha-state-icon {
                                      {% if is_state('light.gevel','on') %}
                                      animation:  blink 1s infinite, illumination 1s infinite ;{%- endif %}
                                      }
                                      @keyframes blink {
                                        50% {opacity: 0;}
                                            }   
                                      @keyframes illumination {
                                          0%, 100% { clip-path: inset(0 0 0 0); }
                                          95% { clip-path: polygon(0% 99%, 20% 55%, 22% 37%, 39% 20%, 61% 21%, 77% 35%, 79% 57%, 99% 100%); }
                                                }
                                    ha-card {
                                      width: 160px;
                                      height:60px !important;  
                                      ha-card-box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.3) !important  
                                      {% if is_state('light.gevel','on') %}
                                      {
                                      background-color: rgb({{ states("input_text.kleur_iconen_bleker")}}) !important;
                                      ha-card-border-color: rgb({{ states("input_text.kleur_iconen_bleker")}}) !important;
                                      ha-card-border-width: "2px"
                                      }
                                      {%else %}
                                        white
                                      {%- endif %}

this is the part you need debugging ? if yes, please post only the relevant bit, and not let us find your code for youā€¦
for example, what card is this? Mushroom? ok, which, etc etc

also, show the card, and the output this template has in dev tools

btw there is a dedicated Mushroom styling thread in Mushroom Cards Card Mod Styling/Config Guide be sure to check there first

it is a mushroom template card

                                    ha-card {        
                                      {% if is_state('light.gevel','on') %}
                                      background-color: rgb({{ states("input_text.kleur_iconen_bleker")}}) !important;
                                      {% endif %}
                                      }

so when my entity is on, i want to change the background color of the cardā€¦

Posted this in the front end category but was directed here instead. Iā€™m new to HA and understand enough to copy/paste and make modifications to suite my needs.

Iā€™m using card-mod to make a card transparent, without a border, and to change text colors. Any card I edit in the first dashboard in card position 1 (first card on first dashboard), will not keep the css styles applied to it. I can edit the card, add the css styles, and then save it and it works, but as soon as I refresh my browser, or close the android HA app and re-open, the card goes back to its default settings and ignores the css styles. Iā€™ve cleared the cache, tried different browsers, and deleted/re-installed the HA app and the issue still persists across multiple devices. This is the example of the card Iā€™m using and the css mods added to it.

show_current: true
show_forecast: true
type: weather-forecast
entity: weather.forecast_home
forecast_type: daily
card_mod:
  style: |
    ha-card {background: transparent;
       --border-style: none;
       --primary-text-color: white;
       --secondary-text-color: teal;
       --border: 0px;
       --box-shadow: none;
       --background: rgba(50,50,50,0.3);
       border: none;}
       

This only happens to the first card on my first dashboard, any other position or dashboard and all the styles work correctly. Any help is greatly appreciated!

Cannot reproduce in Chrome.


BTW, these are wrong vars:

--border-style
--border
--background
--box-shadow

Can you provide any help with what the variables should be, or how they should be formatted if improper? Iā€™m trying to learn more about CSS but very slow going. I copied the style code from another post, but canā€™t remember where. Iā€™m just trying to get some basic styles going until I really have time to learn more.

  1. Some examples are provided & can be used as a ā€œstarting pointā€: 1st post of this thread ā†’ link at the bottom titled ā€œfantasticā€ ā†’ scroll down for weather card.
  2. This small code could be rather sufficient:
card_mod:
  style: |
    ha-card {
      background: transparent;
      --primary-text-color: white;
      --secondary-text-color: teal;
      border: none;
    }

At least it removes a border, makes a transp. background & set colorsā€¦

1 Like

That does simplify it, I really appreciate it. Hopefully I can figure out why the styling doesnā€™t work for me in that first dashboard position. I really like the theme Iā€™m getting together so far, and the transparency really ties it together.

Note that a stock weather card can change itā€™s class dynamically (ā€œnarrowā€ etc) dependently on a view, this might (just a guess) cause issues on some browsersā€¦

Iā€™ll try to find a card that isnā€™t dynamic and see if that has any effect. So far Iā€™ve tried the default weather card, custom expander card, and default thermostat card and have the same issue in that first card slot. Iā€™ll try some other cards and see if I can find one that doesnā€™t lose its styling. Thanks for the help. I would assume that most cards are going to be dynamic (or have some kind of scaling) when using the HA companion app?

No idea. Only a few stock cards (weather, markdown, ???) have dynamic classes added. Other cards have dynamic sizes (as expected) (and letā€™s not talk about sectionsā€¦).

Yes, you can. But donā€™t put the hex color between quotes (even if it works) because it is not the standard. Do it like this:

rgb(from #98878f r g b / 0)

Yes, you need to separate any color by spaces, that is because you can do this:

rgb(from #98878f r 25 50)

So, it will take the red from #98878f (0x98 = 152) and the blue and red as 25 and 50.

1 Like

Iā€™ve been playing around trying to customise some climate tiles for a wall display dashboard to primarily control a heat pump
Unfortunately I have been unable to locate examples to understand how to achieve what I want - any assistance with the following appreciated

See below photos - I was hoping to be able to customise inactive and active colours for the HVAC mode and fan speed buttons on the climate tile card
I was also hoping to replace the icons with text ā€œHeatā€ ā€œCoolā€ etc
Any help with how to reference the buttons and achieve the above would be appreciated
Cheers
Waz

1 Like

I have been struggling with this for 2 days, and I need to know if it is even possible (so I can stop if it isnā€™t)ā€¦ I have really tried to avoid asking someone to think for me, but I have failedā€¦ Please help, I need to sleep!

I have a default sections dashboard with a couple of tabs (calendar and shopping list). Iā€™ve tried to show the code with as little as possible whilst showing enoughā€¦
I am successfully placing an image from my template in the background of the custom:week-planner-card (as shown with the first card_mod), but I want the image in the background behind that (and the other) cards, basically the background of the dashboardā€™s tab.
I have tried so many versions of the second card_mod shown below to get the image behind the card (in ha-sortable or hui-view or hui-view-container, I canā€™t get anything in the card_mod slot of those)ā€¦

kiosk_mode:
views:
  - title: Calendar
    sections:
      - type: grid
        cards: ...
      - type: grid
        cards:
          - type: custom:week-planner-card
            calendars: ...
            ...
            card_mod:
              style: |
                ha-card {
                  background: url('{{ states('sensor.unsplash_random_image') }}') no-repeat center;
                  background-size: 100%;
                }
      - type: grid
        cards: ...
    card_mod:
      style: |
        ha-sortable: {
          background: url('{{ states('sensor.unsplash_random_image') }}') no-repeat center;
          background-size: 100%;
        }
    cards: []
    type: sections
    ...
    badges: ...

I have also tried DOM navigation using things like the below:

ā€œbody home-assistant $ home-assistant-main $ ha-drawer partial-panel-resolver ha-panel-lovelace $ hui-root $ #view hui-view hui-sections-view $ ha-sortableā€:

But, I think I just donā€™t get itā€¦

Please, can someone tell me how to do this, or tell me it canā€™t be done. Either way, end my misery!

Thanks,
Matthew

nice, and great explanation, something for Ildarā€™s cookbook overview I would think!

fwiw, my complete mod now, well, my complete type: map view even, is

title: Kaart
path: kaart
icon: mdi:map
type: panel
cards:
# https://community.home-assistant.io/t/card-mod-add-css-styles-to-any-lovelace-card/120744/7530
# https://community.home-assistant.io/t/card-mod-add-css-styles-to-any-lovelace-card/120744/7551
  - type: map
# can Not use entity as object because of the card_mod
    hours_to_show: 48
    entities:
      - person.me
      - etc
      - 
    card_mod:
      style:
        ha-map $:
          ha-entity-marker $: |
            {% for entity in config.entities %}
              {% set id = entity.split('.')[1] %}
              {% set activity = states('sensor.' ~ id ~ '_activity') %}
              {% set afstand = states('sensor.thuis_' ~ id ~ '_distance')|int(-5) %}
              {% set thresholds =
                [(300,0.5),(200,1),(150,1.5),(100,2),(50,2.5),(30,3),(20,3.5),(10,4)] %}
              {% set pix =
                thresholds|selectattr('0','<=',afstand)
                |map(attribute='1')|list|first|default(8) %}
              {% set t = ((1 / 150) * afstand + 1)|round(1) %}
              {% if activity in ['Automotive','Cycling','Walking'] %}
                :host([entity-id='{{entity}}']) .marker {
                  outline: solid {{pix}}px var(--warning-color);
                  animation: resizing_outline calc( {{t}}s ) linear infinite;
                  border: var(--warning-color) {{pix}}px dashed;
                }
              {% endif %}
            {% endfor %}

            @keyframes resizing_outline {
              0% {outline-offset: 0;outline-color: rgb(from var(--warning-color) r g b / 1);}
              25% {outline-offset: 5px}
              50% {outline-offset: 10px}
              75% {outline-offset: 15px}
              100% {outline-offset: 20px;outline-color: rgb(from var(--alert-color) r g b / 0.1);border-color: transparent;}
            }

and has been working perfectly with the latest adjustments

leaves me with just 1 challenge: how to mod this on a single entity in a Map config like:

type: map
entities:
  - entity: person.me
    name: Me in map
    focus: false
    card_mod:
      style:
        ha-entity-marker $: | ??

its different because of the path, and because of the fact its now using an entity object (opposed to the entity in the posted config above)

I would like the text and the icon to turn red when the window is open and green when it is closed. Unfortunately, my code is not working. Could someone please help me?

type: heading
heading_style: subtitle
heading: "BĆ¼ro "
badges:
  - type: entity
    entity: binary_sensor.fenster_buro_tur_2
    style: |
      :host {
       color: {{ states('binary_sensor.fenster_buro_tur_2') == 'on' ? 'var(--error-color)' : 'var(--success-color)' }};
      }
  - type: entity
    entity: sensor.rauchmelder_buro_reinheit
icon: mdi:desk