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

I have this card for controling my TVR

Przechwytywanie

features:
  - type: climate-hvac-modes
    hvac_modes:
      - auto
      - heat
      - 'off'
type: tile
entity: climate.zigbee_grzejnik_salon
name: Grzejnik salon
icon: mdi:radiator
tap_action:
  action: more-info
card_mod:
  style: |
    ha-card {
      position: relative;
    }
    ha-card::before {
      content: '{{ states("sensor.zigbee_grzejnik_salon_position") }}%';
      position: absolute;
      top: 16px;
      right: 16px;
      z-index: 1;
      font-weight: bold;
    }

Iā€™ve managed to get the position state to show on the card, but now how can I get more info of the position sensor when clicking it? Also can I add a valve icon in front of the position %?

1 Like

Thanks, very helpful! I replicated your headings card-mod code but I canā€™t figure out how to vertically center the text. Any thoughts?

You seem to have changed a height of the heading, right? Probably you need to align a container (which includes all elements) vertically. Check in Code Inspector, find this container and play with corr attributes, cannot tell more, no access to pc

Nope. The heading card has this extra space on top if itā€™s not contained in a vertical stack card. Itā€™s truly infuriating.

This is called a margin-top. Try to reduce it then.

Trying to implement the following as a template:

card_mod:
  style: |
    /* Styles for mobile devices */
    @media (max-width: 767px) {
      ha-card {
        position: fixed;
        bottom: 0px;
        left: 0px;
        width: 100%;
        height: 80px;
        box-shadow: 0px -5px 10px rgba(0, 0, 0, 0.3);  
      }
    }
    /* Styles for desktop devices */
    @media (min-width: 766px) {
      ha-card {
        position: fixed;
        bottom: 0px;
        left: 0px;
        width: 20%;
        height: 75px;
        padding: 5px;
      }
    }

However when putting the following in the theme file:

.nav-bar {
  /* Styles for mobile devices */
    @media (max-width: 767px) {
      ha-card {
        position: fixed;
        bottom: 0px;
        left: 0px;
        width: 100%;
        height: 80px;
        box-shadow: 0px -5px 10px rgba(0, 0, 0, 0.3);  
      }
    }
    /* Styles for desktop devices */
    @media (min-width: 766px) {
      ha-card {
        position: fixed;
        bottom: 0px;
        left: 0px;
        width: 20%;
        height: 75px;
        padding: 5px;
      }
    }
}
card_mod:
  class: nav-bar

and assigning the card mod as a template, it doesnā€™t seem to take. Not sure how to utilize the @media functions within a template.

I have this weird issue that my column id (in hui view) has a certain width but the class for some reason has a max-width (purple is the ā€œpaddingā€)
image

It is basically nor dynamically adjusting to the screen width (e.g. when I flip the phone)

My theme file does not command any sizes or widths and each view starts with a simple vertical-stack. I tried to apply widths directly to the cards to no avail.

The only thing I could think of was my use of kiosk mode but even without it, the column width is not dynamic.

Any help is appreciated. Iā€™m running out of ideas here.

This is wrong syntax. You placed a media query between 2 selectors. It must be topmost. Imagine that simple removing media query should not break the syntax - but it breaks in your case.

Not quite sure what youā€™re suggesting to write out. I tried this as well:

card-mod-theme: theme
card-mod-card: | 
  /* Styles for mobile devices */
  @media (max-width: 767px) {
    ha-card(.nav-bar)  {
      position: fixed;
      bottom: 0px;
      left: 0px;
      width: 100%;
      height: 80px;
      box-shadow: 0px -5px 10px rgba(0, 0, 0, 0.3);  
    }
  }
  /* Styles for desktop devices */
  @media (min-width: 766px) {
    ha-card(.nav-bar) {
      position: fixed;
      bottom: 0px;
      left: 0px;
      width: 20%;
      height: 75px;
      padding: 5px;
    }
  }
card_mod:
  class: nav-bar

No success

This is also wrong.
Do you want to say that ā€œnav-barā€ class contains ha-card element?
Anyway, suggest you first to achieve your goal in a theme without using media query - to find out a proper DOM path.

If ā€œhav-barā€ is a class of a card - then probably a proper path should be ā€œha-card.nav-barā€.
So try this (untested):


card-mod-card: | 
  /* Styles for mobile devices */
  @media (max-width: 767px) {
    ha-card.nav-bar  {
      ā€¦.
    }
  }

Also, discussing themes is not supposed to be done here, find a dedicated card-mod-themes thread.

I knew that there was a proposal for that, but I learned that it became baseline just some months ago. If he is using one of the major browsers, that sintax should not be the issue, the issue should be somehere else.

This is the link to the documentation in MDN. Now it is possible to make all those cool stuff directly in CSS :slightly_smiling_face:

Yup, tried that too and doesnā€™t work. Iā€™m not having much luck with templating and this card mod.

I even do something simple like the following:

card-mod-theme: blue
card-mod-card: |
  ha-card.test_nav {
        border-radius: 10px;
  }

Then try to use it like so:

type: custom:mod-card
card_mod: 
  class: test_nav

and Iā€™m not getting it to recognize. Even though if I do the same thing:

type: custom:mod-card
card_mod: 
  style: |
    ha-card {
      border-radius: 10px;
    }

It works

ā€¦. without using a selector like ā€œha-cardā€ or whatever.
So, this variant theoretically shoul work:


card-mod-card: | 
  ha-card.some-class {
    @media (max-width: ā€¦) {
      color: red;
    }
    @media (max-width: ā€¦) {
      color: green;
    }
  }

It will work too. One thing that doesnā€™t work is class concatenation, but nowadays you can write almost a SASS code and it will be interpreted by the browser.

What card are you trying to style?
Mod-card is supposed to be used only when a card does not have ha-card.

This is new world)
The only thing I can use as an excuse that I only started dealing with css when started using card-mod)))

Well I have been using CSS for a long time and this is a new world also for me :sweat_smile:
I would not use it in production code yet though, I would wait for 90+ compatibility.

Hello! I am trying to increase the icon size of the built-in weather card (forecast) using card mod. I am a newbie with CSS. I went to the design inspector in Firefox and inspected the first icon on the card. Hereā€™s the screenshot:

If I change the width and height of .forecast-image-icon to 64px in the design inspector, it works as intended. All the forecast icons get bigger. I have another card for weekly forecast. Itā€™s icons get bigger too, which is fine. I think I updated the .forecast-image-icon class attributes globally.

Anyways, I tried this with the card mod:

show_current: false
show_forecast: true
type: weather-forecast
entity: weather.blah
forecast_type: hourly
theme: Animated Weather Icons Filled
card_mod:
  style: |
    ha-card div.forecast-image-icon 
    {
      width: 64px;
      height: 64px;
    }

It didnā€™t work. I canā€™t post more pictures since I am a new user. Let me post them in a new post. I appreciate any help.

Well, if your styles work in Code inspector - try adding ā€œ!importantā€ in your code.
Cannot say more, answering from mobile.

Hereā€™s the image after my card mod changes:

image

Not what I expected. It pushed down the temperature values at the bottom.