0

I'm looking to darken an element on hover without also darkening the text color. I would like the text to remain pure white on hover.

Example:

<a href="#" class="badge" style="background-color: #0072CE;"><span>Some Text</span></a>

CSS:

.badge {
  display: inline-block;
  padding: 0.35em 0.65em;
  font-size: 0.75em;
  font-weight: 700;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25rem;
  text-decoration: none;
}
.badge:hover {filter: brightness(0.8);}
.badge > span {color: white;}

I tried applying positioning and z-index but that didn't work either. Also, I need the style tag because the background colors are generated dynamically.

Any help on the above or a different solution altogether would be appreciated!

EDIT: My appologies, I should have been clear that I also don't have access to set the hover color in the styles.

Dario Zadro
  • 854
  • 1
  • 11
  • 18

3 Answers3

1

Instead of using filter: Brightness(), I prefer to use filter: Saturate(). It gives object darken background but doesn't affect your text.

.badge {
  display: inline-block;
  padding: 0.35em 0.65em;
  font-size: 0.75em;
  font-weight: 700;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25rem;
  text-decoration: none;
}
/* .badge:hover {filter: brightness(0.8);} */
.badge:hover {filter: saturate(0.5);}
.badge > span {color: white;}
<a href="#" class="badge" style="background-color: #0072CE;"><span>Some Text</span></a>
Binh Vo
  • 74
  • 4
0

This is the best solution I can think of right now when you're unable to change the inline styling.

Essentially styling the span instead, changing it's background opacity based on hover.

.badge {
  display: inline-block;
  font-size: 0.75em;
  font-weight: 700;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25rem;
  text-decoration: none;
}

.badge>span {
  display: block;
  padding: .35em .65em;
  border-radius: inherit;
}

.badge>span:hover {
  background-color: #0004;
}
<a href="#" class="badge" style="background-color: #0072CE;"><span>Some Text</span></a>
Zach Jensz
  • 2,608
  • 3
  • 8
  • 23
  • I don't have access to set the hover color in the stylesheet. Otherwise, I wouldn't be using inline styles at all. – Dario Zadro Apr 01 '22 at 03:31
0

you can use background-color attribute to change the background color

.badge {
  display: inline-block;
  padding: 0.35em 0.65em;
  font-size: 0.75em;
  font-weight: 700;
  line-height: 1;
  color: white;
  text-align: center;
  white-space: nowrap;
  vertical-align: baseline;
  border-radius: 0.25rem;
  text-decoration: none;
  background-color: rgba(111,111,111,1);
}
.badge:hover {background-color: rgba(111,111,111,.5);}

you can make background color transparent with value of alpha in rgba(r, g, b, alpha)

or

you can just change background color

  • This won't work because the inline style has higher specificity and will always override the style tag styling. – Zach Jensz Apr 01 '22 at 03:43