简体   繁体   中英

How do I use rgba in scss as a css variable?

I currently have the following using dart sass...

color: rgba(144, 202, 249, 0.5);

I would like to make it a css variable so I tried this...

--background-color: rgba(144, 202, 249, 0.5);
...
background-color: --background-color;

But I get

Error: expected "{".
  ╷
1 │ --background-color: rgba(144, 202, 249, 0.5);

I also tried...

--background-color: 144, 202, 249;
--opacity: 0.5;
...
background-color: rgba(var(--background-color), var(--opacity));

But still no dice

Error: expected "{".
  ╷
1 │ --background-color: 144, 202, 249;

Make sure you're defining your variable properly in the appropriate scope. Your initial color syntax is correct. Also be sure to reference the color with var(--name) (missing in your first code snippet).

 .root { --background-color: rgba(144, 202, 249, 0.5); }.example { height: 200px; width: 200px; background-color: var(--background-color); }
 <div class="root"><div class="example" /></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM