Home > Enterprise >  CSS - Add a semi-transparent layer on top of a div
CSS - Add a semi-transparent layer on top of a div

Time:12-04

I would like to allow my user to click on the card to "cancel it". I can take care of the JS, but I am wondering what's the best way to add a "red opaque overlay" on top of the card after the user clicks it.

So ideally I can write a css class such as but also make it that it's not simply transparent at 40% but also slightly "red". And I don't want to create another dive that wraps the whole card if possible.

.card-deactivated{
  opacity:40%
}

.card{
  max-width:300px;
  min-width:200px;
  height: 350;
  display: flex;
  flex-direction: column;
  padding: 0px 0px 20px;
  gap: 10px;
  
  background: #FFFFFF;
  /* Gray/4px */
  box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
  border-radius: 10px;
  overflow:hidden;
}

.card-img{
  order: 0;
  display:block;
  margin-bottom:2rem;
}

.card-title{
  align-self:center;
  margin-bottom:1rem;
  
}

.card-description{
  
  align-self:center;
  margin:0px 20px 1rem;
  text-align:center;
}
<div class='card'>
    <img class='card-img' alt='Card Image' src='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYVFRgWFhUZGRgaHRwcHBwcHBwaHxoaGBgaGRoaGhocIS4lHB4rHxgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QHxISHzQrJCs0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NP/AABEIAOEA4QMBIgACEQEDEQH/xAAbAAABBQEBAAAAAAAAAAAAAAAEAAIDBQYBB//EADcQAAEDAQcCAwYFBQEBAQAAAAEAAhEhAwQFEjFBUWFxIoGRBhMyobHBQlLR4fAUFWJy8SMzU//EABoBAAIDAQEAAAAAAAAAAAAAAAECAAMEBQb/xAAlEQACAgIDAAIDAAMBAAAAAAAAAQIRAyEEEjEyQRMiUWFxgUL/2gAMAwEAAhEDEQA/AM5mUNu6XIkfRBONVxInr5MYVwp0iu64y2IBECvQE/NWJFTOPcNh5ytN7O4ns4gHnnqszmDampUT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tB0Tmvc3dByH60tDH2b5I0Ch92BuZ isTbB4iADyPuhX3SNTT0 aKkgeAzmAnVJztp7LjmAHSicHRsjQbH3e8vZVh7hXF2x14ZlJLeSKqlI30XGvrKlAdP02N1xuYzOmmvTtyrRmKsDdTAkkc00Xn7Gfibso3272E5XmDWJ iKcvplMsUGz0ixv2fK8GKGlPRQvxHLAFKc9d1gTilo2sjmo022ITP7y gIaRPWfmUXKdaFWCFm dijgdQuWuLNGrllrvei9TWrIYSSlUpf0DhBOqLK3xtjKAx3rP6KltsacZguNZG3l2QVq5p/CJTQwpG79LowS8Qy3tH2hqYHA 6jbdOqKyHkIy7Xdn4pcOQ4CPWiKbYz0V9jdm7mTwIEoqyhs0aOhOnkKqZzLJmoca7EHbcjeU60vzY GY0zBpI SdIG2DsvT58LQOsfsnC2doXmvClFq9zRLgGgTDaRJ0oonvI4JrXXtqlY6Q0sI1Lj5GZRuHXl9m4EPOaZjpqhruzdzta10891YYTdM1o0E714A3PojCT7KhMij1dmsxuxtrQ2brJ VzYcQWy0g89tVYXS/Pa4MtGgODQSRoTv2Crrx7SsY7KyHRSTx0AVrdb0y1aLSBQHMOIErpRnFuk9nDyY5pK1oL/rmfmHzSVD/AH x/I30STd1/Sr8Mv4ZrCtPJUt  I9ykkuR9Ho4/MERL9EkkGWEVnr/ADlFXn4SkkgvRGV2ygKSSuRCRiZZ79kkkGKE3PRyHt9W/wCq6kmXgr9IrxqUKNUkky wovbhorC8/B5pJKr6KpfJFRafEnu0SSSfRpQrHUd/uEbePiPY/VJJRCseP/mP9ioLP4h3CSStgMGXnVnmuD4z2SSUYEQv JXGFb/6FJJJH1CZPCrstfJav2f/APjb9j9F1JXY/mZ R8DHpJJJyo//2Q=='>
    <h3 class='card-title'>Name Surname</h3>
    <p class='card-description'>This is a long descriptionThis is     a long descriptionThis is a long descriptionThis is a long descriptionThis is a long description </p>
  </div>

Edit I am achieving some sort of result by leveraging this css but I don't think I am doing it "right"...or at least in the best possible way.

.card{
  max-width:300px;
  min-width:200px;
  height: 350;
  display: flex;
  flex-direction: column;
  padding: 0px 0px 20px;
  gap: 10px;
  
  background: #FFFFFF;
  /* Gray/4px */
  box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
  border-radius: 10px;
  overflow:hidden;
  position:relative;
}

.overlay-selected::after {
    content: ""; 
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: .3;
    background-image: linear-gradient(90deg, #eaee44, #eaee44);
}

CodePudding user response:

You can achieve this by using one of the pseudo-elements, either ::before or ::after, that will act as an overlay when the user clicks on the .card element.

Basically, we'll initially style that overlay, we'll use the ::after pseudo-element, and place it behind the .card element using z-index of -1 so that the overlay is hidden initially. When a click occurs, we add a class, let's call it is-canceled, to the .card element that simply sets the z-index of the overlay to a higher value (like 2 or 999) so that the overlay appears on top of the .card element and act as the intended overlay.

To make the overlay transition smoothly, we'll initially set its background-color to transparent and override that when the is-canceled class is attached to the .card element. When the is-canceled class is attached to the .card element, we'll set the background-color to a reddish background (something like rgba(255, 0, 0, .7). Using the transition property we can instruct the browser to have a smooth transition when the is-canceled is added/removed from the .card element.

To illustrate, here's alive demo that allows you to toggle the overlay when you click on the .card element:

const card = document.querySelector('.card');

card.addEventListener('click', () => card.classList.toggle('is-canceled'));
.card {
  position: relative; /* required to correctly position the overlay */
  max-width: 300px;
  min-width: 200px;
  display: flex;
  flex-direction: column;
  padding: 0px 0px 20px;
  gap: 10px;
  box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
  border-radius: 10px;
  overflow: hidden;
}


/* initial overlay styles */
.card::after {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  inset: 0;
  background-color: transparent;
  transition: all .4s 0s ease;
  z-index: -1;
}

/* styles to be applied to the overlay when the "is-canceled" class is added */
.card.is-canceled::after {
  background-color: rgba(255, 0, 0, .7);
  z-index: 999;
}

.card-img {
  order: 0;
  display: block;
  margin-bottom: 2rem;
}

.card-title {
  align-self: center;
  margin-bottom: 1rem;
}

.card-description {
  align-self: center;
  margin: 0px 20px 1rem;
  text-align: center;
}
<div >
  <img  alt="Card Image" src="https://via.placeholder.com/250">
  <h3 >Name Surname</h3>
  <p >This is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long description </p>
</div>

The above demo is just for demonstration purposes and you're invited to build upon it and customize it the way you see fit.

  • Related