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

If you want to change the amount of space taken up by the four elements, thatā€™s not the html-element to target. The size of the four elements is defined by the flexbox at <div id="root" dir="ltr">, which is 9 rows higher up in your screenshot. The element that you have highlighted will change the distribution within the individual elements - my understanding is that thatā€™s not what you want, right?

I just tried to replicate your setup and noticed that for some reason the flexbox basically skips a hierarchy -but this should work (while youā€™re at it, maybe change to the new custom:stack-in-card):

type: custom:stack-in-card
cards:
  - type: custom:mod-card
    card_mod:
      style: 
        hui-horizontal-stack-card $: |
            div#root > :first-child > * {
              width: 43%;
              flex: auto; 
            }

            div#root > :nth-child(2) > * {
              width: 27%;
              flex: auto; 
            }
            div#root > :nth-child(3) > * {
              width: 15%;
              flex: auto; 
            }
            div#root > :last-child > * {
              width: 15%;
              flex: auto;
              display: flex;
              justify-content: flex-end;
            }
    card:

image

cool car by the way!

2 Likes

This is exactly what I needed, thanks so much!!!

Never wouldā€™ve figured out, nor do I understand why the extra ā€œ> *ā€ is needed now. If you could elaborate on that part, Iā€™d appreciate it a lot.

P.s.: tried changing to the stack-in-card, but the vertical alignment is off when I do that. Need to look into that one as well, thanks for the hint.

Thatā€™s just standard CSS selectors - if you frequently style your cards it would definitely make sense to read up on this - w3 schools, mozilla, and css-tricks have some great resources on this (css tricks is a godsend if you want to style flexbox or grid).

letā€™s start with why itā€™s even the root element that I mentioned
You were on the right track with your dev tools - if you had highlighted the #root element, this will be highlighted on your webpage as well and you can see that this is what creates the flex elements - this is also indicated by the flex chip in the dev tools. And then basically the direct children of this element are the ones that get their sizing from the flexbox based on different criteria (again, see css-tricks on flexbox regarding how you can style these - other approaches would have worked as well, you could also define the sizes dynamically which might even make more sense in your appplication ā†’ then the browser defines the best sizes which makes it much more responsive).

How to select the correct element
In this case, for some reason, it skips a hierarchy (donā€™t ask me why, I donā€™t understand that, it seems hui-card isnā€™t a real element) - you can see that be hovering over the children elements in the dev tools and see whatā€™s being selected. If you select the <mushroom-template-card> in your dev-tools youā€™ll see that indicated on your webpage. Thatā€™s also where I found the flex: ruleset and realized that this was the item being styled. There you can also see the selector that using the asterisk because it skips a level.

Basically what that selector is doing is: find a div with id=root, of its immediate children (indicated by >) take the first child and of its immediate children take all (which is just the one element). So youā€™re doing root>hui-card>mushroom-template-card, which is the element you want to style.

itā€™s really just finding the element to target and then find the path to it from the element that youā€™ve started from (which is the hui-horizontal-stack-card).

Usually you wouldnā€™t need every single step, this is just because of your setup wanting to target very specific elements. If you wanted to target all four elements at once you could have done #root mushroom-template-card for example - this would have targeted all 4 of your items and you could e.g. change all of their font sizes.

regarding your mushroom-card element
You had <mushroom-card> selected, which is also a flex-box. There is certainly a way how you could have used that to change the size, but it would have been much more complicated to get the other element to react accordingly. Whenever you have a flexbox or a grid itā€™s easiest to work from the top down.

They are made to be responsive and change sizes dynamically, so itā€™s best not to target element too deep down in the hierarchy. As mentioned: even better than your fixed percentages would be to work with flex-grow and flex-shrink for example (not tested, but should be possible), as this helps the content to react to larger and smaller screen sizes. On a ultrawide monitor a width of 43% is way too much - scaling all elements accordingly would probably be the way to go. Flexbox and Grid take a lot of the manual rules out of the game and let the browser decide whatā€™s best - you just have to define some guidelines (such as min-width/max-width, flex-grow, flex-shrink, etc.)

1 Like

I have a similar problem to:

I have type: entities, type: conditional, type: 'custom:template-entity-row'

I want to recolor the State (ā€œOpenā€) to purple.

type: entities
entities:
  - type: conditional
    conditions:
      - entity: input_boolean.confirm_door_open
        state: 'off'
    row:
      type: 'custom:template-entity-row'
      entity: lock.wohnungstur
      name: 'WohnungstĆ¼r'
      icon: mdi:door-closed-lock
      state: 'Open'
      tap_action:
        action: call-service
        service: script.temporarily_confirm_door_opening_request
