John John
John John

Reputation: 1455

How to change Icon color while hover over an <a> tag

While I hover over an <a> tag area I want to switch the icon's color but for some reason it remains the same. I was able to get the wanted result only if I pinpointed the icon directly .sidebar .menu li a .icon:hover{background: #ecedf2;color: #3d5654;} but I have to hover exactly over the icon. What I wanted to achieve is to change the icon's color while a hover over the tag area. Let me know if I am doing something wrong.

HTML

<div class="sidebar">
  <ul class="menu">
    <li>
      <a href="#"
        ><span class="icon"><i class="fas fa-desktop"></i></span
        ><span class="text">Dashboard</span></a
      >
    </li>
  </ul>
</div>

CSS

* {
  list-style: none;
  text-decoration: none;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Jost', sans-serif;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  height: 100vh;
  width: 100wh;
  padding: 0;
  margin: 0;
  font-size: 1rem;
  font-weight: 300;
  line-height: 1.6;
  display: grid;
  grid-template-columns: auto 8fr;
  grid-template-rows: 80px auto 50px;
  grid-template-areas:
    'header header'
    'sidebar main'
    'footer footer';
}

.sidebar {
  grid-area: sidebar;
  width: 260px;
  background: #2c343b;
  height: 100%;
}

.sidebar .menu {
  padding-right: 30px;
  padding-left: 20px;
  margin: 0;
  overflow: auto;
  transition: all 0.5s;
}

.sidebar .menu li a {
  color: #ecedf2;
  display: block;
  width: 100%;
  line-height: 60px;
  text-decoration: none;
  padding: 15px;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 1px;
  transition: 0.5s;
}


.sidebar .menu li a .icon {
  color: #ecedf2;
  width: 25px;
  display: inline-block;
}

.sidebar .menu li a:hover,
.sidebar .menu li a.active {
  background: #ecedf2;
  color: #3d5654;
}

Upvotes: 0

Views: 854

Answers (5)

Alae Essaki
Alae Essaki

Reputation: 78

try this instead :

.sidebar .menu li a .icon i:hover {
     color:red;
}

Upvotes: -1

Muhammad Awais
Muhammad Awais

Reputation: 93

You can achieve using this code

.sidebar .menu a:hover i{
        background: #ecedf2;
        color: #3d5654;
}

Upvotes: 2

SaMB0
SaMB0

Reputation: 1

You can achieve this with the :hover (it's a pseudo class) syntax

a:hover { color : value }

//a is the element you want the hover to apply to// //check mdn documentation,pseudo class//

Upvotes: 0

Nilesh Chavan
Nilesh Chavan

Reputation: 381

Maybe it will help you. I just add a CSS selector for the icon.

.sidebar .menu li a:hover i{
   color:red;
}

Upvotes: 1

Ashish Sondagar
Ashish Sondagar

Reputation: 1095

  • Try this..

* {
  list-style: none;
  text-decoration: none;
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Jost', sans-serif;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}

body {
  height: 100vh;
  width: 100wh;
  padding: 0;
  margin: 0;
  font-size: 1rem;
  font-weight: 300;
  line-height: 1.6;
  display: grid;
  grid-template-columns: auto 8fr;
  grid-template-rows: 80px auto 50px;
  grid-template-areas:
    'header header'
    'sidebar main'
    'footer footer';
}

.sidebar {
  grid-area: sidebar;
  width: 260px;
  background: #2c343b;
  height: 100%;
}

.sidebar .menu {
  padding-right: 30px;
  padding-left: 20px;
  margin: 0;
  overflow: auto;
  transition: all 0.5s;
}

.sidebar .menu li a {
  color: #ecedf2;
  display: block;
  width: 100%;
  line-height: 60px;
  text-decoration: none;
  padding: 15px;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 1px;
  transition: 0.5s;
}


.sidebar .menu li a .icon {
  color: #ecedf2;
  width: 25px;
  display: inline-block;
}

.sidebar .menu li a:hover,
.sidebar .menu li a.active {
  background: #ecedf2;
  color: #3d5654;
}
<div class="sidebar">
  <ul class="menu">
    <li>
      <a href="#"
        ><span class="icon"><i class="fas fa-desktop"></i></span
        ><span class="text">Dashboard</span></a
      >
    </li>
  </ul>
</div>

Upvotes: 0

Related Questions