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

And why do you not show your faulty jinja if statement then?

Try this:

background: {{iif (is_state('switch.sonoff_tavolo_switch_1',  'on'), 'yellow', 'blue'}}

Yellow will be the color when the switch is on, blue for when itā€™s off.

1 Like

Thank you so much, itā€™s worked.
Sorry if I take advantage of your kindness, but I would have another problem. I have now set the buttons to 50% of the width because I wanted to put them side by side, but I see that anyway on my lovelace there are only 3 columns. How do you increase them and maybe put all the buttons for which I have set the width to 50%?

I think the easiest way to do that would be to wrap your buttons in a horizontal stack.

1 Like

Not exactly sure I understand what youā€™re trying to do. But take a look at the grid card.

Itā€™ll let you put your buttons into one of the lovelace columns. You can then specify how many columns / rows you want, and itā€™ll scale the button cards to fit. It can replicate both / either horizontal or vertical stacks, plus more. The next 3 rows are a single 2x3 grid.

The top row of this image is a 1x5 grid, if I add another button, Iā€™d probably make it a 2x3 grid and everything will resize. Both grid cards are nested in a vertical-stack (although I could have used a 1x2 grid :slight_smile: )

1 Like

Could @Ildar_Gabdullin or anyone else help me out and point me in the right direction for adding CSS styles to the regular standard grid card.? I donā€™t think this is something that normally gets styled so thereā€™s nothing really on it in the forums. Iā€™m specifically trying to add styling to accomplish two things on the grid card.

  1. Add styling that will change the spacing between each of the cards within the grid. Iā€™m trying to increase the space on the side of each card in the grid because some of the cards in my smaller grids look like theyā€™re almost on top of each other. I found one way to do this using mod-card and it works but I want to see if there is a better way or if this is the correct way.
- type: custom:mod-card
  card_mod:
	style: |
	  :host {
		--grid-card-gap: 10px;
		}
  card:
	type: grid
	title: null
	square: true
	columns: 4
	cards:
	  - type: custom:button-card
            (etc. etc.)
  1. Add styling that will increase the top and bottom margins of the grid to create more space between the grid and the top/bottom of the cards within it. The problem Iā€™m having is the top of the box-shadow style gets cut off on the cards in the top row of the grid. I think this is because the cards in the top row of the grid sit flush or almost flush with the edge of the grid card itself. I used the browser element inspector to increase the top-margin and that solves the problem but Iā€™m lost as to how to correctly add the styling to the grid card. Shadow roots and all of that still confuse me.

hi
Is it possible to change color as soon as the situation changes from Home to Not_HOME

action: fire-dom-event
browser_mod:
  service: browser_mod.popup
  data:
    title: Device Tracker
    size: fullscreen
    content:
      type: grid
      columns: 2
      square: false
      cards:

        #01
        - type: custom:bar-card
          width: 45%
          height: 1.5em
          positions:
            icon: outside
            indicator: 'off'
            name: outside
          entity_row: false
          entities:
            - entity: device_tracker.amazon_echo_dot
            - entity: device_tracker.amazon_echo_show
            - entity: device_tracker.khdr_mkhshbym_deco
            - entity: device_tracker.living_room_deco
            - entity: device_tracker.qvmh_shnyh_deco
            - entity: device_tracker.oven
          columns: 2
          square: true

Iā€™ve searched and searched and cannot find an answer.
Is it possible with card_mod to remove the background and shadow from an entities card within an auto-entities card?

     - type: conditional
       conditions:
          - entity: sensor.upcoming_anniversaries
            state_not: '0'
        card:
          type: custom:auto-entities
          card:
            type: entities
          filter:

Thank you so much! It works!!!

I would like to know, I was trying to change the background also for the media control cards, but they donā€™t do it with the same code, why ???

type: media-control
entity: media_player.fire_tv_192_168_1_16
card_mod:
  style: |
    ha-card {
      background: {{iif (is_state('media_player.fire_tv_192_168_1_16', 'off'), 'rgb(255, 134, 0)', '')}};
      border-radius: 5px;
      overflow: hidden;
      border: solid 0px rgb(0,0,0,255);
      width: 100%;
      height: 100% }

Iā€™m not able to set the height of a state-switchesā€™ div to 100% to have the same height like the other containers without a state switch. I tried a lot of things, but it still doesnā€™t work.
The div#root has to be set to 100%, but Iā€™m struggling with the solution.

2022-09-27 12_30_51-Window
2022-09-27 12_28_17-Window

type: custom:state-switch
entity: sensor.power_mode
card_mod:
  style: |
    div#root {
      height: 100% !important;
    }
