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

see:

- title: Map mods
  path: map_mods
  icon: mdi:map
  badges:
  cards:

# This mod is functional for listed map entities
    - type: map
      entities:
        - person.marijn
      card_mod:
        style:
          ha-map$: |
            {% for entity in config.entities %}
              {% if is_state(entity, 'home') %}
                div:has(> ha-entity-marker[entity-id="{{ entity }}"]) {
                  filter: grayscale();
                }
              {% endif %}
            {% endfor %}

# This exact same mod is not functional on the auto-entities filtered entities, 
#  and needs to adapted to use the initial entities mapper list
    - type: custom:auto-entities
      card:
        type: map
        card_mod:
          style:
            ha-map$: |
              {% set entities = config.entities | map(attribute='entity') | list %}
              {% for entity in entities %}
                {% if not is_state(entity, 'not_home') %}
                 div:has(>  ha-entity-marker[entity-id="{{ entity }}"]) {
                    filter: grayscale(1) brightness(1.75);
                  }
                {% endif %}
              {% endfor %}
      filter:
        include:
          - domain: person

# Note:
# In Chrome we can mod directly on the ha-entity-marker[] element, but for Safari the
# extra div:has() is required

wow, that been huge progress @elchininet , thank you very much!

The only thing2 things I now seek are:

1 - a way to apply the mod on the options option of the filtered entities:

    card:
      type: custom:auto-entities
      show_empty: false
      filter:
        include:
          - domain: person
            not:
              state: home
            options:
              card_mod:
                style:
                  ha-map$: |
                  ???

Usecase: this is an ultimate abridged config, and other aspects are added in various cards, that should Not get the generic config.entities Map mod.
It seems to require a completely different Dom path though, and ofc the use of this.entity_id

2 - a way to mod the more-info of a person entity with the same template logic. As this would be system wide, I am hoping to get card-mod theming to pick that upā€¦


this seems to become a too complex way of getting an entity_picture to grayscale in all spots in the UI.
I just realized the more-info has the map And the entity_pictureā€¦

A dedicated plugin in the end will be much more efficientā€¦

or a core option to customize the entity_picture states ofcā€¦ like device_class sort of thing

Nop, that is not the same :wink:

## Correct
ha-entity-marker[entity-id="person.somebody"]

## Incorrect
ha-entity-marker[entity-id="{'entity': 'person.somebody'}"]

Thatā€™s right, first problem was the wrong template, second problem is the Safari issue. You have mixed both well.

Youā€™re welcome :+1:

I think that should not be done with card_mod, because the entities are not cards.

For this maybe a card_mod theme will be more suitable.

Thatā€™s right, card_mod should be used to modify small and specific things in specific cards. If you want something global that works everywhere, then a dedicated plugin is the way to go.

ultimately, yes, that is what I would love to see.

just consider being able to so entity entity_picture ā€˜translationsā€™ like we can do icon_translations currently.
take device_trackers or person and make that work for entity_pictures

