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

As a HASS newbie, Iā€™m losing my mind going round in circles simply trying to change the font size and colour of an entity state (in this case, a weather forecast).

I struggled for hours just trying to work out how to wrap text so the string fit, and ended up having some success with a button card (of all things?!). Now Iā€™ve spent the last hour just trying to resize the text! My card code is as follows:

  • show_name: false
    show_icon: false
    type: button
    tap_action:
    action: toggle
    entity: sensor.redcliffe_bom_extended_text_0
    show_state: true

Can someone please tell me what I need to do to increase the font size?

you mean like this:

type: button
entity: input_boolean.test
name: Button with card-mod
card_mod:
  style:
    ha-state-icon:
      $: |
       ha-icon {
        color: {{'red' if is_state(config.entity,'on') else 'green'}};
       }
    .: |
      ha-card {
        color: {{'green' if is_state(config.entity,'on') else 'red'}};
        background: {{'black' if is_state(config.entity,'on') else 'gold'}};
        font-size: 40px !important;
      }

1 Like
background: #443EF4;

Delete the quotes for hex colors, then it will work ā€¦

2 Likes

I ended up getting it to work with this:

styles:
  card:
    - font-size: 20px
  state:
    - text-wrap: wrap

However, for some reason the wrap text works on my PC and phone, but not my tablet :frowning: Could this be that the tablet is too old (Android 6, Chrome 106.0.5249.126)?

Yes, you still have quotes here.

1 Like

How can I move only text to the bottom

type: custom:mushroom-template-card
layout: vertical
primary: livingroom
icon: mdi:sofa
icon_color: "#C63C51"
entity: input_boolean.vacuum_room_7
tap_action:
  action: toggle
fill_container: false
layout_options:
  grid_columns: 1
  grid_rows: auto
multiline_secondary: true
card_mod:
  style:
    mushroom-shape-icon$: |
      .shape {
        --shape-color: transparent;
        --icon-symbol-size: 30px;
        --icon-size: 0px;
      }
    .: |
      ha-card {
        box-shadow: 4px 4px 4px 0px rgba(0,0,0,0.66);
        background: 
          {% if ( (is_state('sensor.l20_ultra_current_room', 'Living room') and (is_state('sensor.l20_ultra_task_status', 'cleaning_paused') or is_state('sensor.l20_ultra_task_status', 'cleaning'))) or is_state('input_boolean.vacuum_room_7', 'on') ) %}
            #ffb152
          {% else %}
            "#ffffff"
          {% endif %};
        --card-primary-font-size: 11px;
        --card-primary-font-weight: bold;
        --card-secondary-font-size: 12px;
        --card-primary-color: black;
        --card-secondary-color: black;
        margin: 0px 0px 0px 0px !important;
        padding: 7px 0px 0px 0px !important;
        border: 0.1px outset gray;
        
      }
      ha-card:active {
        transform: scale(0.975);
        transition: 0s;
      }

Screenshot from 2024-12-15 11-15-31

This is not card_mod but a config setting in custom:button-card so completely unrelated to this thread, or your question to which I posted my help

Iā€™m attempting to make my moon cardā€™s icon bigger, and Iā€™ve been over the documentationā€¦this code feels like it should work, but it doesnā€™t.

The card in question: HA-TSMOON

type: custom:tsmoon-card
language: en
card_mod:
  class: middle-contained
  style:
    ha-card $:
      card-content $:
        entity-row $:
          icon $:
            img $: |
              .moon-img-svg {
                height: 10vh;
                width: 10vw;
                }

Reading other cases, it seems that unless youā€™re drilling down through a shadow-root, you donā€™t have to do the string of selectors thing, but Iā€™ve tried just .moon-img-svg and that didnā€™t work either, as well as several variations of the above.

Try without ha-card.

My apologies - Iā€™m very new at all this.

Iā€™m afraid that doesnā€™t work, either.

In your screenshots the relevant tags are not within shadow roots. You should be able to directly style

card_mod:
  style: |
              .moon-img-svg {
                height: 10vh;
                width: 10vw;
                }

Depending in order of assigment with !important

besides the exercise of getting this to work, why not try a different approach that focuses on the design you truly prefer? seems a button card with the Moon picture and those singled out attributes would be more to your liking.

enlarging the entity_picture in this cardā€™s design would probably blow it out of proportion?

blowing it up, use a 1 column gridā€¦:

or 2

and then use card_mod to kill the card gap and resize a bit

It seems that the issue was that I never tried this solution:

card_mod:
  style: |
    .moon-img-svg {
      height: 10vh !important;
      width: 10vw !important;
      }

With !important. Sometimes getting these selectors to work is an exercise in frustration.

Enlarging the picture does in fact make it out of proportion to the text, but now that I have the selector correct, I can resize the text. Wondering where youā€™re pulling your image from? Iā€™ve looked a few times for linkable photos and come up empty. Thatā€™s a clean-looking card!

see this Moon platform with moon phases pictures - #23 by Mariusthvdb on how the template is done, but you can also directly add those to a button-card ofc.

Hide because off topic button-card
type: custom:button-card
template: #button_body
  - main_config
  - styles_grid
#   - styles_label
  - styles_name_left
  - styles_state
entity: sensor.moon
show_state: false
icon: none
name: >
  [[[ var moon = states['sensor.moon'].state;
      var phases = {new_moon:'Nieuwe Maan',
                    waxing_crescent:'Jonge Maansikkel',
                   first_quarter:'Eerste Kwartier',
                   waxing_gibbous:'Wassende Maan',
                   full_moon:'Volle Maan',
                   waning_gibbous:'Afnemende Maan',
                   last_quarter:'Laatste Kwartier'};
      return phases[moon] || 'Asgrouwe Maan'; ]]]
styles:
  card:
    - background-image: >
        [[[ return `url("${states['sensor.moon_phases'].attributes.entity_picture}")`; ]]]
    - background-repeat: no-repeat
    - background-size: 100%
    - background-position: center
    - color: ivory

I suppose the moon pictures are to be found in the community freely, just search for them

the entities card on the right side is a core entities card, so nothing special (other than using type: attribute)

Can I also flex a little please? :sweat_smile: :rofl: Try my lunar phase card, where you have a lot of info about tracking moon phases :point_down: :sweat_smile:

3 Likes

haha sure, how could I forget :wink:

off topic thoughā€¦

I appreciate all the responses, as well as the cool moon card I did not know existed, so thank you VietNgoc. :slight_smile:

Can I ask, so that I can understand better:

Why doesnā€™t my first solution work? If Iā€™m reading the documentation correctly, it should work whether there is a #shadow-root in the way, or not, correct? Why isnā€™t it matching all the classes there?

I have a little tip for beginners with cardmod. Use this excellent helper. Follow the helper instruction. From the converted output, copy everything from the end to the ha-card selector. And insert into card under card_mod style with this as the current element.

.: |

https://matt8707.github.io/card-mod-helper/

In your case, the result looks like this
Later, after some trial and error, you will surely find out where what is under what and howā€¦

type: custom:tsmoon-card
card_mod:
  style:
    .: |
      ha-card>div.card-content>div>div.icon>img {
        height: 10vh;
        width: 10vw;
      }

1 Like

Hello

I need help on installation.

my configuration.yaml

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes
  extra_module_url:  
  - /www/community/lovelace-card-mod/card-mod.js

and the yaml code on a switch card

type: tile
entity: switch.eg_hwr_licht_eg_hwr_licht
card_mod:
  style: |
    ha-card {
      color: red;
    }

But the Text on card isnā€™t red.

does anyone see the mistake?