Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change opacity on all elements except hovered one

Tags:

css

hover

I wonder if there is a way to lower opacity (on hover) to all of the 'li's' except the one I'm actually hovering? Something similar to this picture: lower opacity on element except hovered one

.main-navigation { 
    margin: 0;
    padding: 20px 0px 25px;
    list-style: none;
    background-color: #ffffff;
    text-align: center;
    display:block;
    font-size:1.1em;    
}
.main-navigation li.hvr a.lvl1:link,
.main-navigation li.hvr a.lvl1:visited 
{
	display: block;
	padding: 5px 2px 5px 3px;
	color: #000;
	text-decoration: none;
    text-align:center;
}
.main-navigation li.hvr a.lvl1.active { 
    background: #eeeeee; 
    color:#000000;
}
.main-navigation li.hvr a.lvl1:hover
{
	background-color: #E6E6E6;
    color:#666666;
} 
.main-navigation li.hvr { 
	float: left; 
	position: relative;
    width:191px;
    margin:0;
    font-family: 'Open Sans', sans-serif;
}
.main-navigation li.hvr:hover { 
	background-color: #E6E6E6;
}
.main-navigation ul { 
	display: none;
	position: absolute;
	top:100%;
	left: 0;
	z-index: 9999;
	background-color: #777;
	margin: 0;
	padding: 0;
	min-width:100%;
	text-align:left;
}
.main-navigation li.hvr:hover ul { display: block; }
.main-navigation li.hvr ul li { 
	margin: 0;
	padding: 0;
	list-style: none;
}
.main-navigation li.hvr ul li a:link,
.main-navigation li.hvr ul li a:visited
{
	display: block;
	padding: 5px 20px;
	color: #fff;
    text-align: center;
}
.main-navigation li.hvr ul li a:hover,
.main-navigation li.hvr ul li a:active
{
    display: block;
    padding: 5px 20px;
    color: #fff;
    background-color:#cccccc;
}
<ul class="main-navigation clearfix">
    <li class="hvr ">
        <a class="lvl1 active" href="">Title 1</a>
        <ul>
            <li><a href="">Sub title 1</a></li>
            <li><a href="">Sub title 2</a></li>
            <li><a href="">Sub title 3</a></li>
        </ul>
    </li>
    <li class="hvr ">
        <a class="lvl1" href="">Title 2</a>
        <ul>
            <li><a href="">Sub title 1</a></li>
            <li><a href="">Sub title 2</a></li>
            <li><a href="">Sub title 3</a></li>
        </ul>
    </li>
    <li class="hvr ">
        <a class="lvl1" href="">Title 3</a>
        <ul>
            <li><a href="">Sub title 1</a></li>
            <li><a href="">Sub title 2</a></li>
            <li><a href="">Sub title 3</a></li>
        </ul>
    </li>
    <li class="hvr ">
        <a class="lvl1" href="">Title 4</a>
        <ul>
            <li><a href="">Sub title 1</a></li>
            <li><a href="">Sub title 2</a></li>
            <li><a href="">Sub title 3</a></li>
        </ul>
    </li>
</ul>
like image 671
user3507631 Avatar asked Oct 23 '14 11:10

user3507631


People also ask

How do I make an image opaque when hovering?

To make the caption opaque (and easier to read) when the mouse hovers over it, you can use the :hover pseudo-class to change opacity to 1 on hover.

Is opacity inherited CSS?

Opacity is not inherited, but because the parent has opacity that applies to everything within it. You cannot make a child element less transparent than the parent, without some trickery. Values are a number from 0 to 1 representing the opacity of the channel (the “alpha” channel).

How is opacity specified in CSS?

In addition to RGB, you can use an RGB color value with an alpha channel (RGBA) - which specifies the opacity for a color. An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

What is background opacity?

opacity is a CSS property that allows you to change the opaqueness of an element. By default, all elements have a value of 1 . By changing this value closer to 0 , the element will appear more and more transparent. A common use case is using an image as part of the background.


1 Answers

You lower the opacity of all alements except the hovered one with CSS.

The point is to lower the opacity of all <li> elements when the parent (ul) is hovered and to reset the opacity to 1 on the hovered li element like this :

ul:hover li { opacity:0.5; }
ul li:hover { opacity:1; }

Here is a simple demo :

li{
  float:left;
  width:33.33%;
  line-height:50px;
  background:grey;
  list-style-type:none;
}
ul:hover li{
  opacity:0.5;
}
ul li:hover{
  opacity:1;
}
<ul>
  <li>item</li>
  <li>item</li>
  <li>item</li>
</ul>
like image 79
web-tiki Avatar answered Oct 28 '22 14:10

web-tiki