Issue with css variables and button background styling

I've been creating a <share-button> custom element and after I launched it, I noticed that something was funky with default button styling. They were white instead of grey.

I needed to be able to dynamically let the developer style the button that was in my Shadow DOM and to do that you have to use CSS Variables. In Chrome (at least... I need to test other browsers.) there seems to be no way to have a button with a background-color whose value is defined by a CSS Variable even if the value of the CSS variable is 'initial', 'inherit', something undefined, and have it appear as the default button.

The instant you do anything with the background color it changes the <button> to be non-natively styled.

For example:

#button2 {
  --but2: initial;
  background-color: var(--but2);
<button>Button 1</button>
<button id="button2">Button 2</button>


Here is the image of it failing for me:

There is a way around this. My colleague Surma suggested that I should try and get the default computed styles, add that to the custom CSS variable and then apply that to the background-color.

It works, but it is hacky.

  #button3 {
    --but3: initial;
<button>Button 1</button>
<button id="button3">Button 3</button>
  const defaultButtonStyle = window.getComputedStyle(button3);'--but3', defaultButtonStyle.backgroundColor); = 'var(--but3)';

Here is the image of it working for me:

I lead the Chrome Developer Relations team at Google.

We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.

Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.

I love to learn about what you are building, and how I can help with Chrome or Web development in general, so if you want to chat with me directly, please feel free to book a consultation.

I'm trialing a newsletter, you can subscribe below (thank you!)