Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use SCSS color functions to emulate a white transparent overlay?

Tags:

css

sass

I'm trying to use SCSS to fake a white transparent color overlaying another solid color. I could easily do background-color:rgba(255,255,255,.5) on the overlay <div>, but I'd rather have a solution that doesn't require the browser to support rgba colors. Also, since I'm using SCSS variables, I won't necessarily know what the bottom color is beforehand, so I'll need to calculate the result.

It seems like one of the SCSS color functions should be able to achieve this effect, but I've tried a few things, and I can't seem to get it to work.

Does anyone know how to do this?

Here's a demo to better illustrate what I'm trying to do, or you can see the code pasted below. http://jsfiddle.net/BRKR3/

HTML

<div class="background">
    <div class="overlay rgba"></div>
</div>

<div class="background">
    <div class="overlay scss"></div>
</div>

SCSS

$background: #009966;

.background {
    background-color:$background;
    height:60px;
    margin:20px;
    padding:20px;
    width:60px;
}

.overlay {
   height:60px;
   width:60px;
}

.rgba {
    background-color:rgba(255,255,255,0.5);
}

/* works if $background is $808080, but not if it's a color */
.scss {
    background-color:scale-color($background, $lightness:150%);
}


UPDATE

Here's a working jsFiddle using Chuck's answer: http://jsfiddle.net/BRKR3/3/

like image 295
Philip Walton Avatar asked Oct 31 '11 06:10

Philip Walton


People also ask

How do I add transparency to RGB?

These colors can be useful for charts and graphics with overlapping elements. The rgb() command is the key: you define a new color using numerical values (0–255) for red, green and blue. In addition, you set an alpha value (also 0–255), which sets the transparency (0 being fully transparent and 255 being “solid”).

Can RGB values represent transparency?

RGBA is an extension of the RGB color model. The acronym stands for red green blue alpha. The alpha value represents the level of transparency/opacity of the color.


Video Answer


1 Answers

Lightness doesn't blend a color with white; it makes it a lighter color, which means it's lighter but the components are also more intense (whereas blending with white makes them more muted). You can think of lightness as multiplying the color. In order to get a screen effect when you adjust lightness, you need to decrease the saturation proportionately.

To get the effect you want, just use mix($background, white, 50%). This performs the same kind of blending that compositing colors with alpha does.

like image 177
Chuck Avatar answered Sep 30 '22 01:09

Chuck