Caspert
Caspert

Reputation: 4363

How to apply CSS to other elements except the hovered one?

On the web you will see the last time a lot of beautiful hover effects, especially for a portfolio page, where, when you hover over an item, the other once will be get a lower opacity, so the one you hover on is highlighted.

I tried to accomplished this effect and I finished with this (also a more extensive one in CodePen):

a {
  text-decoration: none;
  transition: color 0.1s linear; 
  -moz-transition: color 0.1s linear; 
  -webkit-transition: color 0.1s linear; 
  -o-transition: color 0.1s linear;
}

a:link, a:visited { color: inherit; } /* Body color */
a:hover, a:active {	color: #d7354a; }

h1, h2, h3, h4, h5, h6 { font-weight: normal; /*margin: 16px 0;*/ } 

h1 { 
  font-family: "Adelle", Arial, sans-serif;
  font-size: 80px; 
  line-height: 90px;
}

h3, h4 { color: #000; }
h3 { 
  font-size: 28px; /* 28px / 16px = 100% */ 
  font-weight: 600;
  line-height: 38px; 
}

.txt-center { text-align: center; }

body {
  margin: auto;
  font-family: "Merriweather", Arial, sans-serif;
  font-size: 18px; /* 18px = 100% */
  line-height: 26px;
  letter-spacing: 1px;
  color: #6f6f6f;
  -webkit-font-smoothing: none; 
  -webkit-font-smoothing: subpixel-antialiased; 
  -webkit-font-smoothing: antialiased; 
}

section { 
  padding: 80px 0; 
  position: relative;
}

#register div:hover .group { opacity: .5; }
#register div .group:hover { opacity: 1; }

#register hgroup { margin-bottom: 80px; }
#register .group a {
  display: block;
  color: #333; 
}

#register .group a:hover .button { 
  background: #FFF; 
  color: #d7354a;
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2s ease;
  -ms-transition: all 0.2s ease;
  -o-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

#register .group figure { margin-bottom: 30px; }
#register .group figure img {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transition-duration: 0.3s;
  -moz-transition-duration: 0.3s;
  -o-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-property: transform;
  -moz-transition-property: transform;
  -o-transition-property: transform;
  transition-property: transform;
  -webkit-transition-timing-function: ease-out;
  -moz-transition-timing-function: ease-out;
  -o-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}

#register .group:hover figure img {
  -webkit-transform: translateY(-6px);
  transform: translateY(-6px);
}

#register .group p:first-of-type { 
  font-family: "Ubuntu", Arial, sans-serif;
  font-size: 22px;
  font-weight: 500; 
}

#register .group h3 { 
  font-size: 70px;
  line-height: 100px;
  font-weight: 400;
  color: inherit; 
  text-transform: lowercase;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>


<main class="main">
  <section class="page-header" id="register">
    <div class="container">
      <div class="row">
        <hgroup class="col-md-10 col-md-offset-1 txt-center">
          <h1>
            Register
          </h1>

        </hgroup><!-- End hgroup.col-md-10 col-md-offset-1 -->

        <div class="col-md-6 group txt-center">
          <a href="#">
            <figure>
              <img src="http://placehold.it/120x120" alt="Register as Watcher">
            </figure>
            <p>Register as</p>
            <h3>Watcher</h3>
            <p>
              Suspendisse sodales ut neque ut facilisis. <br>
              Cras lobortis vestibulum varius.
            </p>
            <div class="button secondary">I am a watcher</div>
          </a>
        </div><!-- End div.col-md-6 group txt-center -->

        <div class="col-md-6 group txt-center">
          <a href="#">
            <figure>
              <img src="http://placehold.it/120x120" alt="Register as Vlogger">
            </figure>
            <p>Register as</p>
            <h3>Vlogger</h3>
            <p>
              Suspendisse sodales ut neque ut facilisis. <br>
              Cras lobortis vestibulum varius.
            </p>
            <div class="button secondary">I am a vlogger</div>
          </a>
        </div><!-- End div.col-md-6 group txt-center -->


      </div><!-- End div.row -->
    </div><!-- End div.container -->

  </section><!-- End section#hero -->
</main>

But the problem here is, when your cursor is already in a certain area (around the "Register" text), it will give both items a lower opacity. And that's not what I want. What I want is, that when you hover over the div .group it will give the other one a lower opacity.

I know you can reach this with jQuery, but I know also this should be possible with CSS only. How can I achieve what I am looking for?

Upvotes: 4

Views: 183

Answers (2)

Harry
Harry

Reputation: 89780

The solution is actually simple considering how far you'd have actually got to in terms of achieving it.