<ha-card class="type-entities">
  <div id="states" class="card-content">
    <div>
      <hui-conditional-row class="type-conditional">
        <template-entity-row>
		  <!--#shadow-root (Open)-->
            <div id="wrapper" class="">
              <state-badge class=" pointer " statecolor="" icon=""></state-badge>
              <div class=" info pointer ">
                WohnungstĆ¼r
                <div class="secondary">
                </div>
              </div>
              <div class="state">
                Open
              </div>
            </div>
            <div id="staging">
              <hui-generic-entity-row no-secondary="">
              </hui-generic-entity-row>
            </div>
        </template-entity-row>
      </hui-conditional-row>
    </div>
  </div>
</ha-card>

(hope I copied it correctly with the shadow root)

For the life of me I canā€™t figure out the corect combination between the css levels, shadow roots etc.

This is one of the endless combinations I tried:

card_mod:
  style:
    hui-conditional-row:
      template-entity-row$: |
        .state {
          color: purple;
          font-weight: bold;
        }

Iā€™d appreciate some help.

as a starting point:

type: entities
entities:
  - input_boolean.test_boolean
  - type: conditional
    conditions:
      - entity: input_boolean.test_boolean
        state: 'off'
    row:
      type: custom:template-entity-row
      entity: sun.sun
    card_mod:
      style:
        template-entity-row $: |
          div#wrapper {color: red}

image

1 Like

Hello everybody,

Iā€™m not sure, if the following is an issue with card-mod or with bubble-card.

Generally bubble-card works as described, just one thing ā€¦

Iā€™d like to replace the lower section part (Klima and the 2 following card) with a popup-card.
I added my standard style settings to the card

The card with the rounded border is the popup-button-card, with the following code:

type: custom:bubble-card
card_type: button
button_type: name
name: Klima
icon: mdi:temperature-celsius
button_action:
  tap_action:
    action: navigate
    navigation_path: '#showtime'
card_layout: large
card_mod:
  style: |
    ha-tile-icon {
      --mdc-icon-size: 30px;
    }
    ha-icon-button {
      width: 90px !important;
      height: 90px !important;
    }
    ha-card {
      --ha-card-background: #;
      font-size: 40px;
      border-radius: 15px;
      box-shadow: rgba(195, 255, 104, 1) 0px 1px 50px;
    }
    ha-card {
    --primary-text-color: grey;
    --text-align: center !important;
    }

As you can see it accepts some card-mod styles (border-radius & primary-text-color), and ignores others (box-shadow, text-align and font-size)

I moved the card to the end, to show the box-shadow in the 1st picture is actually those of the adjactend cards.

For the sake of completeness, the actual popup with the working styles

I traced the element with inspector, but couldnt make it work with:

