jss how to change opacity for a color
Asked Answered
N

11

99

Currently I am using the following code to add a color to an element using jss.

const styleSheet = theme => ({
  root: {
     backgroundColor: theme.colors.red,
  },
})

I would like to know if exist a function to add opacity based on colortheme.colors.red.

example smt like: backgroundColor: color(theme.colors.red, .05),

Nonlinearity answered 13/11, 2017 at 16:8 Comment(3)
from what I can tell from the documentation can you not just set the opacity property? github.com/cssinjs/cssinjs/search?utf8=&q=opacity&type=Sweater
opacity is for all the element I need instead just change the value inside the background colorNonlinearity
Assuming I understand the question correctly, you should remove the jss tagConvection
K
201

Material UI has a colorManipulator utility file, which includes an alpha function:

import { alpha } from '@material-ui/core/styles/colorManipulator';

/**
 * Sets the absolute transparency of a color.
 * Any existing alpha values are overwritten.
 * @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla(), color()
 * @param {number} value - value to set the alpha channel to in the range 0 - 1
 * @returns {string} A CSS color string. Hex input values are returned as rgb
 */

{
    backgroundColor: alpha(theme.colors.red, 0.5)
}

For Mui v5:

import { alpha } from "@mui/material";

Alternatively, you can add the color library from npm for color manipulation:

import Color from 'color';

{
    backgroundColor: Color(theme.colors.red).alpha(0.5).string()
}
Kalagher answered 23/8, 2018 at 5:57 Comment(4)
The solution in this answer is now deprecated in the latest Material-UI. Use alpha instead of fadePectase
For Mui v5, you need to add this code: import { alpha } from "@mui/material";Cathead
for Mui v4: import { alpha } from '@material-ui/core'Metamathematics
for Mui v5: import { alpha } from '@mui/material/styles'. See: mui.com/guides/migration-v4/#stylesAfb
C
17

Alternatively, you can use the fade function provided in Material UI Next.

import {fade} from 'material-ui/styles/colorManipulator';

const theme = createMuiTheme({
  overrides: {
    MuiButton: {
      root: {
        boxShadow: `0 4px 8px 0 ${fade(defaultTheme.palette.primary[500], 0.18)}`,
      }
    },
  }
});

export default theme;

Here's how it's working : https://github.com/mui-org/material-ui/blob/v1-beta/src/styles/colorManipulator.js#L157-L164

Another solution could be to use similar color functions from https://github.com/styled-components/polished

Crawler answered 2/1, 2018 at 15:51 Comment(1)
in version 4 of material-ui that is now: import {fade} from '@material-ui/core/styles/colorManipulator';Unduly
A
15

Assuming you don't already have the alpha channel defined in the color, you can also do:

backgroundColor: theme.colors.red + '00' 

This will set alpha channel to 0, thus transparent. You can append any value between '00' to 'ff'

Angelaangele answered 17/4, 2019 at 20:2 Comment(1)
This is the fastest and most straight-forward solution. Can't believe this was so hard to find. Thanks.Metamorphism
E
9

for MUI v5 this seems to work:

import { alpha } from '@mui/material';

...

MuiContainer: {
      styleOverrides: {
        root: {
          '&.MuiContainer-asideWithImage': {
            backgroundColor: alpha(MY_COLOR, 0.78),
          },
        },
      },
    },

...

Elmore answered 23/6, 2022 at 21:3 Comment(0)
N
6

I found a solution using

 backgroundColor: theme.utils.rgba(theme.axColor.black, 0.7),
Nonlinearity answered 13/11, 2017 at 16:38 Comment(2)
This doesn't work for me with the latest version of Material-UI. I can't find it in the documentation either. Do you have a link?Kalagher
This doesn't work anymore in the current version. @Crawler solution works.Ananthous
P
4

Some of these answers are referencing deprecated Material-UI functions. The current preferred approach is to use alpha:

import { alpha } from "@material-ui/core";

...
                 // yields rgba(255,255,255,0.85)
backgroundColor: alpha(theme.palette.background.paper, 0.85) 
Pectase answered 22/7, 2021 at 15:35 Comment(1)
can't find any reference in the official documentation, and is not available in the current v4 versionMetamathematics
S
2

You can use RGBA values

const styleSheet = theme => ({
  root: {
     backgroundColor: 'rgba(255, 255, 255, 0.5)',
  },
})

https://facebook.github.io/react-native/docs/colors.html

Stanleystanly answered 13/11, 2017 at 16:18 Comment(2)
sorry I wanted to re-use value for my variableNonlinearity
Make theme.colors.red an rgba valueStanleystanly
C
1

Another possibility is:

import color from "color"

const themeColorsRed = color
  .rgb(theme.colors.red)
  .array()

Then you can do:

{
  backgroundColor: `rgba(${themeColorsRed}, 0.05)`,
}
Cattalo answered 7/1, 2021 at 0:6 Comment(0)
P
0

For what it's worth, an 8 digit hex code works too

const styleSheet = theme => ({
  root: {
     backgroundColor: '#ffffff80',
  },
})
Plasticizer answered 23/11, 2018 at 18:45 Comment(0)
U
0

For the current v5 of mui use this from the documentation:

import { useTheme,alpha } from '@mui/material/styles';

...

const theme = useTheme();

...

backgroundColor: alpha(theme.palette.background.default,0.9)
Undermost answered 21/1 at 9:21 Comment(0)
C
0

In the MUI5 you can use theme locally:

import {alpha} from '@mui/material';

const styles = {
   background: (theme) => alpha(theme.palette.colors.yousColorName, 0.1),
}
Cecilacecile answered 23/1 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.