The following are the fixes that I had done:

  • Added another selector which sets opacity of all sibling .group elements to 1 when the mouse is over the hgroup which has the "Register" text.
  • Changed the margin-bottom: 80px on the hgroup to padding-bottom: 80px because margin is not a part of the element whereas the area occupied by the padding is. Hence, the hgroup:hover selector would apply even when the mouse is over the space below the "Register" button (but not inside the .group itself).
  • Added a margin-left: 0px to the hgroup because the Bootstrap CSS was by default giving the hgroup element a 80px left margin which was causing problems to the hgroup:hover selector.

In the below snippet, I have also added a black border around the hgroup and .group elements for you to see the boundaries.

a {
  text-decoration: none;
  transition: color 0.1s linear; 
  -moz-transition: color 0.1s linear; 
  -webkit-transition: color 0.1s linear; 
  -o-transition: color 0.1s linear;
}

a:link, a:visited { color: inherit; } /* Body color */
a:hover, a:active {	color: #d7354a; }

h1, h2, h3, h4, h5, h6 { font-weight: normal; /*margin: 16px 0;*/ } 

h1 { 
  font-family: "Adelle", Arial, sans-serif;
  font-size: 80px; 
  line-height: 90px;
}

h3, h4 { color: #000; }
h3 { 
  font-size: 28px; /* 28px / 16px = 100% */ 
  font-weight: 600;
  line-height: 38px; 
}

.txt-center { text-align: center; }

body {
  margin: auto;
  font-family: "Merriweather", Arial, sans-serif;
  font-size: 18px; /* 18px = 100% */
  line-height: 26px;
  letter-spacing: 1px;
  color: #6f6f6f;
  -webkit-font-smoothing: none; 
  -webkit-font-smoothing: subpixel-antialiased; 
  -webkit-font-smoothing: antialiased; 
}

section { 
  padding: 80px 0;
  position: relative;
}

#register div:hover .group { opacity: .5; }
#register hgroup:hover ~ .group { opacity: 1; }
#register div .group:hover { opacity: 1; }

#register hgroup { 
  padding-bottom: 80px; 
  width: 100%;
  margin-left: 0;
}
#register .group a {
  display: block;
  color: #333; 
}

#register .group a:hover .button { 
  background: #FFF; 
  color: #d7354a;
  -webkit-transition: all 0.2s ease;
  -moz-transition: all 0.2s ease;
  -ms-transition: all 0.2s ease;
  -o-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

#register .group figure { margin-bottom: 30px; }
#register .group figure img {
  display: inline-block;
  vertical-align: middle;
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transition-duration: 0.3s;
  -moz-transition-duration: 0.3s;
  -o-transition-duration: 0.3s;
  transition-duration: 0.3s;
  -webkit-transition-property: transform;
  -moz-transition-property: transform;
  -o-transition-property: transform;
  transition-property: transform;
  -webkit-transition-timing-function: ease-out;
  -moz-transition-timing-function: ease-out;
  -o-transition-timing-function: ease-out;
  transition-timing-function: ease-out;
}

#register .group:hover figure img {
  -webkit-transform: translateY(-6px);
  transform: translateY(-6px);
}

#register .group p:first-of-type { 
  font-family: "Ubuntu", Arial, sans-serif;
  font-size: 22px;
  font-weight: 500; 
}

#register .group h3 { 
  font-size: 70px;
  line-height: 100px;
  font-weight: 400;
  color: inherit; 
  text-transform: lowercase;
}

hgroup, .group{
  border: 1px solid;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>


<main class="main">
  <section class="page-header" id="register">
    <div class="container">
      <div class="row">
        <hgroup class="col-md-10 col-md-offset-1 txt-center">
          <h1>
            Register
          </h1>

        </hgroup><!-- End hgroup.col-md-10 col-md-offset-1 -->

        <div class="col-md-6 group txt-center">
          <a href="#">
            <figure>
              <img src="http://placehold.it/120x120" alt="Register as Watcher">
            </figure>
            <p>Register as</p>
            <h3>Watcher</h3>
            <p>
              Suspendisse sodales ut neque ut facilisis. <br>
              Cras lobortis vestibulum varius.
            </p>
            <div class="button secondary">I am a watcher</div>
          </a>
        </div><!-- End div.col-md-6 group txt-center -->

        <div class="col-md-6 group txt-center">
          <a href="#">
            <figure>
              <img src="http://placehold.it/120x120" alt="Register as Vlogger">
            </figure>
            <p>Register as</p>
            <h3>Vlogger</h3>
            <p>
              Suspendisse sodales ut neque ut facilisis. <br>
              Cras lobortis vestibulum varius.
            </p>
            <div class="button secondary">I am a vlogger</div>
          </a>
        </div><!-- End div.col-md-6 group txt-center -->


      </div><!-- End div.row -->
    </div><!-- End div.container -->

  </section><!-- End section#hero -->
</main>

Upvotes: 2

Tarik  Khvyl
Tarik Khvyl

Reputation: 3

from your example:

 #register div:hover .group { opacity: .5; }
 #register div .group:hover { opacity: 1; }

Upvotes: -1

Related Questions