.bubble-name {ā€¦

Thanks a lot for the detailed explanation, appreciate it!

Anyone got a clue about this?

Iā€™m not working with section, but I wouldnā€™t know how youā€™d target this, as usually you go after the ha-card element.

If I wanted to achieve this Iā€™d look into the card_mod themes, which probably allow you to do this if you find the right dom navigation + selectors.

someone knows why this code does not set my background?

card_mod:
  style: |
    ha-card {
      background: url('{{ state_attr("media_player.firetv_stick_4k_max", "entity_picture") }}') no-repeat center;
      background-size: cover;
    }

Works fine:

Check what is a value of that attribute.
And check with this simplest just in case:

card_mod:
  style: |
    ha-card {
      background: red no-repeat center;
      background-size: cover;
    }

thanks for reply, this is the value

entity_picture: >-
  /api/media_player_proxy/media_player.firetv_stick_4k_max?token=6b6af85b5118fb9a51fcdf870101fbe7b34f7172045da9cc16ecddce0e54be35&cache=8673bbbc1758d827

this works for example, in the browser but not via card_mod

https://homeassistant.local:8123/api/media_player_proxy/media_player.firetv_stick_4k_max?token=6b6af85b5118fb9a51fcdf870101fbe7b34f7172045da9cc16ecddce0e54be35&cache=314130e6a060465f

Could be an issue with your networking.
And check with ā€œredā€ as suggested.

can it be that the integration card does nto allow this?
im using ā€œcustom:firemote-cardā€

The 1st rule of testing: check with the simplest setup. Means - with a stock card.

i have testing your ā€œredā€ background idea, this does not work, so it looks like the problem is there the Card

try adding ā€œ!importantā€

1 Like

ah yeah thats it, thank you

1 Like

Ok the issue with text alignment was solved, but I cannot get my regular
box-shadow working. Would be really grateful for help.

type: custom:bubble-card
card_type: button
button_type: name
name: Wetter
icon: mdi:weather-partly-cloudy
button_action:
  tap_action:
    action: navigate
    navigation_path: '#wetter'
card_layout: large
layout-options:
  grid_rows: 1
  grid_columns: 2
layout_options:
  grid_columns: full
styles: |2-
      ha-card {
          margin-top: 0;
          background: none;
          opacity: 1;
      }
      .is-unavailable {
          opacity: 0.5;
      }

      .bubble-button-card-container {
          position: relative;
          width: 100%;
          height: 50px;
          background-color: var(--background-color-2,var(--secondary-background-color));
          border-radius: 32px;
          overflow: scroll;
          touch-action: pan-y;
      }

      .bubble-button-card,
      .bubble-range-slider,
      .bubble-button-background {
          display: flex;
          position: absolute;
          justify-content: space-between;
          align-items: center;
          height: 100%;
          width: 100%;
          transition: background-color 1.5s;
      }
      .bubble-button-background {
          background-color: var(--bubble-button-background-color);
          opacity: .5;
          border-radius: 32px;
      }
      .bubble-range-fill {
          position: absolute;
          top: 0;
          bottom: 0;
          left: 0;
          width: 100%;
          left: -100%;
          transition: all .3s;
      }
      .is-dragging .bubble-range-fill {
          transition: none;
      }
      .is-light .bubble-range-fill {
          opacity: 0.5;
      }
      .is-unavailable .bubble-button-card,
      .is-unavailable .bubble-range-slider {
          cursor: not-allowed;
      }
      .bubble-range-slider {
          cursor: ew-resize;
          border-radius: 25px;
          overflow: hidden;
      }
      .bubble-icon-container {
          display: flex;
          flex-wrap: wrap;
          align-content: center;
          justify-content: center;
          min-width: 38px;
          min-height: 38px;
          margin: 150px;
          border-radius: 50%;
          background-color: var(--card-background-color, var(--ha-card-background));
          overflow: hidden;
          position: relative;
          cursor: pointer;
      }
      .bubble-icon-container::after {
          content: '';
          background-color: currentColor;
          position: absolute;
          display: block;
          width: 100%;
          height: 100%;
          transition: all 1s;
          left: 0;
          right: 0;
          opacity: 0;
      }
      .is-light.is-on .bubble-icon-container::after {
          opacity: 0.2;
      }
      .is-unavailable.is-light .bubble-icon-container::after {
          opacity: 0;
      }

      .bubble-icon {
          display: flex;
          opacity: 0.6;
      }

      .is-on .bubble-icon {
        filter: brightness(1.1);
        opacity: 1;
      }

      .bubble-entity-picture {
          background-size: cover;
          background-position: center;
          height: 100%;
          width: 100%;
          position: absolute;
      }

      .bubble-name,
      .bubble-state {
          display: flex;
          position: relative;
          white-space: nowrap;
      }

      .bubble-name-container {
          display: flex;
          line-height: 18px;
          flex-direction: column;
          justify-content: center;
          flex-grow: 1;
          margin: 0 160px 0 4px;
          pointer-events: none;
          position: relative;
          overflow: hidden;
      }

      .bubble-name {
          font-size: 25px;
          font-weight: 600;
      }

      .bubble-state {
          font-size: 12px;
          font-weight: normal;
          opacity: 0.7;
      }

      .bubble-feedback-element {
          position: absolute;
          top: 0;
          left: 0;
          opacity: 0;
          width: 100%;
          height: 100%;
          background-color: rgb(0,0,0);
          pointer-events: none;
      }

      @keyframes tap-feedback {
          0% {transform: translateX(-100%); opacity: 0;}
          64% {transform: translateX(0); opacity: 0.1;}
          100% {transform: translateX(100%); opacity: 0;}
      }

      .large .bubble-button-card-container {
        height: 56px;
        border-radius: 32px;
      }

      .large .bubble-icon-container {
        --mdc-icon-size: 40px;
        min-width: 42px !important;
        min-height: 42px !important;
        margin-left: 8px;
      }

      .rows-2 .bubble-sub-button-container {
        flex-direction: column;
        gap: 4px !important;
        display: grid !important;
        grid-template-columns: repeat(2, min-content);
        grid-template-rows: repeat(2, 1fr);
        grid-auto-flow: column;
        width: auto;
        padding-right: 14px;
      }

      .rows-2 .bubble-sub-button {
        height: 20px !important;
      }
  .bubble-button-card-container {
    background: rgba(12,120,50,0.5) !important;
  }     
card_mod:
  style: |
    ha-card {
     --primary-text-color: grey;
     --ha-card-background: #;
     font-size: 100px;
     border-radius: 15px;
     box-shadow: rgba(195, 255, 104, 1) 0px 1px 50px;
     }

Hello masters!
There is any way to style the header text of a grid? I know about card mod, but i donā€™t know how to use it in a grid card.
Thanks!