html - How do I layer CSS opacity? -
i'm attempting create particular hover effect in html list purely css. when hovering, "background" nav
element should half opacity "foreground" li
should full opacity.
nav { font-size:2.0em; } nav:hover { background-color: #0000ff; opacity: 0.5; } nav li:hover { background-color: #ff0000; opacity: 1.0; }
<nav> <ul> <li>first</li> <li>second</li> </ul> </nav>
unfortunately li
hover appears have picked half opacity, though opacity:1.0
has been specified.
why happening , missing?
is want?
nav { font-size:2.0em; } nav:hover { background-color: rgba(0,0,255,0.5); } nav:hover li { opacity:0.5; } nav li:hover { background-color: #ff0000; opacity: 1.0; }
<nav> <ul> <li>first</li> <li>second</li> </ul> </nav>
edit
about "why" part:
according mdn:
the value applies element whole, including contents, though value not inherited child elements. thus, element , contained children have same opacity relative element's background, if element , children have different opacities relative 1 another.
and
computed value: specified value, clipped in range [0,1]
so if set opacity:0.5
<nav>
, of child share same opacity "base". , since opacity
can in range [0,1], <li>
s inside <nav>
can not have more practical opacity 0.5.
using rgba
sets background color of <nav>
, , nav:hover li{opacity:0.5}
applies transparency each <li>
itself, making possible target specific li:hover
different opacity.
Comments
Post a Comment