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

Sorry, wrong formatting options on this forum. Fixed:

 type: custom:thermostat-dark-card
 card_mod:
   style: |
     svg {
       width="75%"
     }

And by changing the width in that element manually using the inspector, I was able to get the desired effect. Was my approach correct? The image I pasted showed the values I changed it to (75%) and it rescaled correctly. Should it have worked?

You changed the width via Code inspector by setting some property to some value for some element.
Result is OK.
Then try to set the same value for the same property for the same element by card-mod.
The result is supposed to be OK too - just define a proper DOM path to the element; probably, the ā€œ!importantā€ keyword may be required.

Must be:

width: 75%;

I took that to heart, and through trial and error actually got it to work.
Added the following to every entity:

card_mod:
  style:
    hui-generic-entity-row:
      $: |
        :host {
          height: 20px;
        }

Considering I have never touched any coding before, I am incredibly happy with figuring it out after spending ~30 hours on it. Thanks to the community I learned a few things along the way.
2 fist were swiftly raised in the air.

1 Like

ā€œ30 hoursā€ :question::flushed::flushed::flushed:

lol, as I said, I have no experience with JS, Yaml, or CSS whatsoever. All of this is a learning experience.
And the time consumption doesnt really bother me much, I enjoy creating something of my own from ā€œthe bottom upā€

This is a good point! HA is a kind of hobby for many people))).
Btw, I have never been a CSS expert too, never been a web-programmer or designer))).
Everything is achieved step by step.

The code is not 100% correct.
See this:


There are at least 2 ways of decreasing a row height:

  • remove margins between rows - 2nd column;
  • decrease a rowā€™s height (40px ā†’ 25px) - 3rd column= for all rows, 4th column=for each row - up to you to decide which variant needs less of lines.
type: horizontal-stack
cards:
  - type: entities
    entities: &ref_0
      - entity: sun.sun
      - entity: sensor.cleargrass_1_co2
      - entity: input_boolean.test_boolean
      - entity: input_number.test_number
  - type: entities
    entities: *ref_0
    card_mod:
      style: |
        .card-content div {
          margin-top: 0px !important;
          margin-bottom: 0px  !important;
          color: red;
        }
  - type: entities
    entities: *ref_0
    card_mod: ### leave the code only for your rows (i.e. for sensors only if no other types are present)
      style:
        hui-simple-entity-row:
          $: |
            hui-generic-entity-row {
              color: red;
              height: 25px;
            }
        hui-sensor-entity-row:
          $: |
            hui-generic-entity-row {
              color: cyan;
              height: 25px;
            }
        hui-toggle-entity-row:
          $: |
            hui-generic-entity-row {
              color: magenta;
              height: 25px;
            }
        hui-input-number-entity-row:
          $: |
            hui-generic-entity-row {
              color: green;
              height: 25px;
            }
  - type: entities
    entities:
      - entity: sun.sun
        card_mod: &ref_1
          style: |
            hui-generic-entity-row {
              height: 25px;
            }
      - entity: sensor.cleargrass_1_co2
        card_mod: *ref_1
      - entity: input_boolean.test_boolean
        card_mod: *ref_1
      - entity: input_number.test_number
        card_mod: *ref_1
4 Likes

I am trying to achieve the same but I canā€™t. Could please help me?

is there no way to change color of the thermostat card?

type: thermostat
entity: climate.mbr_ac
name: ' '
style: |
  ha-card {
    color:red;
    }

So Iā€™ve used card-mod to apply a dynamic color to the icons of every thermometer entity in my entity list. Thereā€™s 8 of them, so basically itā€™s 8 times this:

- entity: sensor.bla
  card_mod:
    style: |
      :host {
        -- 17 lines of code here
      }

Anyone who knows about programming will know what a nightmare it is to have the exact same bit of code just copy/pasted, so Iā€™d really like to get rid of this unnecessary amount of code, it really makes it hard to change anything in this entity card.

So basically: is there any way at all to apply a style to each icon, where the color is based on the value of the matching entity? I noticed config.entity, but that only seems to work inside a :host section.

  1. auto-entities with one card_mod.
  2. Specify card_mod for the 1st row and then use yaml anchor like card_mod: *ref_card_mod_for_sensor.
  3. Use card-mod-theme for entity-row to specify a style for every row containing a sensor with name/id/device_class/etc corresponding to some conditions.
1 Like

First of all: thanks for the suggestions, I appreciate it.

I didnā€™t explore this option, since I already had the list of entities ready, and auto-compiling a list could be limiting in future scenarios.


This is what I ended up going with. Itā€™s not quite as compact as I wouldā€™ve liked, but itā€™s a lot more compact than the original, and it works, so Iā€™m quite satisfied. Again though, the documentation on these things isnā€™t great, so Iā€™ll give a short summary here for those that run in to the same issue:

While typing this up, I saved the card config, closed and re-opened, and to my horror I saw that this does not at all work like I expected. I expected the anchors to behave like variables: defined once, can be looked up infinitely. Instead, anchors work like placeholders, and the code gets compiles. I.e., when you click save, all your anchors get replaced by the code that anchor represents.


