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

type: vertical-stack
title: Left padding
cards:
  - type: entities
    entities: &ref_entities
      - entity: sun.sun
      - entity: sun.sun
      - entity: sun.sun
  - type: entities
    entities: *ref_entities
    card_mod:
      style: |
        .card-content {
          padding-left: 64px;
        }
  - type: entities
    entities:
      - entity: sun.sun
      - entity: sun.sun
        card_mod:
          style: |
            hui-generic-entity-row {
              padding-left: 24px;
            }
      - entity: sun.sun
        card_mod:
          style: |
            hui-generic-entity-row {
              padding-left: 48px;
            }

I do not know how to add an icon - but you can add an image:

      - entity: sun.sun
        card_mod:
          style:
            hui-generic-entity-row:
              $: |
                .info.pointer.text-content:before {
                  background: url("/local/images/Stan_small.png");
                  content: "----";
                  color: transparent;         
                  background-size: cover;
                }

изображение

Look at this card:

  - type: entities
    title: Header
    entities:
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
    card_mod:
      style: |
        .card-header {
          background-color: rgba(255,0,0,0.2);
        }
        .card-content {
          background-color: rgba(0,255,0,0.2);
        }

It is clear that “header” & “card-content” areas are overlapping.
With “margin-top: 0px” they are not:

  - type: entities
    title: Header
    entities:
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
    card_mod:
      style: |
        .card-header {
          background-color: rgba(255,0,0,0.2);
          padding-top: 0px;
          padding-bottom: 0px;
        }
        .card-content {
          background-color: rgba(0,255,0,0.2);
          margin-top: 0px;
          overflow-y: scroll;
          overflow-x: hidden;
          max-height: 300px;
          margin-bottom: 16px;
        }

изображение

a yes, thats a nice variant too. It has the same effect as my margin in the header adding the 8px to the bottom.

However, because all of my entities are folds, the card shows the top divider. which is what I ws trying to prevent, and have it look like the first screenshot above

guess that cant be done

icons are also sag images, so you can add:

            style:
              hui-generic-entity-row:
                $: |
                  .info.pointer.text-content:before {
                    background: url("/local/weather/laptop-mac.svg");
                    content: "----";
                    color: transparent;
                    background-size: cover;
                  }

they have to be in /local though. I tried to add it from custom_icons in the config root, and that would work, though inspector showed the correct path, it claimed it couldn’t find it…

You may make the 1st divider transparent.
Kind of:

type: entities
title: Header
entities:
  - &ref_0
    type: custom:fold-entity-row
    head:
      type: section
      label: Label
    padding: 0
    entities:
      - sun.sun
      - sun.sun
      - sun.sun
      - sun.sun
  - *ref_0
  - *ref_0
  - *ref_0
  - *ref_0
  - *ref_0
  - *ref_0
  - *ref_0
card_mod:
  style:
    .card-content div:
      fold-entity-row $ hui-section-row $: |
        .divider {
          margin-top: 0px !important;
        }
      fold-entity-row $: |
        ha-icon {
          margin-top: 8px !important;
        }
    .card-content:
      fold-entity-row $ hui-section-row $: |
        .divider {
          background-color: transparent !important;
        }
    .: |
      .card-header {
         background-color: var(--divider-color);
         padding-top: 0px;
         padding-bottom: 0px;
       }
       .card-content {
         overflow: hidden scroll;
         max-height: 366px;
         margin-top: 0px;
         margin-bottom: 16px;
         padding-bottom: 0px;
       }

azxsw

I’ll try to check it.

1 Like

I do not see any try with card_mod, the topic you have put your question. Where is it?

Anyway: First you should always read 🔹 Card-mod - Add css styles to any lovelace card - #1396 by Ildar_Gabdullin. As most of the times, there are examples for the cards, here picture-elements.

Above, I pointed to the CSS-values, you should try to adjust. And as expected, it seems, that it is directly working.

  - type: state-icon
    card_mod:
      style: 
        state-badge:
          $: |
            ha-state-icon {
              color: red !important;
              filter: initial !important;
            }

