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

Thanks for your effort. The problem with the margins were not the problem. I did it more ore less 1:1 before. The problem was ā€œonlyā€ the sticking when using mod-card to get rid of the first margin. And as this is present here as well and I donā€™t want to switch to an custom-layout card (did it with horizontal (with assigned comlumn, etc.) already before, but I donā€™t like, that it is not loading in a streaming way card by card but load everything first and show clack/background until it then appears.

  card-mod-view-yaml: |
    hui-masonry-view $ div#columns div.column hui-vertical-stack-card $: |
      #root hui-conditional-card:nth-child(2) {
        {% if (states("binary_sensor.ui_alert") == "off") %}
          margin-top: 0px;
        {% endif %}
      }

This one was the clue ā€¦ if you only have one dashboard. Later I saw, that if you go to another dashboard and back, etc. this mod getā€™s lost and will not be re-apllied. For whatever reason. So, no real solution, but interesting to know.

And then I though. Why not having a vartical stack as the build in, but with an ha-card (really a pitty, that so many helper-cards in HA are build different than all other cards). Like vertical-stack-in-card or stack-in-card. But tweek it that way, that it looks like tthe build in card, that means re-assign the backgrounds, etc. :joy:

This is possible with both cards, bat faster (and with less custom modding) with stack-in-card, because of the keep-objekt. Et voila:

type: custom:stack-in-card
card_mod:
  style:
    hui-vertical-stack-card$: |
      div#root { background-color: red; }
keep:
  background: true
  margin: true
  outer_padding: false
  border_radius: true
  box_shadow: true
cards:

No sticking. Behavior like in standard card. Order of cards same. Most probably only until I will see or find another problem with this approach, but it looks promising. And with that I can mod the margins as I want.

BTW Ma assumption, why they stick together is, that the mod-card is calculated by HA as 0px height and therefor more or less not existent for the masonry positioning of the cards. So calculation of the break to the next column will start with the next card. Something like that I think.

Fixed this. So Iā€™m back from two bugs to one:

value = 0:
grafik ā†’ right :white_check_mark:

value = 1:
grafik ā†’ WRONG! :x:

For the 3rd time: how to get rid of the icon when value = 1?

Maybe you want to just try it on your own? All the theoretical looking & directing ā€œwithout touchingā€ is not very effective :frowning:

      - entity: sensor.problems_any
        name: Probleme
        icon: mdi:check-circle
        card_mod:
          style:
            ha-state-label-badge $:
              ha-label-badge $: |
                .badge-container .label-badge {
                  border-radius: 20% !important;
                }
                .badge-container .label-badge .value::after {
                  {% if is_state('sensor.problems_any','1') %}
                    content: "{{states(config.entity)}}";
                  {% endif %}
                }   
            .: |
              ha-label-badge ha-state-icon {
                {% if is_state('sensor.problems_any','0') %}
                  display: none;
                {% endif %}
              }
              :host {
                {% if states(config.entity) | int(0) == 0 -%}
                --label-badge-text-color: green;
                --label-badge-red: green;
                {% else %}
                color: red;
                --label-badge-text-color: red;
                --label-badge-red: red;
                --label-badge-background-color: yellow;
                {% endif %}
              }
1 Like

I have already tested it and pointed to the problem with indentation for :host.
You did not corrected it.

For the sake of completes: I switched to vertical-stack-in-card and removed stack-in-card as well completely, because this would be one of the few places for the stack-in-card and I prefer to have only one of these types.

type: custom:vertical-stack-in-card
card_mod:
  style:
    .: |
      ha-card { background-color: red; }
    div hui-entities-card:nth-child(2) $: |
      ha-card {
        margin-top: 4px;
      }
styles:
  border-radius: var(--ha-card-border-radius, 4px)
  box-shadow: >-
    var( --ha-card-box-shadow, 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px
    0px rgba(0, 0, 0, 0.14), 0px 1px 3px 0px rgba(0, 0, 0, 0.12) )

Reason: The vertical-stack-in-card has fewer problems with stacks inside stacks (a popup with this construction is not working), some button cards were not displayed, etc. etc.

Where EXACTLY is the issue? I spend 1 hour of trying for things you are able to SHOW in 10 seconds. Please, use my code and mark which part needs to be shifted where to the left or the right. That would be great.

Who told you I have not tried it by myself??? I tested - I pointed you to the error clearly - you have not corrected it.
I am not very good in English, my native language is Russian - cannot express myself now; in short, change your attitude, please. I tried to help you, honestly).

The code:

        card_mod:
          style:
            ha-state-label-badge $:
              ha-label-badge $: |
                .badge-container .label-badge .value::after {
                }   
              .: |
                ha-label-badge ha-state-icon {
                }
            .: |
              :host {
              }

That was the error.
The ā€œ:hostā€ part ONLY was misplaced. The ā€œ:hostā€ is a root-level (do not know how to say it better) thing, so you need to put it on the 1st level.