After finding out that #2 didnā€™t work for me, I dove deeper in to #3. It seems however that for this to work, you need to set the theme for HA as an entirety, which doesnā€™t make sense at all to me. Is there no option to assign a theme to a single section or something like that?


Iā€™m very curious to see if Iā€™m just not understanding things correctly, or if itā€™s seriously this hard to style multiple items with the same bit of code.

Anchors should be used in yaml mode only.
When used in UI editor then these anchors like "ref_my_anchor_for_sensor" MAY BE saved as "ref_0" - but also may be just replaced by code (depends, I havenā€™t investigated this issue).

You may just define a card-mod style for ALL entities in the Entity card (see the 1st post ā†’ accumulative post ā†’ post for Entities card). Note that since Entity card may contain different types of rows (text0entity-row, sensor-entity-row, ā€¦) you have to specify a code for each type.

I never tried but canā€™t we set a theme for a particular card?

1 Like

Huh, I didnā€™t know thereā€™s a real difference between editing the yaml file directly, and doing it through the ā€œcode editorā€.


I think youā€™re referring to the first code block in that post here, where style: is at the very front of the line (not indented). This indeed would have been great, but only for static styling, at least thatā€™s what it looked like to me. I couldnā€™t get anything regarding to config.entity working when using that method.


We can (apparently). I never used themes, so I never noticed this option. This is the winner for me, for now at least.

Basically just follow these steps: Card mod Themes Ā· thomasloven/lovelace-card-mod Wiki Ā· GitHub
Make sure to create the theme folder, create the theme yaml file, add the frontend bit in configuration.yaml, restart HA, reload themes, and of course set the theme to the entities list.

Itā€™s not perfect, since you need to call the reload themes service every time you update a theme (which is VERY easy to forget, especially if you havenā€™t done it for a while), but itā€™s the best option Iā€™ve seen so far.

Agreed, this cannot be used on the card level.

Each time you edit a template sensor you need to reload them template sensors.
Here is the same situation.

Not sure if this is possible, but seeing how some users seem to be magicians, I hope someone knows how to achieve this. Basically I want an iframe (webcard) to adjust to the screen size automatically. I use this card on different devices. The content in the iframe is already responsive, so I hope the iframe can be as well. This is the code I currently use. I used mod-card because (if I remember correctly when creating this) it didnā€™t work as standard card-mod style.

                      - type: custom:mod-card
                        card_mod:
                          style: |
                            ha-card {
                              border-radius: 20px !important;
                              --ha-card-border-radius: 20px !important;
                              height: 85vh;
                              
                            }            
                        card:            
                          type: iframe
                          url: 'https://LINK/'
                          aspect_ratio: 165%

The main challenge with iframe is that you need to use an aspect ratio, which forces it to a certain shape. So I hope I can alter this card using card-mod to make it responsive and always fill the entire screen.

But perhaps I need Config Template Card for this and use a template to determine the ratio depending on the viewport of the screenā€¦?

I cannot reproduce it.
Tried with the 1st site and it is scaled properly:


And there is no need for mod-card:
ŠøŠ·Š¾Š±Ń€Š°Š¶ŠµŠ½ŠøŠµ

card_mod:
  style: |
    ha-card {
      --ha-card-border-radius: 20px !important;
    }            
type: iframe
url: https://meduza.io
aspect_ratio: 165%
1 Like

The content inside the iframe is scaled fine, thatā€™s not the issue. Itā€™s the card element of the iframe. I need the card to take the size of the entire screen automatically. The content in the card already fitā€™s itself fine to the card size.

So If I open the webcard on a screen with less height, itā€™s should still be fullscreen and I donā€™t need to scroll the page. Also when the screen has more height, it needs to fill the entire screen and not leave empty spaces below it. I tried with height: 100vh but that didnā€™t work.

See my example: Imgur: The magic of the Internet

I hope I explained it well what Iā€™m trying :slight_smile:

Whatā€™s funny is that it does work as intended when I open the url as itā€™s own custom panel (in sidebar). It adapts to the screensize. Ofcourse I can forward to that as well, but then I have no UI there and no way to go back with a simple button (like I have now in my own dashboard).

Your example - what I noticed is:

  • if you change a height of the screen then you see more/less ā€œlinesā€ on the page;
  • there is a sticky footer (probably as a part of the page).

My example w/o height does the same (w/o sticky footer).
So, still I am not getting the problem((((.

What UI do you need? Not clear

I donā€™t want to see more/less lines on the page, thatā€™s the challenge. I want the webcard to fill the entire screen, on different devices. I want the webcard to always fill the entire screen. Also as you can see, if I make the window smaller, I have to scroll within Lovelace, the webcard is larger than the screensize, it doesnā€™t adapt. This means that the webcard is not scaling with the screen, itā€™s fixed due to the aspect ratio. So I need the webcard to be responsive, like the content in the iframe.

Currently I have this inside my dashboard (i.e. UI). So I can open my LMS webpage as part of my dashboard. Basically I want the webcard to act like this: Imgur: The magic of the Internet As you can see, it always fitā€™s the entire screen.

But I think this is impossible due to how the webcard renders itself and needs an aspect ratio.

I am afraid I cannot help since havenā€™t got an example of a page which may fit whole area.