{
  "entity_component": {
    "_": {
      "default": "mdi:account",
      "state": {
        "not_home": "mdi:account-arrow-right"
      }
    }
  },

picture:

{
  "entity_component": {
    "_": {
      "default": "person_home.png",
      "state": {
        "not_home": "person_not_home.png"
      }
    }
  },

ofc, this is too simple, but I hope you get what I am after. It would be not setting a filter but referencing an actual secondary picture.

Anyways, if you have any ideas how this could be done, please DM, because getting off topic here

1 Like

Just got to testing this - works fantastically - thank you very much!

1 Like

config.entity + mod-card

We use ā€œconfig.entityā€ in many places like Entities card, Entity card, Glance card etc - like here:

  - type: entities
    entities:
      - entity: input_boolean.test_boolean
        card_mod:
          style:
            hui-generic-entity-row $: |
              state-badge {
                {% if is_state(config.entity,'on') -%}
                  color: red;
                {%- else -%}
                  color: cyan;
                {%- endif %}
              }

image

image

Also we can use ā€œconfig.row.entityā€ in conditional rows - like here:

type: entities
entities:
  - type: conditional
    conditions:
      - entity: input_boolean.test_boolean
        state: 'on'
    row:
      entity: input_boolean.test_boolean_2
    card_mod:
      style:
        hui-toggle-entity-row $:
          hui-generic-entity-row $: |
            state-badge {
              {% if is_state(config.row.entity,'on') -%}
                color: red;
              {%- else -%}
                color: cyan;
              {%- endif %}
            }

image

image

Now assume we are using mod-card for some row-like card:

decl_test_mod_card_config:
  card:
    type: custom:mod-card
    card:
      type: custom:multiple-entity-row
      entity: '[[ENTITY]]'
      name: some name
    card_mod:
      style:
        multiple-entity-row $:
          hui-generic-entity-row $: |
            state-badge {
              {% if is_state('[[ENTITY]]','on') -%}
                color: red;
              {%- else -%}
                color: cyan;
              {%- endif %}
            }

image

image

The template uses the ā€œENTITYā€ variable.
But it also can use ā€œconfig.card.entityā€ variable:

            state-badge {
              {% if is_state(config.card.entity,'on') -%}
                color: red;
              {%- else -%}
                color: cyan;
              {%- endif %}
            }

It can be useful in case of ā€œabstractingā€ parts of code.

1 Like

Can someone help me with some missing syntax? In spite of all the reading Iā€™ve done, Iā€™m missing something fundamental between moving card-mod styles from a card to a theme.

This works using card-mod with a Mushroom Select card:

card_mod:
  style:
    mushroom-select-option-control$:
      mushroom-select$: |
        .mdc-select__anchor {
          border-radius: 30px !important;
        }

Therefore, I thought it would be easy to move it to a theme. However, the following does not work:

  card-mod-card-yaml: |
 
    .type-custom-mushroom-select-card$:
      .mushroom-select-option-control$:
        mushroom-select$: |
          .mdc-select__anchor {
            border-radius: 30px !important;
          }

I assume Iā€™m off somewhere with the .type-custom-mushroom-select-card$: but Iā€™ve tried a lot of different things. Now Iā€™m not sure if I need to be chasing shadow root stuff, indentation, etc. Any help would be greatly appreciated!

Fix for a restriction card inside a Grid with square cells

Test code:

  type: grid
  square: true
  cards:
    - type: custom:restriction-card
      card:
        type: entity
        entity: sun.sun
    - type: entity
      entity: sun.sun
    - type: history-graph
      entities:
        - sun.sun
    - type: custom:restriction-card
      card:
        type: entity
        entity: sun.sun
  columns: 2

Default look:

Modded:

type: custom:mod-card
card_mod:
  style:
    hui-grid-card $:
      hui-card:
        restriction-card $: |
          :first-child, div#card {
            height: 100%;
          }
        .: |
          restriction-card {
            height: 100%;
          }
card:
  type: grid
  square: true
  ...

image

Issue on Github.

1 Like

I want to change the opacity of the selected part, so it looks like the other parts, but I canā€™t figure out how to target it.

I managed to target it.

image

image

but when I make my background like the other parts, the opacity change doesnā€™t work.

image
image

Hi,
I have an issue with the transparency of my mushroom cards on my mobile (iOS device). Everything is perfect on PC screen tough !
I searched this forum for days but couldnā€™t find an answerā€¦

On my PC screen:
image

On my phone:

sample of code of ā€˜Prime Videoā€™ key

type: custom:mushroom-template-card
primary: ''
secondary: ''
icon: phu:prime-video
badge_color: cyan
tap_action:
  action: perform-action
  perform_action: media_player.select_source
  target:
    entity_id: media_player.salon
  data:
    source: Prime Video
icon_color: light-blue
card_mod:
  style:
    mushroom-shape-icon$: |
      .shape {
        box-shadow: 0px 8px 15px 0px rgba(0,0,0,0.36) !important;
      }
    .: |
      :host {
        --mush-icon-size: 80px; 

      ha-card {
        background: none;
        width: 110px;
        box-shadow: none;
        padding-left: 15px !important;
          
        
      }       

Do you have an idea where the issue is ?
Thank you in advance !

1 Like

Headings card

Some super useful/useless styles:

heading

code
type: vertical-stack
cards:
  - type: heading
    icon: mdi:fan
    heading: some long long long long long long title
    heading_style: title
    badges:
      - type: entity
        entity: sun.sun
        icon: mdi:fan
      - type: entity
        entity: fan.kitchen
    card_mod:
      style:
        .badges hui-heading-badge:nth-child(1) hui-entity-heading-badge $: |
          ha-heading-badge {
            --icon-color: red;
          }
          state-display {
            color: magenta;
          }
          ha-state-icon {
            animation: rotating 2s linear infinite;
          }
          @keyframes rotating {
            0% {transform: rotate(0);}
            100% {transform: rotate(359deg);}
          }
        .badges hui-heading-badge:nth-child(2) hui-entity-heading-badge $: |
          ha-heading-badge {
            --icon-color: orange;
          }
          state-display {
            color: red;
          }
          ha-state-icon {
            animation: rotating 2s linear infinite;
          }
          @keyframes rotating {
            0% {transform: rotate(359deg);}
            100% {transform: rotate(0);}
          }
        .: |
          ha-card {
            background-color: lightgreen !important;
            border-radius: var(--ha-card-border-radius, 12px);
            border-width: var(--ha-card-border-width, 1px);
            border-style: solid;
          }
          .title > p {
            color: red;
            white-space: break-spaces !important;
          }
          .title ha-icon {
            --icon-primary-color: yellow;
            --mdc-icon-size: 32px;
            animation: rotating 2s linear infinite;
          }
          @keyframes rotating {
            0% {transform: rotate(0);}
            100% {transform: rotate(359deg);}
          }

  - type: entities
    entities:
      - entity: sun.sun
      - entity: sun.sun
3 Likes

Regarding the earlier discussion about styling a Map card:
Some time ago a card-mod based styling for markers was not persistent after F5 (post) - and we have to use an external js-file described here.
Retested the card-mod way - seems to be stable:

  - type: map
    entities:
      - device_tracker.xxx
    card_mod:
      style:
        ha-map $ ha-entity-marker $: |
          .marker {
            border: 1px solid red !important;
            background-color: yellow !important;
          }

image

was experimenting with this too just now, and looking for a way to style the individual entity:

        entities:
          - entity: sun.sun
            color: state
            card_mod:
              style:
                ha-heading-badge:
                   $: |
                     .state-display {
                        color: red !important;
                     }
          - entity: sun.sun
            color: state

any chance you could help out here too?

usecase: see the post on the class in the card-mod theme just now, and requiring an override of those entities state colors

btw, I now spot that the official yaml (like you use) is with the badges, and type: entityā€¦ I had simply jotted this down:

      - type: heading
        heading: Speciale Scenes
        heading_style: title
        card_mod:
          class: class-section-heading
        entities:
          - entity: sun.sun
            color: state
            card_mod:
              style:
                ha-heading-badge:
                   $: |
                     .state-display {
                        color: red !important;
                     }
          - entity: sun.sun
            color: state

and those entities are picked up too :wink:
Iā€™ll change that so the card-mod wont be affected by that. (still need the state-display to be modded, the icon is then car of by the color: state )

following your mods above, and considering it starts at the actual entity badge, I tried to use:

        badges:
          - type: entity
            entity: sun.sun
            color: state
            card_mod:
              style: |
                hui-entity-heading-badge $: |
                  state-display {
                    color: red !important;
                  }

but still no luck, the mod isnt applied in the path

image

Glance card - trying to make the name boldā€¦ (or red at this stage, but i actually want it bold)

  • <div class="name" title="Cat Flap"><!--?lit$203497311$-->Cat Flap</div>

Iā€™ve tried every variant of this I can think of

e.g.

    - type: glance
      entities:
        - entity: lock.harley_s_flap_locked_in
          name: Cat Flap
          fill_container: true
          state_color: true
          tap_action:
            action: none
          hold_action:
            action: toggle
      card_mod:
        style: |
          ha-card {
            background: {{ 'rgba(200, 0, 0, 0.05)' if is_state('lock.harley_s_flap_locked_in', 'unlocked') else 'rgba(230, 177, 6, 0.1)' if is_state('lock.harley_s_flap_locked_in', 'locking') else 'rgba(230, 177, 6, 0.1)' if is_state('lock.harley_s_flap_locked_in', 'unlocking') else 'rgba(0, 200, 0, 0.05)'}}; 
      style:
        .entities .entity:
          $: | 
            .name {
              font-weight: red;
            }
      card_mod:
        style: |
          ha-card {
            background: {{ 'rgba(200, 0, 0, 0.05)' if is_state('lock.harley_s_flap_locked_in', 'unlocked') else 'rgba(230, 177, 6, 0.1)' if is_state('lock.harley_s_flap_locked_in', 'locking') else 'rgba(230, 177, 6, 0.1)' if is_state('lock.harley_s_flap_locked_in', 'unlocking') else 'rgba(0, 200, 0, 0.05)'}}; 

        .entities .entity:
          $: | 
            .name {
              font-weight: red;
            }

But nothing seems to change the font in questionā€¦
I managed to sort the background without too much trouble, but making something bold should be easier than this :sob: :rofl:

With this code:

type: entities
entities:
  - entity: input_number.octet1
    name: Voer IP in
    icon: mdi:ip-network
  - entity: input_number.octet2
    name: ' '
    icon: mdi:none
  - entity: input_number.octet3
    name: ' '
    icon: mdi:none
  - entity: input_number.octet4
    name: ' '
    icon: mdi:none

I get this card:

image

I would like the card to look like this (quick and dirty ms paint job):
image

With horizontal stack the buttons get unusable due to the white space of the title element still being there. I thought of using card-mod to eleminate the title and icon element, only leaving the number input box, and then use horizontal stack but my card-mod skills are basicly none.

There doesnt seem to be an custom card providing only the input box. Does anyone know how to achive my goal?

Unrelated to my other post above, but funnily enough Iā€™m trying to do quite similar

image

I want to have my button horizontal in the same pane - if i do horizontal stack it bumps it over and looks disconnected.

(I also want to round my text boxes but thatā€™s a problem for another day!)

Do you want to style a particular entity in the Heading card?
The example I posted above contains styles for an icon & ā€œstate-displayā€.

yes, but that is using the mod on the nth number of entity, seen from the main header?

Iā€™d love to do that on the entity itself

Hi guys! I want ā€œripple effectā€ in weather-forecast card, when it gets clicked. In the video below, the card on the left has ā€œripple effectā€ but weather-forecast card on the right doesnā€™t.

20241003_154044

I ā€œbuttonizedā€ the weather-forecast card (itā€™s inside button-card, so that it looks the same as my other cards). Therefore I think either of below methods could work:

  1. add ā€œripple effectā€ to weather-forecast card
    OR
  2. make button-card intercept click event, instead of weather-forecast card.

My code (only relevant parts):

  1. button-card template to ā€œbuttonizeā€ other cards:
buttonize:
  show_icon: false
  show_name: false
  show_state: false
  styles:
    card:
      - border: none
      - color: '#4b5254'
      - background-color: 'rgba(250, 250, 250, 0.75)'
      - padding: 0px
      - box-shadow: inset 0 0 20px var(--state-inactive-color)
      - '--mdc-ripple-color': 'rgba(0, 0, 0, 0.8)'
      - '--mdc-ripple-press-opacity': 0.5
    grid:
      - grid-template-areas: |
          "inner_card"
      - grid-template-columns: auto
      - grid-template-rows: auto
  tap_action:
    action: more-info
  1. ā€œbuttonizedā€ weather card:
type: custom:button-card
template: buttonize
entity: weather.forecast_dom
custom_fields:
  inner_card:
    card:
      type: weather-forecast
      show_current: false
      show_forecast: true
      entity: weather.forecast_dom
      forecast_type: daily
      card_mod:
        style: |
          ha-card {
            padding: 0px 0px 4px 0px !important;
            border: none;
            background-color: transparent;
          }
          /* here goes much more styling, irrelevant for the question */