0

I have a few cards that all have different-colored background linear gradients. On mouse over, I want to overlay a semi-opaque white background so I can put some popup text, while preserving the original gradient underneath. How can I accomplish this in an automated way?

I don't want to do the brute force way: add a :hover in each individual card's CSS and add a second linear-gradient(rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.3)) after the initial linear-gradient(actual card color).

I'd like to use jQuery, but I can't get it to append to the background property. It only replaces with a new value.

Fiddle here or code below:

<div class="card card--1">
  <p>
    Some text
  </p>
</div>
<div class="card card--2">
  <p>
    More text
  </p>
</div>
<div class="card card--3">
  <p>
    Even more text
  </p>
</div>
    .card {
      height: 50px;
      border-radius: 15px;
      position: relative;
      transition: 0.1s all;
      transform: translateY(0px);
      text-align: center;
        
      &--1 {
        background: linear-gradient(0deg, #ffd66e, #ffeab2, #c8f8ff 80%);
      }
    
      &--2 {
        background: linear-gradient(11deg, #f5ac9c, #ffefe3);
      }
    
      &--3 {
        background: linear-gradient(18deg, #693b10, #fff5d3);
      }
    }
$(document).ready(function() {
  $(".card").mouseenter(function() {
    $(this).css("background", "linear-gradient(0deg, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0.3))")
  })
});

3 Answers 3

1

You don't need to add hover to each individual card. You can just add it once to .card.

Remove the JQ code and add the following to the CSS and it works.

.card:hover
{
  background: none;
  background-color: rgba(255, 255, 255, 0.3);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Another more complicated way to do it with JQ would be to put 2 more divs in each card. One div for the normal bg color, and another div for the rollover. When you rollover the .card div it hides (or puts the opacity at 0) for div#1 and shows div#2, and roll out it does the opposite.
Are you sure this works the way I'm asking? When I try your approach, it doesn't maintain the original linear-gradient behind the new overlay. I don't want to completely replace the color with a new color, I want to overlay.
This comment you wrote got the closest to actually answering the question - I implemented a similar solution but using opacity: 0.5 instead of switching from 0 to 1.
0

Another way

Change JQ to:

$(document).ready(function() {

  $(".card").mouseenter(function() {
  $(this).addClass( "cardRollover" );
  })
  
    $(".card").mouseleave(function() {
  $(this).removeClass( "cardRollover" );
  })
  
});

Add to CSS:

.cardRollover{
   background: none;
  background-color: rgba(255, 255, 255, 0.3);
}

1 Comment

I appreciate you trying, but this answer, like your other one, doesn't answer my question. This completely replaces the background color with a new color. I want to overlay a color over the original linear-gradient.
0

I haven't found any way of appending linear-gradients to an existing background: linear-gradient, so I implemented a workaround - I added an overlay div that only shows on hover, with opacity: 0.5 and visibility: hidden.

<div class="card card--1">
  <div class="card__hover"></div>
  <p>
    Some text
  </p>
</div>
<div class="card card--2">
  <div class="card__hover"></div>
  <p>
    More text
  </p>
</div>
<div class="card card--3">
  <div class="card__hover"></div>
  <p>
    Even more text
  </p>
</div>
.card {
  height: 50px;
  border-radius: 15px;
  position: relative;
  transition: 0.1s all;
  transform: translateY(0px);
  text-align: center;

  &:not(:first-child) {
    margin-top: 4rem;
  }

  &--1 {
    background: linear-gradient(0deg, #ffd66e, #ffeab2, #c8f8ff 80%);
  }

  &--2 {
    background: linear-gradient(11deg, #f5ac9c, #ffefe3);
  }

  &--3 {
    background: linear-gradient(18deg, #693b10, #fff5d3);
  }
}

.card__hover {
  position: absolute;
        width: 100%;
        height: 100%;
        background-color: rgba(255, 255, 255, 0.5);
        border-radius: 15px;
        visibility: hidden;
}
$(document).ready(function() {
  $(".card").mouseenter(function() {
    $(this).children(".card__hover").css("visibility", "visible")
  })
  $(".card").mouseleave(function() {
    $(this).children(".card__hover").css("visibility", "hidden")
  })
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.