0

Trying to hover over an icon that triggers the h4 to transition margin, display and background.
Is this to correct way to select one element:hover{} but affect another element?

.nav_item i:hover h4

Please see the code pen link below:

CodePen

ps I put x and o in place of the icon's, font-awesome wouldn't load.

css

.nav_item h4 {
      -webkit-transition: all 1s ease;
       transition: all 1s ease;
       width: 40rem;
       display: none;
    }

.nav_item i {
  width: 5rem;
}

.nav_item i:hover h4 {
  background-color: #b4fdd2;
  width: 20rem;
  display: block;
}

HTML

    <div class="nav_box">
        <div class="nav_ul">
            <div class="nav_item">
                <span class="d"><i class="fas fa-home">O</i></span>                    
                <h4 class="h4">Home</h4>
            </div>
            <div class="nav_item">
                <span class="d"><i class="fas fa-barcode">X</i></span>                    
                <h4 class="h4">Shop</h4>
            </div>
            <div class="nav_item">
                <span class="d"><i class="fas fa-book">O</i></span>                    
                <h4 class="h4">Learn</h4>
            </div>                                                             
            <div class="nav_item nav_down">
                <span class="d"><i class="fas fa-key">X</i></span>
                <h4 class="h4">Sign in</h4>
            </div>
        </div>
    </div>
</nav>
sltdev
  • 429
  • 3
  • 6
  • 13

2 Answers2

0

You can't access ancestors in CSS, but you can select siblings with the + (next sibling) selector. The h4 is the next sibling of span you can do:

.nav_item span:hover + h4 {
  background-color: #b4fdd2;
  width: 20rem;
  display: block;
}

You will need to do further adjustments to your code for it to work properly.

methodofaction
  • 70,885
  • 21
  • 151
  • 164
0

Well since you wrap your h4 text and font you can use span:hover instead of i hover.

Please note if you use "+" it will apply only to first H4 tag next to span If you use "~" it will apply to every H4 tag next to span. Check example here https://jsfiddle.net/dkn6zch1/3/

<script src="https://kit.fontawesome.com/38d66fee78.js" crossorigin="anonymous"></script>
@import url("https://fonts.googleapis.com/css?family=Spartan&display=swap");
* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
          box-sizing: border-box;
}

html {
  font-size: 62.5%;
  height: 100vh;
}

body {
  font-family: "Spartan";
}

a {
  text-decoration: none;
}

.nav {
  font-size: 4rem;
  background-color: lavender;
  height: 100vh;
  display: inline-block;
}

.nav_logo {
  width: 100%;
  height: 5rem;
}

.nav_box {
  display: inline-block;
  height: 95vh;
}

.nav_ul {
  display: inherit;
  height: auto;
}

.nav_item {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  text-align: center;
}

.nav_item:not(:last-child) {
  margin-top: 2rem;
}

.nav_item h4 {
  -webkit-transition: all 1s ease;
  transition: all 1s ease;
  width: 40rem;
  display: none;
}

.nav_item i {
  width: 5rem;
}

.nav_item span:hover ~ h4 {
  background-color: #b4fdd2;
  width: 20rem;
  display: block;
}

.nav_item i,
.nav_item h4 {
  text-align: center;
}
.nav_down {
  //margin-top: 64vh;
}
/*# sourceMappingURL=style.css.map */
<nav class="nav">
        <div class="nav_logo"></div>

        <div class="nav_box">
            <div class="nav_ul">
                <div class="nav_item">
                    <span class="d"><i class="fas fa-home">O</i></span>                    
                    <h4 class="h4">Home</h4>
                </div>
                <div class="nav_item">
                    <span class="d"><i class="fas fa-barcode">X</i></span>                    
                    <h4 class="h4">Shop</h4>
                </div>
                <div class="nav_item">
                    <span class="d"><i class="fas fa-book">O</i></span>                    
                    <h4 class="h4">Learn</h4>
                </div>                                                             
                <div class="nav_item nav_down">
                    <span class="d"><i class="fas fa-key">X</i></span>
                    <h4 class="h4">Sign in</h4>
                </div>
            </div>
        </div>
    </nav>

Please take a look at How to affect other elements when one element is hovered

Noob
  • 1
  • 3