Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sass parent selector and hover?

Tags:

css

sass

Is it possible to trigger a parent's class when on hover? I know it can be done with jquery but I want to do a pure css solution.

My code:

.navbar-form{
    padding-left: 55px;
    .input-group{
        width: 200px;
        .form-control{
            border-right: none;
            width: 350px;
             &:hover & {
                border-color: blue;
            }
        }
        .input-group-addon{
            background-color: white;
            border-left: none;
        }
    }
}

FIDDLE: http://jsfiddle.net/fcKyc/

Looking to have that when I focus on the input, .input-group-addon would do something. input and the icon are children of the input-group.

like image 320
Mohamed El Mahallawy Avatar asked Mar 03 '14 05:03

Mohamed El Mahallawy


2 Answers

If I understand it right, here is my interpretation of the DOM interaction you're describing.

.parent

  • .child1

  • .child2

A hover on .child1 affects .child2

If yeah, then here:

.form-control {
    ... //your CSS for this
    input {
        //CSS for input
        &:hover ~ .input-group-addon {
            //CSS for .input-group-addon when input is hovered
        }
    }
}

OR, if .input-group-addon is right after input (adjacent sibling), then you could use the following:

.form-control {
    ... //your CSS for this
    input {
        //CSS for input
        &:hover + .input-group-addon {
            //CSS for .input-group-addon when input is hovered
        }
    }
}

As @Martin suggested.

like image 61
Richard Shi Avatar answered Sep 18 '22 17:09

Richard Shi


I think you are looking for something like this:

<style type="text/css">
.navbar {
  background: #000;
  &:hover .change{
    background: #ccc;
  }
}
</style>

<div class="navbar">
  <div class="change">
  </div>
</div>

If you hover above the navbar then the .change div will change colors. This is what I usually use instead of using jQuery for certain effect triggers.

like image 44
RaySinlao Avatar answered Sep 21 '22 17:09

RaySinlao