1 Like

I will check your long post a bit later.

Meanwhile - regarding the vertical-stack-in-card.
I got some issues with stack-in-card. For me, it seems that the card is not maintained any more.
But first I used the vertical-stack-in-card and then switched to the stack-in-card - because some people on the forum complained like ā€œthe card is not maintained any moreā€ )))).
If you really advise to use the vertical-stack-in-card instead - I will start testing it.

Both of them have more or less the same number of open issues. :joy: The vertical-stack-in-card has a slightly newer update.

If I switch 1:1 from vertical-stack-in-card to stack-in-card, 50% of my cards are not working anymore. And I didnā€™t do special stuff. Esp. that nested stack didnā€™t work (anymore) and some popups and some button cards either, let me go back to vertical-stack-in-card as the preferred solution.

AFAIR Marius has excatly to opposite problems of this and moved to stack-in-card. :joy:

So it depends most probably and as always on the needs. But at least I have not a singe problem (till now) with vertical-stack-in-card.

All my issues with the stack-in-card may be treated by ā€œcard-modā€:
one
two
three
four
I use the stack-in-card inside a decluttering template ONLY. On this level I may apply a required card-mod style to fix a bug in the whole HA setup.

Ok Ildar, here comes the funny thing. And thatā€™s whatā€™s so frustrating: itā€™s a constant try and error (for hours) without success, for one single change. Sorry if I bothered you - I only want to make this finally work after two days and learn something while doing so.

So: There is a second ā€œ.: |ā€ in your full code snippet (which I didnā€™t know/see before). I entered it. Now I have exactly the same code (or: code structure) as you.

Hereā€™s a text comparison, 1:1:

And that code (mine) still gives:

  • value = 0: grafik ā†’ :x: whereā€™s the icon? that was a step backwards again *1
  • value = 1: grafik ā†’ :x: still buggy. Please go away you bad icon, please :smiley:

So question #1: what does your badge look like with your code for states 0 and 1?