states:
  import:
    type: custom:mini-graph-card
    name: netzimport
    update_interval: 10
    cache: true
    hour24: true
    hours_to_show: 2
    points_per_hour: 30
    decimals: 0
    lower_bound: 0
    icon: mdi:arrow-down-bold
    show:
      labels: true
      points: false
    entities:
      - entity: sensor.shelly_3em_channel_a_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_3em_channel_b_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_3em_channel_c_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_power_import
        show_legend: false
        show_state: true
        show_graph: false
    card_mod:
      style: |
        :host {
          height: 100%;
        }
        ha-card {
          height: 100%;
          align-items: center;
        }
        header, .states {
          font-weight: inherit !important;
        }
        .icon {
          position: absolute;
          right: 16px;
          color: orange !important;
        }
        .name > span {
          color: var(--ha-card-header-color, --primary-text-color) !important;
          font-size: var(--ha-card-header-font-size, 24px) !important;
          opacity: inherit !important;
          font-weight: inherit !important;
        }
        .false:first-child {
          display: none;
        }
        .states--secondary {
          margin-left: 0 !important;
        }
        .ellipsis {
          margin: 0 2px !important;
          opacity: unset !important;
          font-size: 2.4em !important;
          line-height: 1.2em !important;
        }
  export:
    type: custom:mini-graph-card
    name: netzexport
    update_interval: 10
    cache: true
    hour24: true
    hours_to_show: 2
    points_per_hour: 30
    decimals: 0
    lower_bound: 0
    icon: mdi:arrow-up-bold
    show:
      labels: true
      points: false
    entities:
      - entity: sensor.shelly_3em_channel_a_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_3em_channel_b_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_3em_channel_c_power
        show_legend: false
        show_state: false
      - entity: sensor.shelly_power_export
        show_legend: false
        show_state: true
        show_graph: false
    card_mod:
      style: |
        :host {
          height: 100%;
        }
        ha-card {
          height: 100%;
          align-items: center;
        }
        header, .states {
          font-weight: inherit !important;
        }
        .icon {
          position: absolute;
          right: 16px;
          color: green !important;
        }
        .name > span {
          color: var(--ha-card-header-color, --primary-text-color) !important;
          font-size: var(--ha-card-header-font-size, 24px) !important;
          opacity: inherit !important;
          font-weight: inherit !important;
        }
        .false:first-child {
          display: none;
        }
        .states--secondary {
          margin-left: 0 !important;
        }
        .ellipsis {
          margin: 0 2px !important;
          opacity: unset !important;
          font-size: 2.4em !important;
          line-height: 1.2em !important;
        }
view_layout:
  grid-area: graph_one

Tried this also:

card_mod:
  style:
    state-switch:
      $: |
        #root {
          height: 100% important;
        }

Does it work when you replace the iif statement with a simple color?

No, nothing

You wrote ā€œiifā€ā€¦ Typo?

1st post ā†’ link at the bottom ā†’ styles for media-player

And next time please post your failed attempts w/o any excessive code which is not related to the issue.
Same is about using jinjia templates. First achieve a desired result w/o templates ā†’ then add templates.

Why should it?

Beside, that this is idea is wrong at all, because there is of course a iif, instead of guessing, Michele should always have a first look at the examples from Ildar and then have a look in the html dom, and try there changes in the browser. And you will directly see, that the background CSS attribute in ha-card is not related to the background to change.

1 Like

Good morning Ildar and thanks for the suggestions. I had completely missed all those examples you posted on the discussion, now I will certainly have less problems to configure my lovelace. Thanks again, and thanks for the tips on how to post, Iā€™ll treasure it.

Edit: Iā€™ve seen your posts, and used for my case this code

type: media-control
entity: media_player.kodi_iiyama
card_mod:
  style: |
    .background.no-image > * {
      background-color: cyan;
    }   

my question is simple: itā€™s possible to add an IF for choose background color according to the state of the media? E.g. Iā€™ve a PS4, itā€™s in IDLE status, but I see only cyan, and when itā€™s turned off I canā€™t choose the color. @Ildar_Gabdullin please can you help me?

No, Jinja: Templating - Home Assistant

1 Like

Fixed it myself with

type: custom:mod-card
card_mod:
  style:
    $: |
      :host {
        height: 100%;
      }
    state-switch $: |
      #root {
        height: 100%;
      }
      #root > .visible > mini-graph-card {
        transform: none;
      }

Here could be a confusion for a person who finds this post.

There are two parts in your code:
ā€“ fixing a height (your issue);
ā€“ fixing a glitch with state-switch (your issue on Github).

These are unrelated things. The ā€œtransform: noneā€ part is not related to the ā€œheight issueā€.

Solution for ā€œstate-switch issueā€ was provided here.

Example of ā€œif ā€¦ else if ā€¦ else ā€¦ā€

What is ā€œOFFā€? Means - the ā€œmedia_playerā€ is ā€œunavailableā€?
If yes - then the color is greyish (in a default light theme), then a different selector (not ā€œbackground.no-imageā€) must be used.