3 Replies - 1099 Views - Last Post: 27 October 2016 - 10:15 PM

#1 RobertSkii   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 05-September 14

jquery .css(opacity) inheritance

Posted 27 October 2016 - 11:36 AM

I am trying to make a div with .hover() that changes opacity when hover on and off. The problem is that I create a div and it inherits the opacity from the parent.

Here is the piece of jquery code
 $(".image-box").hover(
                function(){$(this).css("opacity","0.6"), $(this).append("<div>Read More +</div>")},
                function(){$(this).css("opacity","1"), $( this ).find( "div:last" ).remove();}
            );


and here is the html
<div class="image-box">
                    <img class="info-img" src="https://quest.bloomu.edu/wp-content/uploads/2012/04/summer-camp-2.jpg" alt="" />
                </div>


and the css where I tried to change the opacity but didn't work
.image-box div{
            font-size: 3em;
            color: black;
            position: relative;
            z-index:2;
            text-align: center;
            top: -60%;
            bottom: -20%;
            left: 2%;
            opacity: 1;
            border-style: solid;
            border-color: black;
            width:95%;

        }

is there a way to disable the opacity on the child? thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: jquery .css(opacity) inheritance

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: jquery .css(opacity) inheritance

Posted 27 October 2016 - 12:43 PM

As I recall the solution is to use RGBA/HSLA or a transparent PNG. (An odd solution is to rearrange the elements so that the child elements are no longer children of the troublesome parent.) More info can be found on searching "jquery opacity is inherited".

[That's not huge sogging longbottoms as someone suggested, but hue, saturation and lightness (and alpha).]
Was This Post Helpful? 0
  • +
  • -

#3 RobertSkii   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 134
  • Joined: 05-September 14

Re: jquery .css(opacity) inheritance

Posted 27 October 2016 - 03:02 PM

I tried using
.css("background-color","rgba(255,255,255,0.6")


and changing the color from the div to background-color: rgba(255,255,255,1) but didn't change at all

instead of
.css("opacity","0.6")


am I doing it wrong? I haven't used rgba that much
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: jquery .css(opacity) inheritance

Posted 27 October 2016 - 10:15 PM

There's a bracket missing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1