Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set primary and secondary colors in Material Design Components for Web?

Tags:

Details

I went looking into Material Components (MDC) for Web and landed to using CDN (hosted CSS and JavaScript libraries):

  • https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css
  • https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js

This is by virtue from their getting-started-docs-page in [1]. Similarly, MDC has this predecessor-slash-lighter-library Material Design Lite (MDL) which you could easily customize the theme colors. It can be done through its custom CSS theme builder. [2]

However, according to MDC Web's Theming Guide: [3]

... At the moment, MDC Web supports theming with Sass and with CSS Custom Property, with plans for CDN support as well, once that service is available.

It turns out, modifying the theme colors through the MDC's CDN URL is currently not an option.

So again back to my question, how could one set the primary and secondary colors in the new MDC for Web using the CDN?

References

  1. Getting started, Material Components for the Web
  2. Custom CSS theme builder, Material Design Lite
  3. Theming guide, Material Components for the Web
like image 714
Abel Callejo Avatar asked Sep 18 '18 06:09

Abel Callejo


People also ask

How do we use primary and secondary Colours in UI Design?

For a UI designer, the basic tool for the selection of appropriate colors is a good color wheel. The primary colors of the color wheel comprise of Red, Blue, Green and the secondary colors are Green, Orange, Purple.

What is primary color in Material Design?

In Material Design, a primary color refers to a color that appears most frequently in your app. A secondary color refers to a color used to accent key parts of your UI. Using colors from the Material Design palette is optional.


1 Answers

If you're using MDC Web's CSS from CDN, you can modify a theme using CSS custom properties (variables) like this:

/* my-style.css */

:root {
  --mdc-theme-primary: #fcb8ab;
  --mdc-theme-secondary: #feeae6;
}

The full list of CSS custom properties for MDC Theme is here. For instance, here how you can modify text color on top of a primary/secondary backgrounds:

/* my-style.css */

:root {
  --mdc-theme-primary: #fcb8ab;
  --mdc-theme-secondary: #feeae6;
  --mdc-theme-on-primary: #fff;
  --mdc-theme-on-secondary: #fff;
}

Note that this CSS should come after importing the MDC Web's CSS file, e.g.:

<link rel="stylesheet" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">
<link rel="stylesheet" href="my-style.css">

MDL theme customizer you've mentioned has nothing to do with MDC Web. MDL is an MDC Web's predecessor which is deprecated in favor of MDC Web.

like image 190
Rustem Gareev Avatar answered Sep 28 '22 02:09

Rustem Gareev