Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color opacity on mouseover

I have a DIV that I'd like to change the background color opacity on, depending on if the mouse is over it or not.

I know you can use background: rgba(54, 25, 25, .5) etc, but I want to set the colour separately. Is there any way I can JUST modify the OPACITY, and not the colour.

I could opacity: 0.3, etc, but that effects the whole DIV, and I just want to affect the background colour.

like image 772
Chuck Le Butt Avatar asked Apr 30 '12 18:04

Chuck Le Butt


People also ask

How do you change the background color of opacity?

Changing the opacity of the background color only To achieve this, use a color value which has an alpha channel—such as rgba. As with opacity , a value of 1 for the alpha channel value makes the color fully opaque. Therefore background-color: rgba(0,0,0,. 5); will set the background color to 50% opacity.

How do I reduce background opacity?

Approach: We can use the opacity property in CSS which is used to change the opacity of an element. The value of the property can be set in a range of 0 to 1, where “0” is fully transparent and “1” is opaque. Any decimal value can be used in between to set the opacity accordingly.

How do I change the color of opacity in CSS?

To set the opacity of color we mainly change the value of alpha. The value of alpha varies from 0.0 (fully transparent) to 1.0 (fully opaque). Example: In the following example, we use the CSS background-color property with alpha value (opacity).

How do I change the background opacity without affecting text?

The percentage of opacity is calculated as Opacity% = Opacity * 100 To set the opacity only to the background and not the text inside it. It can be set by using the RGBA color values instead of the opacity property because using the opacity property can make the text inside it fully transparent element.


6 Answers

No html/css doesn't have that option built in, but since you're accessing/setting the colour in javascript you might as well add in your own function which can handle that for you.

Here's an example for you:

<script type="text/javascript">
function RGBA(red,green,blue,alpha) {
    this.red = red;
    this.green = green;
    this.blue = blue;
    this.alpha = alpha;
    this.getCSS = function() {
        return "rgba("+this.red+","+this.green+","+this.blue+","+this.alpha+")";
    }
}

// store a copy of the color
var bgColor = new RGBA(255,0,0,0.5);

function setBgOpacity(elem, opac) {
    bgColor.alpha = opac;
    elem.style.backgroundColor = bgColor.getCSS();
}
</script>

Then in the HTML use the onmouseover event to change the opacity of the bgColor:

<div onmouseover="setBgOpacity(this, '0.3');"
     onmousout="setBgOpacity(this, '0.5');">Put your mouse over me</div>
like image 149
Ozzy Avatar answered Oct 30 '22 04:10

Ozzy


You can make the background part of a different div and set the opacity of THAT div, i.e.

<div id="container">
    <div id="background"></div>
    <div id="content">
        <p>Lorum ipsum...</p>
    </div>
</div>

And

#container { overflow:hidden; position:relative; }
#background { 
    position:absolute; 
    top:0; 
    left:0; 
    width:100%; 
    height:100%; 
    background:#FF0000;
}
#background:hover { opacity:0.3; }
#content { position:relative; z-index:10; }
like image 28
Jeff Avatar answered Oct 30 '22 02:10

Jeff


There a difference between the Alpha value in RGBa and Opacity. Opacity affects all child elements, Alpha does not.

You'll have to read the current colour value, then restate it as RGBa with the new Alpha value. You may need to convert the current hex colour value to a decimal triplet to do this.

like image 26
Diodeus - James MacFarlane Avatar answered Oct 30 '22 04:10

Diodeus - James MacFarlane


If you are relying on RGBA to modify the opacity of the background color, no, there is no way to set that separately from the color itself. You will have to declare explicit RGBA values for both your normal and hover states.

like image 44
chipcullen Avatar answered Oct 30 '22 03:10

chipcullen


No, you can't edit only the alpha of rgba. So you should use the RGB part of the RGBa.

like image 28
Wouter J Avatar answered Oct 30 '22 02:10

Wouter J


If you want a separate background colour from the container you may want to use :before or :after.

.container { 
    position: relative;

    &:before {
        content: '';
        position: absolute;
        width: 100%;
        height: 100%;
        background-color: #000;
        opacity: 1;
        z-index: -1;
    }

    &:hover {
        &:before {
            opacity: 0.5;
        }
    }

    .content {
        z-index: 1;
    }
}

When you hover on the .container, only the opacity of the :before is effected and not the contents.

like image 34
Matthew Skinner Avatar answered Oct 30 '22 02:10

Matthew Skinner