Toño Bobadilla
Toño Bobadilla

Reputation: 33

Add background image when element is hovered

So what I want to do is the following:

  1. When you hover over the li, the a becomes transparent too (so it's also affected)
  2. When you click on the li, it takes to to a different page (basically, it's doing the thing the a is supposed to do).
  3. When you hover over the li, a background image appears, replacing the text that was inside the a.

This is what I have in html:

nav ul {
  list-style: none;
  padding: 0;
  display: flex;
  justify-content: space-between;
}
nav ul li:hover {
  background-color: #FFB53A;
}
nav ul li a {
  text-decoration: none;
  color: black;
  font-family: Gotham book;
}
nav ul li {
  padding-top: 15px;
  padding-bottom: 15px;
  text-align: center;
  width: 20%;
  border-right: 1px solid #b3b3b3;
}
nav {
  background-color: #ccc;
  width: 100%;
}
nav ul .lupa {
  background-image: url("http://i.imgur.com/kXm05cw.png");
  background-repeat: no-repeat;
  background-position: center;
}
<nav>
  <ul>
    <li class="opcion-1"><a title="Opcion 1" href="#">Quiénes somos</a></li>
    <li class="opcion-2"><a title="Opcion 2" href="#">Nuestro trabajo</a></li>
    <li class="opcion-3"><a title="Opcion 3" href="#">UNAC news</a></li>
    <li class="opcion-4"><a title="Opcion 4" href="#">Cómo donar</a></li>
    <li class="lupa"><a title="Opcion 5" href="#"></a></li>
  </ul>
</nav>

Upvotes: 2

Views: 37

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371749

nav {
  display: flex;
  justify-content: space-between;
  background-color: #ccc;
}

nav > a {
  text-decoration: none;
  color: black;
  font-family: Gotham book;
  text-align: center;
  width: 20%;
  border-right: 1px solid #b3b3b3;
  padding-top: 15px;
  padding-bottom: 15px;
}

nav > .lupa {
  background-image: url("http://i.imgur.com/kXm05cw.png");
  background-repeat: no-repeat;
  background-position: center;
}

nav > a:hover {
  background-color: #FFB53A;
  color: transparent;
  background-image: url(http://i.imgur.com/60PVLis.png);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}
<nav>
  <a class="opcion-1" title="Opcion 1" href="#">Quiénes somos</a>
  <a class="opcion-2" title="Opcion 2" href="#">Nuestro trabajo</a>
  <a class="opcion-3" title="Opcion 3" href="#">UNAC news</a>
  <a class="opcion-4" title="Opcion 4" href="#">Cómo donar</a>
  <a class="lupa" title="Opcion 5" href="#"></a>
</nav>

jsFiddle

Upvotes: 1

Related Questions