0

I have the following button:

<button class="btn btn-outline-secondary btn-lg">
  TEXT<i class="fa fa-plus"></i>
</button>

I would like to style the buttons text and the fa-icon with a different color. Both should be able to change the color when i hover over the button. Im able to do either of them but cant get both working at the same time:

I can style them differently:

.fa.fa-plus {
  color: green;
}

.btn {
  color: WHITE
}

This makes the text white and the icon green(Thats what i want). When i hover over the button, im now only able to change the texts color:

.btn:hover {
  color: BLACK;
}

How can i change the icons color too if the button is hovered? I would like the text for example to be black and the icon to be white when the button is hovered. But im not able to achieve the latter.

M.Dietz
  • 900
  • 10
  • 29
  • .btn:hover .fa.fa-plus {color:#fff;} ? – Sfili_81 Dec 27 '19 at 15:42
  • Does this answer your question? [CSS :: child set to change color on parent hover, but changes also when hovered itself](https://stackoverflow.com/questions/14792574/css-child-set-to-change-color-on-parent-hover-but-changes-also-when-hovered) – AGE Dec 27 '19 at 15:48

6 Answers6

2

You can use a rule to both the button inner text as well as the icon as following:

.fa.fa-plus {
  color: green;
}

.btn {
  color: WHITE
}

.btn:hover .fa,
.btn:hover{
    color: red;
}
<button class="btn btn-outline-secondary btn-lg">
  TEXT<i class="fa fa-plus">+</i>
</button>
Pedro Figueiredo
  • 2,304
  • 1
  • 11
  • 18
2

Your code means that you want to change button text on hover.

To do what you want, you should apply styles to your icon on button hover:

.btn:hover .fa.fa-plus {
  color: RED;
}

So, in this case, you will apply styles to .fa.fa-plus, but on button hover. Hope, it makes sense to you :)

1

Give a parent div for that button and add hover effect using that parent class

<style>
.fa.fa-plus {
  color: green;
}

.btn {
  color: WHITE
}
.hover:hover .btn, .hover:hover .fa.fa-plus{
color: BLACK;
}
</style>
<div class="hover">
<button class="btn btn-outline-secondary btn-lg">
  TEXT<i class="fa fa-plus"></i>
</button>
</div>
Bhavana
  • 101
  • 4
0

The below is an example of how to change the child via the parent :hover attribute, implement it according to how you wish to color the plus icon.

.btn:hover .fa.fa-plus {
    color: red;
}
AGE
  • 3,752
  • 3
  • 38
  • 60
0

You can use this code to access the child element of hover:

.btn:hover . { color: BLACK; }

M074554N
  • 61
  • 1
  • 6
0

use this in each element one-by-one on your main page,

For an example:

div.e:hover {
    background-color:red;
}