Skip to content Skip to sidebar Skip to footer

How To Make An Element's Background-color A Little Darker Using Css

I have the following CSS for a button: .Button { background-color: #somehex; } When the user hovers over the button, I want the background-color to be a little darker. I have tr

Solution 1:

Add a dark layer on the top of it using background-image. This method keeps your text visible in the same color while changing only the background.

.button {
  display: inline-block;
  color:#fff;
  padding: 10px20px;
  font-size: 20px;
  background-color:red;
}

.button:hover {
  background-image: linear-gradient(rgba(0, 0, 0, 0.4) 00);
}
<divclass="button"> some text </div><divclass="button"style="background-color:lightblue;"> some text </div><divclass="button"style="background-color:green;"> some text </div><divclass="button"style="background-color:grey;"> some text </div>

To have a transition:

.button {
  display: inline-block;
  color:#fff;
  padding: 10px20px;
  font-size: 20px;
  background: linear-gradient(transparent,rgba(0, 0, 0, 0.4)) top/100%800%;
  background-color:red;
  transition:0.5s;
}

.button:hover {
  background-position:bottom;
}
<divclass="button"> some text </div><divclass="button"style="background-color:lightblue;"> some text </div><divclass="button"style="background-color:green;"> some text </div><divclass="button"style="background-color:grey;"> some text </div>

Another idea with mix-blend-mode:

.button {
  display: inline-block;
  color: #fff;
  padding: 10px20px;
  font-size: 20px;
  background-color: red;
  background-image: linear-gradient(rgba(0, 0, 0, 0.4) 00);
  background-blend-mode: lighten;
}

.button:hover {
  background-blend-mode: darken;
}
<divclass="button"> some text </div><divclass="button"style="background-color:lightblue;"> some text </div><divclass="button"style="background-color:green;"> some text </div><divclass="button"style="background-color:grey;"> some text </div>

Solution 2:

This is one way you can do it

.button {
  background-color: red;
}

.button:hover {
    filter: brightness(60%);
}
<buttonclass="button">Button</button>

Anything above brightness(100%) will increase the brightness and anything less will make it darker.

Solution 3:

Put pseudo element behind button content and modify its opacity on hover

const colors = ['#052F5F','#005377','#06A77D','#D5C67A','#F1A208'];
const btn = document.querySelector('.Button');
btn.addEventListener('click', (e) => {
  e.preventDefault();
  btn.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
});
.Button {
  background-color: #F1A208;
  color:#fff;
  border: 0;
  border-radius: 20px;
  padding: 10px22px;
  cursor:pointer;
  position: relative;
}
.Button:before {
  content: '';
  position: absolute;
  z-index:0;
  border-radius: 20px;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background-color: rgba(0,0,0,0);
  transition: .2s ease;
}
.Buttonspan {
  position: relative;
  z-index:1;
}
.Button:hover:before {
  background-color: rgba(0,0,0,.3);
}
<buttonclass="Button"><span>hover me</span></button>

Solution 4:

Using HSL color values may meet your needs. The HSL color model is more human-readable than Hex (#FF334E).

HSL color values are specified with: hsl(hue, saturation, lightness).

div {
  width: auto;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  margin: 0.5rem0;
  padding: 1rem2rem;
  color: white;
}

.Button {
  --hue: 712;
  --saturation: 100%;
  --lightness: 60%;
  background-color: hsl(var(--hue), var(--saturation), var(--lightness));
}

.Button.dark:hover {
  transition: 0.2s ease-in;
  --lightness: 40%;
  /* 20% dark */
}

.Button.light:hover {
  transition: 0.2s ease-in;
  --lightness: 80%;
  /* 20% light */
}
<divclass='Button dark'>some text</div><divclass='Button light'>some text</div>

Post a Comment for "How To Make An Element's Background-color A Little Darker Using Css"