*1 So because of this, I change the code back to what I had before (more precisely: I remove the line ā€œ .: |ā€ before the ā€œ:host {ā€ line), now we - again - have:

      - entity: sensor.problems_any
        name: Probleme
        icon: mdi:check-circle
        card_mod:
          style:
            ha-state-label-badge $:
              ha-label-badge $: |
                .badge-container .label-badge {
                  border-radius: 20% !important;
                }
                .badge-container .label-badge .value::after {
                  {% if is_state('sensor.problems_any','1') %}
                    content: "{{states(config.entity)}}";
                  {% endif %}
                }   
            .: |
              ha-label-badge ha-state-icon {
                {% if is_state('sensor.problems_any','0') %}
                  display: none;
                {% endif %}
              }
              :host {
                {% if states(config.entity) | int(0) == 0 -%}
                --label-badge-text-color: green;
                --label-badge-red: green;
                {% else %}
                color: red;
                --label-badge-text-color: red;
                --label-badge-red: red;
                --label-badge-background-color: yellow;
                {% endif %}
              }

ā€¦which gives:

  • value = 0: grafik ā†’ perfect! :white_check_mark:
  • value = 1: grafik ā†’ again: :x: still buggy. Please go away you bad icon, please :smiley:

So to sum up:

  1. I use exactly your code structure: 2 errors (styling is wrong for both states)
  2. I use the same code without the ā€œ .: |ā€ on top of the ā€œ:hostā€ line/section: 1 error remaining

I can not tell you what is wrong here. You are the styling guru expert, tell me :smiley:
I really canā€™t.
I donā€™t know what else I can do.
I closely follow your instructions, use the same code and repeatingly get the wrong result.
:question: Can it be a browser issue maybe?
:question: Or a HA version mismatch (Iā€™m on 2022.4.7)?

Please contact me via PM, weā€™ll finalize this case

1 Like

To everyone potentially interested: together we sorted this out and finally fixed it, everyone spotted things and alltogether formed a working solution (only minor, but very important changes). To sum things up:

  1. :exclamation: My mistake: the missing additional ā€œ.: |ā€ line above the :host line
  2. :exclamation: All condition checks must be identical (check on the same value)
  3. :exclamation: If itā€™s not a binary_sensor (or binary input_helper) but a sensor with various possible numbers, the if condition checks need to be adjusted.

This is working now:

      - entity: sensor.problems_any
        name: Probleme
        icon: mdi:check-circle
        card_mod:
          style:
            ha-state-label-badge $:
              ha-label-badge $: |
                .badge-container .label-badge {
                  border-radius: 20% !important;
                }
                .badge-container .label-badge .value::after {
                  {% if states(config.entity) | int(0) > 0 %}
                    content: "{{states(config.entity)}}";
                  {% endif %}
                }   
              .: |
                ha-label-badge ha-state-icon {
                  {% if states(config.entity) | int(0) > 0 %}
                    display: none;
                  {% endif %}
                }
            .: |
              :host {
                {% if states(config.entity) | int(0) == 0 -%}
                --label-badge-text-color: green;
                --label-badge-red: green;
                {% else %}
                color: red;
                --label-badge-text-color: red;
                --label-badge-red: red;
                --label-badge-background-color: yellow;
                {% endif %}
              }

Gosh this was a tricky one!
I learned a lotšŸ‘Øā€šŸŽ“
Big props to Ildar :partying_face: :v: :ok_hand: the :crown: of styling

1 Like

Small note on this: there seems to be a browser issue. My Firefox on my (Windows) notebook randomly (okay, tbh most of the time, maybe 5 % of the time it is shown correctly) just shows the default icon (not the dynamic one), e. g.
grafik
ā€¦where at the same time all other devices (iOS, Android - always running the HA Companion app) show them correctly:
grafik

I have no idea if maybe some privacy addon of Firefox is blocking something or why it doesnā€™t work - but Iā€™ll try to further investigate this so this will give a consistent user experience on all devices and browsers. Will report back if I tracked this down.

I try to change the actual label text (as provided by the entity, so not add a new but change the existing one).

Is that done in this section (not working)?

                .badge-container .label-badge {
                  border-radius: 20% !important;
                  content: "My label"; <-- this line has no effect
                }

I checked šŸ”¹ Card-mod - Add css styles to any lovelace card - #1310 by Ildar_Gabdullin twice which is all about the look, but not the content.

I use core cards wherever possible. type: entities/grid/horizontal-stack/vertical-stack.

to embed any other card, I use the custom:hui-element as the custom card that makes all possible.
Ofc card_mod styling that allows me to do anything I need with those. Adjust card margins, delete grid gaps. Even the odd mod-card to get rid of the gaps in stack cards. Works perfectly.

If I cant make it with those, I rethink my wishesā€¦ There have been too many issues/incompatibility with the other cards.

#type: custom:state-switch
#entity: binary_sensor.meteoalarm_brabant
#states:
#  'on':
type: conditional
conditions:
  - entity: binary_sensor.meteoalarm_brabant
    state: 'on'
card:
  type: entities #custom:stack-in-card
#    keep:
#      background: true
#    cards:
  entities:
    - type: custom:hui-element
      card_type: markdown
      card_mod:
        style: |
          ha-card {
            margin: -16px;
            box-shadow: none;
         }

some indenting needs to be adjusted when uncommenting the other cards, but this is the idea. Use core when core is available and working correctlyā€¦

Not a single stack-in-card left.

    type: custom:badge-card
    badges:
      - entity: sensor.memory_free
      - entity: sensor.memory_free
        card_mod:
          style:
            ha-state-label-badge $ ha-label-badge $: |
              .badge-container .label-badge .value::after {
                content: "RAM: {{states(config.entity) | float(0) | round(0)}}";
                font-size: 7px;
              }
              .badge-container .label-badge .value {
                font-size: 0px;
              }

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

1 Like

Thank you. Sorry, I was not precisely enough - I was looking for changing this text (the unit, here it is MB):

grafik

Then you didnā€™t get the problem. :sunglasses: Anyhow, Iā€™m happen now with my setup and I have optimized it, to not having any card-mod now needed for this case. Card mod has unfortunately the problem, that it is sometimes assigned too late and then I have the stuttering, etc. (In some browser, devices, situations) And in this special case it was not pretty, neither to remove gaps or to add them. As said, no improvements here needed.

Replacing this may cause glitches (may give different results on different devices).
Seriously - sometimes it is better to create a template sensor.

  - entity: sensor.memory_free
    name: replaced unit
    card_mod:
      style:
        ha-state-label-badge $ ha-label-badge $: |
          .badge-container .label-badge .label span::after {
            content: "xxx";
            color: blue;
            letter-spacing: normal;
          }
          .badge-container .label-badge .label span {
            color: transparent;
            letter-spacing: -8px;
          }

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

maybe :wink: just to be clear what I meant there:

#type: custom:stack-in-card
#card_mod:
#  style: |
#    ha-card {
#      margin: -16px;
#      box-shadow: none;
#    }
#keep:
#  background: true
#cards:
type: custom:mod-card
card_mod:
  style: |
    ha-card {
      margin: -16px;
      box-shadow: none:
    }
    :host {
      --stack-card-margin: 0px;
    }
card:
  type: custom:hui-element
  card_type: vertical-stack
  cards:

and no further mods required. It tightly knits all enclosed cards together. No gaps. very clean.
if this be done cleaner, Id be interested for sure.

Ill have to lookup you card solution though, because its not easy to find the final card config you ended up with in all posts aboveā€¦

note the margin and box-shadow here are only used because this card is included in an auto-entities card.

meaning, if youā€™d use this as a stand alone card, all modding of the core vertical-stack card would be the

    :host {
      --stack-card-margin: 0px;
    }