• Stars
    star
    1,059
  • Rank 43,353 (Top 0.9 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ”น Add CSS styles to (almost) any lovelace card

card-mod 3

Allows you to apply CSS styles to various elements of the Home Assistant frontend.

Installing

hacs_badge

Install using HACS or see this guide.

Performance improvements

While card-mod can be installed as a lovelace resource, some functionality will benefit greatly from it being installed as a frontend module instead.

To do that, add the following to your configuration.yaml file and restart Home Assistant:

frontend:
  extra_module_url:
    - /local/card-mod.js

You'll need to adjust that path according to where you have installed card-mod.js. If you installed through HACS, this is probably /hacsfiles/lovelace-card-mod/card-mod.js.

Any resource definitions automatically added by HACS can be kept as is even after adding extra_module_url.

Quick start

  • Open your card in the GUI editor
  • Click the "SHOW CODE EDITOR" button at the bottom
  • Add the following to the bottom of the code:
card_mod:
  style: |
    ha-card {
      color: red;
    }

You should see the text of the card turn red as you type.
You should also see a little brush icon popping up near the "SHOW VISUAL EDITOR" button. This indicates that this card has card-mod code which will not be shown in the visual editor.

QuickStart

Usage

Styling cards

Cards are styled by adding a card_mod parameter to the card configuration.

In basic form, this parameter contains a string of CSS which will be injected into the <ha-card> element of the card.

NOTE: card-mod only works on cards that contain a ha-card element. This includes almost every card which can be seen, but not e.g. conditional, entity_filter, vertical-stack, horizontal-stack, grid.

Note, though that those cards often include other cards, which card-mod can work on.
See the manual for each card to see how to specify parameters for the included card(s).

The bottommost element that can be styled is the <ha-card>.

Screenshot of the ha-card element in the Chrome DOM inspector

ha-card

TIP: Home Assistant themes makes use of CSS variables. Those can both be set and used in card-mod - prepended by two dashes:

card_mod:
  style: |
    ha-card {
      --ha-card-background: teal;
      color: var(--primary-color);
    }

Styling entities, badges and elements

In entities and glance cards, each entity can have options. Those elements can be styled individually by adding a card_mod parameter to the entity configuration.

For those cases, the styles are injected into a shadowRoot, and the bottommost element is thus accessed through :host.

This also applies to view badges and elements in picture-elements cards.

type: entities
entities:
  - entity: light.bed_light
    card_mod:
      style: |
        :host {
          color: red;
          }
  - entity: light.ceiling_lights
    card_mod:
      style: |
        :host {
          color: green;
        }
  - entity: light.kitchen_lights
    card_mod:
      style: |
        :host {
          color: blue;
        }

Changing icons

With card-mod installed, the <ha-icon> element - used e.g. by entities, glance and many more cards - will set it's icon to the value found in the CSS variable --card-mod-icon (if present).

It will also set the icon color to the value found in the CSS variable --card-mod-icon-color if present. This ignores entity state, but will still dim unless you also set ---card-mod-icon-dim to none.

- entity: light.bed_light
  card_mod:
    style: |
      :host {
        --card-mod-icon: mdi:bed;
      }

Templates

All styles may contain jinja2 templates that will be processed by the Home Assistant backend.

card-mod also makes the following variables available for templates:

  • config - The entire configuration of the card, entity or badge - (config.entity may be of special interest)
  • user - The name of the currently logged in user
  • browser - The browser_mod deviceID of the device
  • hash - Whatever comes after # in the current URL

DOM navigation

Home Assistant makes extensive use of something called shadow DOM. This allows for easy reuse of components (such as ha-card or ha-icon) but causes some problems when trying to apply CSS styles to things.

When exploring the cards in your browsers element inspector you may have come across a line that says something like "#shadow-root (open)" (exactly what it says depends on your browser) and have noticed that elements inside that does not inherit the styles from outside.

In order to style elements inside a shadow-root, you will need to make your style: a dictionary rather than a string.

For each dictionary entry the key will be used to select one or several elements through a modified querySelector() function. The value of the entry will then be injected into those elements.

NOTE: The modified querySelector() function will replace a dollar sign ($) with a #shadow-root in the selector.

The process is recursive, so the value may also be a dictionary. A key of "." (a period) will select the current element.

Example:

Let's change the color of all third level titles (### like this) in a markdown card, and also change it's background.

If we look at the card in the element inspector of chrome, it looks like this:

markdown-card-dom

The <ha-card> element is the base, and from there we see that we need to go through one #shadow-root to reach the <h3>. That #shadow-rot is inside an <ha-markdown> element, so our selector will be:

ha-markdown$

which will find the first <ha-markdown> element and then all #shadow-roots inside that.

To add the background to the <ha-card>, we want to apply the styles to the base element directly, which has the key

.

This gives the final style:

card_mod:
  style:
    ha-markdown$: |
      h3 {
        color: purple;
      }
    .: |
      ha-card {
        background: teal;
      }

DOM-navigation

NOTE: The selector chain of the queue will look for one element at a time separated by spaces or "$".
For each step, only the first match will be selected.
But for the final selector of the chain (i.e. in a given dictionary key) all matching elements will be selected.

E.g. the following will apply styles to the #shadow-root of the first action button in an alarm-panel card:

"#armActions mwc-button$": |

But the following will apply styles to the #shadow-root of all action buttons (because we end the first key on the mvc-button selector and start a new search within those results with the subkey of "$"):

"#armActions mwc-button":
  $: |

NOTE 2: Following on the note above; due to the high level of load order optimization used in Home Assistant, it is not guaranteed that the #shadow-root or the mwc-button actually exists at the point in time when card-mod is looking for it. If you use the second method above, card-mod will be able to retry looking for #shadow-root at a later point, which may lead to more stable operation.

In short; if things seem to be working intermittently - try splitting up the chain.

Debugging tips

The DOM navigation can be tricky to get right the first few times, but you'll eventually get the hang of it.

To help you, you can use your browsers Element inspector to see which steps card-mod takes.

  • Open up the element inspector and find the base element (e.g. <ha-card>). This should contain a <card-mod> element whether you specified a style or not.
  • Make sure the <card-mod> element is selected.
  • Open up the browsers console (in chrome you can press Esc to open the console and inspector at the same time).
  • Type in $0._input_styles and press enter.
    This is the style information that step of the chain was given. If this is a string, you're at the end of the chain. If it's an object, you can move on to the next ste.
  • Type in $0._styleChildren and press enter.
    This is a set of any <card-mod> elements in the next step of any chain. Clicking "card-mod" in the value: of the set items will bring you to that <card-mod> element in the inspector, and you can keep on inspecting the rest of the chain.

Styling cards without an <ha-card> element

Cards that don't have a <ha-element> can still be styled by using the supplied custom:mod-card card.

This is only necessary in very few instances, and likely to bring more problems than it solves.

Most likely your card contains another card, in which case that is the one you should apply the styles to.

Enough warnings.

I know what I'm doing
type: custom:mod-card
card:
  type: vertical-stack # for example
  ...
card_mod:
  style: |
    ha-card {
      ...
    }

The mod-card will create a <ha-card> element - with removed background and border - and put your card inside that.

More examples

All my test cases are available in the test/views directory.

You can a demo in docker by going to the test directory and running:

docker-compose up

Then going to http://localhost:8125 and logging in with username dev and password dev.

Or you could use the vscode devcontainer and run the task "Run hass".

Themes

For instructions on how to develop a card-mod theme, see README-themes.md.

Development

For adding card-mods styling powers to your custom card, see README-developers.md.


Buy Me A Coffee

More Repositories

1

hass-browser_mod

๐Ÿ”น A Home Assistant integration to turn your browser into a controllable entity and media player
TypeScript
1,253
star
2

lovelace-auto-entities

๐Ÿ”นAutomatically populate the entities-list of lovelace cards
TypeScript
1,190
star
3

lovelace-layout-card

๐Ÿ”น Get more control over the placement of lovelace cards.
TypeScript
1,015
star
4

lovelace-slider-entity-row

๐Ÿ”น Add sliders to entity cards
TypeScript
801
star
5

lovelace-fold-entity-row

๐Ÿ”น A foldable row for entities card, containing other rows
TypeScript
563
star
6

lovelace-state-switch

๐Ÿ”นDynamically replace lovelace cards depending on occasion
TypeScript
386
star
7

hass-fontawesome

๐Ÿ”น Use icons from fontawesome in home-assistant
JavaScript
272
star
8

lovelace-card-tools

๐Ÿ”นA collection of tools for other lovelace plugins to use
JavaScript
244
star
9

lovelace-template-entity-row

๐Ÿ”น Display whatever you want in an entities card row.
TypeScript
213
star
10

hass-config

My Home Assistant configuration
Python
210
star
11

hass-lovelace_gen

๐Ÿ”น Improve the lovelace yaml parser for Home Assistant
Python
209
star
12

lovelace-more-info-card

๐Ÿ”น Display the more-info dialog of any entity as a lovelace card
TypeScript
139
star
13

lovelace-hui-element

๐Ÿ”น Use built-in elements in the wrong place
TypeScript
105
star
14

hass-favicon

๐Ÿ”น Change the favicon of your Home Assistant instance
Python
101
star
15

lovelace-card-modder

JavaScript
101
star
16

lovelace-flower-card

JavaScript
99
star
17

lovelace-popup-card

JavaScript
94
star
18

hass-plejd

๐Ÿ”น Plejd BLE integration for Home Assistant
Python
82
star
19

round-slider

JavaScript
81
star
20

lovelace-badge-card

๐Ÿ”น Place badges anywhere in the lovelace layout
JavaScript
77
star
21

lovelace-toggle-lock-entity-row

JavaScript
62
star
22

lovelace-fullykiosk

Lovelace plugin for using Fully Kiosk Browser features in home-assistant
JavaScript
61
star
23

homeassistant-lovelace-gen

A lovelace configuration file generator for home assistant
Python
57
star
24

lovelace-player

Lets any browser currently viewing your lovelace interface act as an audio receiver with a media_player interface
JavaScript
50
star
25

mittos64

C
42
star
26

lovelace-theme-maker

JavaScript
42
star
27

lovelace-browser-commander

JavaScript
33
star
28

lovelace-useful-markdown-card

JavaScript
30
star
29

lovelace-gap-card

๐Ÿ”น A lovelace card that does nothing and looks like nothing. Incredibly useful! No, really.
JavaScript
29
star
30

hass-custom-devcontainer

A devcontainer for developing and testing custom Home Assistant stuff
Shell
28
star
31

homeassistant-2020-presentation

18
star
32

lovelace-markdown-mod

JavaScript
16
star
33

lovelace-gui-sandbox

๐Ÿ”น Lets you play around with the GUI editors even if you're using YAML mode
JavaScript
16
star
34

lovelace-card-loader

JavaScript
13
star
35

lovelace-color-picker

JavaScript
13
star
36

plejd2mqtt

JavaScript
12
star
37

lovelace-column-card

A columnizing card for lovelace ui for home-assistant.
JavaScript
12
star
38

card-tools

A collection of tools to help develop lovelace cards
JavaScript
11
star
39

os5

My attempts at developing an operating system kernel.
C
11
star
40

lovelace-dummy-entity-row

๐Ÿ”น An entity row with only icon and name
JavaScript
10
star
41

lovelace-q-card

JavaScript
9
star
42

lovelace-long-press

JavaScript
9
star
43

lovelace-time-input-row

JavaScript
9
star
44

dito

A library and some tools for handling disk image files.
C
8
star
45

nextrevo-mod

How I use the E3D Revo system with Prusa Nextruder
7
star
46

mittsnap

rsnapshot-like tool that leverages btrfs snapshots
Shell
5
star
47

dotfiles

MATLAB
5
star
48

lovelace-color-glance-card

A glance card with a colorized background
JavaScript
3
star
49

lovelace-icon-headers

WIP
JavaScript
2
star
50

lovelace-wbah

Lovelace with bluejays and herons
TypeScript
2
star
51

os4

C
1
star
52

new-hass-config

Python
1
star
53

z80Monitor

Assembly
1
star
54

vim-tstatus

My status line for vim
Vim Script
1
star
55

pyplejd

Python
1
star
56

mittos64-old

C
1
star
57

hass-plant_helper

๐Ÿ”น Home Assistant Helper for monitoring plant health using MiFlora devices
Python
1
star