Of course, you have to jinja around this to distinguish beteween on and of and replace red with other color or css-variable.

2 Likes

Thx, it works and this is exactly what I wanted. I nearly gave up because while searching, I never came across the post you linked.

I wish something so simple like choosing an icon for the state and its colour was possible via UI in HA, it would save a ton of time for the CSS noobs like me. Anyway, thanks again.

Alright. So I need to apply it to the whole card, applying it to a row is not possible, right?
So I need to rework my whole section. Because this is what I currently use (for maaaaany sensors) and I only want the type: weblink row to move to the right a bit.

        - type: conditional
          conditions:
            - entity: sensor.haos_update
              state: 'on'
          card:
            type: entities
            entities:
              - type: custom:multiple-entity-row
                entity: sensor.haos_update
                name: HAOS
                toggle: false
                state_color: false
                show_state: false
                secondary_info: ''
                entities:
                  - entity: sensor.haos_update
                    name: Ist
                    attribute: current_version
                  - entity: sensor.haos_update
                    name: Neu
                    attribute: newest_version
                state_header: Update?
                column: false
              - type: weblink
                name: Gehe zu Config-Dashboard
                url: /config/dashboard
                icon: mdi:arrow-right
            card_mod:
              style:
                .: |
                  ha-card {
                    margin-left: 1px;
                    margin-right: 1px;
                    #margin-top: -10px;
                    margin-bottom: -25px;
                    #box-shadow: none;
                  }

Is it possible to combine the hui-generic-entity-row: with the padding-left: xxpx;?

This did NOT work for me. Possibly because I nested the entities card within a conditional card, right?

        - type: conditional
          conditions:
            - entity: sensor.haos_update
              state: 'on'
          card:
            type: entities
            entities:
              - type: custom:multiple-entity-row
                entity: sensor.haos_update
                name: HAOS
                toggle: false
                state_color: false
                show_state: false
                secondary_info: ''
                entities:
                  - entity: sensor.haos_update
                    name: Ist
                    attribute: current_version
                  - entity: sensor.haos_update
                    name: Neu
                    attribute: newest_version
                state_header: Update?
                column: false
              - type: weblink
                name: Gehe zu Config-Dashboard
                url: /config/dashboard
                icon: mdi:arrow-right
                card_mod:
                  style: |
                    hui-generic-entity-row {
                      padding-left: 44px;
                    }
            card_mod:
              style:
                .: |
                  ha-card {
                    margin-left: 1px;
                    margin-right: 1px;
                    #margin-top: -10px;
                    margin-bottom: -25px;
                    #box-shadow: none;
                  }

UI hacking is soooooo complicated. Learned a lot but still feeling like first hour in first class… :joy:

The 3rd card in my example contains different styles for each row.

Using “#” to comment does not work here.

The header will not be displayed since " show_state: false".

This kind of styles should be defined as:

            card_mod:
              style: |
                ha-card {

This row is not "hui-generic-entity-row".
Always use Code Inspector for styling:
изображение
Your element is “a”.

1 Like

Why is this better or needed?

Hey Ildar, getting back to this. It was one of the last cards I had still to perfectionize…

even with your exact config, the text of my template would still run through the images, because it crosses the half of the card, as you can see below.
Even after having cut the word Media in each attribute. (see the template in the config)

So I decided to give it a try an have them vertically stacked, first the markdown, below the image, and found only 2 ways to do so really.

1- a core entities card and set a background on that (and an empty entities list). We need to set the height of the background manually, and that is a bit of a trial and error, but, the end result is really nice

2- a custom:button-card, set to 1/1, no action (can change of course) and nothing fancy

for both cards I needed to find a way to fill the full width. 16px allround. this is what I did:

1- entities card

type: vertical-stack
cards:
  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      title: Plex Playing
      card_mod:
        style: |
          .card-header {
            background-color: var(--background-color-off);
            color: var(--text-color-off);
            padding-top: 0px;
            padding-bottom: 0px;
            margin: 0px 0px 16px 0px;
          }
    filter:
      include:
        - entity_id: 'media_player.plex*'
          state: /playing|paused|'on'/
          options:
            type: custom:hui-element
            card_type: markdown
            card_mod:
              style: |
                ha-card.type-markdown {
                  box-shadow: none;
                  margin: -8px -16px;
                }
            content: >
              {% set player = config.entity %}
              {% if states(player) not in ['unknown','unavailable'] %}
              {%- for attr in states[player].attributes %}
                {%- if not attr in ['media_content_id','icon','entity_picture', 'hide_attributes',
                                    'media_position_updated_at', 'media_duration','media_position',
                                    'player_source', 'media_content_rating',
                                    'is_volume_muted','username','friendly_name', 'volume_level',
                                    'supported_features'] %}
                {{attr.split('media_')[1]|replace('_',' ')|title}}: {{states[player].attributes[attr]|title}} {%- endif %}
              {% endfor %} User: {{state_attr(player,'username')}}
              {% else %} Nothing playing
              {% endif %}

          sort:
            method: name

  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      card_mod:
        style: |
          ha-card {
            margin: -8px 0px 0px 0px;
            box-shadow: none;
          }
    filter:
      include:
        - entity_id: 'media_player.plex*'
          state: /playing|paused|'on'/
          options:
            type: custom:hui-element
            card_type: entities
            card_mod:
              style: |
                ha-card {
                  background: url({{state_attr(config.entity,'entity_picture')}});
                  box-shadow: none;
                  background-size: cover;
                  margin: -16px;
                  height: 500px;
                }
            entities: []

and the button-card:

replace with

  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      card_mod:
        style: |
          ha-card {
            margin: -8px 0px 0px 0px;
            box-shadow: none; /* not really necessary because of button card mode below */
          }
          .card-content {
            margin: -16px;
          }
    filter:
      include:
        - entity_id: 'media_player.plex*'
          state: /playing|paused|'on'/
          options:
            type: custom:button-card
            aspect_ratio: 1/1
            show_name: false
            show_icon: false
            tap_action: none
            card_mod:
              style: |
                ha-card {
                  background: url({{state_attr(config.entity,'entity_picture')}});
                  box-shadow: none;
                  background-size: cover;
                }

result:

note the button really is square, the entities card seems a longer image.
On the iPhone this fits the full screen perfectly.

I also tried it with core picture card, but ofc it complains it needs an image… didnt really try hard yet, and maybe I can overcome that with config-template-card. That would require yet another custom-card, and I’d like to keep it as simple as can be.
didnt test a stack-in-card…
but for you I just did, and it works the same, can simply replace vertical-stack with custom:stack-in-card, and still need several of the mods, so no real advantage I guess

I think this construction is “more traditional”, more clear.
The “.:” means “select the current element” - but what is a meaning of using this for the 1st selector?
Ok, it is not a mistake to use the “.:” first - but it is less clear.

O.k. Thanks. Thought there is more in regard to performance or something else. Compare to my questions somewhere above in the thread.

Read this, I find it very useful.

Note that 99.99 % of my examples are WITHOUT optimization but could be a bit MORE CLEAR for new people since they show DOM navigation:
without optimization:

element:
  $:
    element:
      $: |
        element {
          ...
        }

with optimization:

element $ element $: |
  element {
    ..
  }

Yes. It is. Funny that this was one day before my question above. Didn’t see this before and we had the same questions more or less in parallel.

Only don’t get the leading $ till now

element-a:
  $ element-b $:

But most probably have to read and re-think the post several times again.

He explains:

will create one card-mod element in each element-a , and one in the shadowRoot of the first element-b in the each element-a .

But refering to this

E.g. the following will apply styles to the #shadow-root of the first action button in an alarm-panel card:

"#armActions mwc-button$": |

This already takes only the first. Or is there something missing in the explanation around “in the each”, e.g. “[only] in the [first shadow root of] each element-a”?

had another go at the Plex card:

type: vertical-stack
cards:
  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      title: Plex Playing
      card_mod:
        style: |
          .card-header {
            background-color: black;
            color: orange;
            padding-top: 0px;
            padding-bottom: 0px;
          }
    filter:
      include:
        - entity_id: 'media_player.plex*'
          state: /playing|paused|'on'/
          options:
            type: custom:hui-element
            card_type: markdown
            theme: plex # <-- set background for the markdown and still use a background-image
            card_mod:
              style: |
                ha-card.type-markdown {
                  background-image: url('/local/various/plex.png');
                  background-position: right;
                  background-size: contain;
                  background-repeat: no-repeat;
                  box-shadow: none;
                  color: silver;
                  margin: 8px -16px -16px -16px;
                }
            content: >
              {% set player = config.entity %}
              {% if states(player) not in ['unknown','unavailable'] %}
              <font color='orange'>**User:**</font> {{state_attr(player,'username')}}
              {% for attr in states[player].attributes %}
                {%- if not attr in ['media_content_id','icon','entity_picture','hide_attributes',
                                    'media_position_updated_at','media_duration','media_position',
                                    'player_source','media_content_rating',
                                    'is_volume_muted','username','friendly_name','volume_level',
                                    'supported_features'] %}
               <font color=orange> **- {{attr.split('media_')[1]|replace('_',' ')|title}}:**</font> {{states[player].attributes[attr]|title}} {% endif %}
              {%- endfor %}
              {% else %} Nothing playing
              {% endif %}
          sort:
            method: name

note:
I wanted to see if I could use the Plex icon as background-image, and that worked well, but, ofc, that removes the option for the background: black which I had set, as background for the Plex colors: orange for bold, silver for regular text.

I almost conceded, but then I suddenly remembered we can set a theme on Markdown cards…

created this huge theme:

plex:
  card-background-color: black

and set it in the markdown config. So now I can benefit from a background image And a background color!

One more improvement would be to use the User in the card title, must have a look how to do that most efficiently

this helps:

  - type: custom:auto-entities
    show_empty: false
    card:
      type: entities
      card_mod:
        style: |
          .card-content {
            margin: -16px;
          }
    filter:
      include:
        - entity_id: 'media_player.plex*'
          state: /playing|paused|'on'/
          options:
            type: custom:button-card
            template: button_default_title
            name: >
              [[[ return 'Plex playing: ' + states['this.entity_id'].attributes.username; ]]]
            show_icon: false
            styles:
              card:
                - color: orange
                - background-color: black

but now I have 3 times the same auto-entities filter…huh? it does work but seems awfully inefficient,

or, for the fun of it, mimicking the iPhone frontscreen:

            name: >
              [[[ var caption = 'Plex playing:&nbsp';
                  var title = states['this.entity_id'].attributes.media_title;
                  var artist = states['this.entity_id'].attributes.media_artist;
                  var album  = states['this.entity_id'].attributes.media_album_name;
                  return `<div style='display: flex; padding: 0px 16px 0px 0px;'>
                      <div>${caption}</div>
                      <marquee>
                      <span style='color:silver;align-items:center;'>${artist} - ${album} - ${title}</span>
                      </marquee>`;]]]

Feb-02-2022 15-57-33

could be smoother, but hey, who’s complaining

2 Likes

Hello Guys,

I have recently start using Dwain’s Dashboard. It look pretty awesome but I am struggling a bit with some CSS changes.

I have this CCS:

I want to apply extra width to the hui-vertical-stack-card , but I have tried serveral things. Nothing seems to work.

    - type: vertical-stack
      style:
         hui-vertical-stack-card:
           $: |
             .item {
               width: 850px;
             }

Can someone please assist me on this?

You need to wrap your vertical-stack inside a mod-card. Check for it on the card-mod docs.

Thx you for your answer.

I have already managed to fix it with te following


    - type: vertical-stack
      cards:
        - type: entities
          style: |
            ha-card {
              width: 950px !important;
              max-width: max-content;
            }
            :host {
              width: 950px;
            }