Integrate a partially see-through overlay onto a div

I want to implement a feature where the user can click on a card to "cancel it". I can handle the JS, but I'm looking for the best way to add a "red opaque overlay" on top of the card after it's clicked.

My goal is to create a CSS class that makes the overlay not just transparent at 40%, but also slightly "red". And ideally without adding another div that wraps the entire card.

.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/4AAQSkZJRgABAQAAAQ... (content truncated)
  </div>

Edit I have been able to achieve some results by using this CSS code, however, I believe there might be a better or more optimal way to do it.

.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);
}

Answer №1

If you want to create an overlay effect that appears when a user clicks on a specific element, you can achieve this by utilizing either the ::before or ::after pseudo-elements. These pseudo-elements will act as the overlay and can be styled accordingly.

To implement this, you would initially style the overlay using the ::after pseudo-element and position it behind the target element (in this case, the .card) using a negative z-index value to hide it at first. When a click event occurs, you can add a custom class like is-canceled to the target element, which will adjust the z-index of the overlay to make it visible on top of the target element.

To ensure a smooth transition for the overlay, you can set its initial background-color to transparent and update it to a desired color (e.g., reddish background) when the custom class is applied. By using CSS transitions, you can specify how the change in styles should animate when the class is added or removed from the element.

To see this in action, check out the interactive demo below where you can toggle the overlay by clicking on the .card:

[Interactive Demo]

The provided example serves as a starting point for creating overlays using CSS pseudo-elements and transitions. Feel free to modify and enhance the code based on your specific needs or design preferences.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a FontAwesome icon be used as a button in Bootstrap?

Hi there, I'm trying to use a fontawesome icon as a button in my code. Here is what I have so far: <!DOCTYPE html> <html lang="en"> <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Tips for choosing Week Range values with Selenium WebDriver

Currently, I am working with Selenium WebDriver and I am trying to figure out how to select week range values from a dropdown menu at once. I have a dropdown called Period, which, when selected, automatically reveals additional dropdowns for From week and ...

Incorporate row numbering feature into jQuery DataTables

Is there a way to have jQuery datatables generate a row number column in the first column, similar to a datagrid in VB? This is what I'm aiming for: If you have any insights on how this can be achieved, please share! ...

Exploring Javascript's potential with for loops and arrays

Looking to create a webpage with the following features: (generate question)...this will be a button ? x 1 = ( ) ? x 2 = ( ) ... ? x 9 = ( ) (check answer)...this will also be a button The user starts by selecting a number after clicking th ...

How come the image file path isn't functioning properly in bootstrap?

Here is the directory structure of my folders: Main Directory: C:/website/index.html Images folder: C:/images/sample.jpg I tried linking the image like this, but the path seems to be incorrect. I have included Bootstrap CDN in the header section. Howev ...

The SimpleHTTPServer is causing Google Maps Places Autocomplete feature to malfunction

Currently, I am implementing a location search feature along with form auto-fill using the google.maps.places.Autocomplete functionality. While accessing the HTML file directly (file:///location.html), everything is functioning as expected. However, when ...

The inner div is too big for the outer div to contain

I am facing an issue with three div elements, where the first (content) contains two nested div elements. While in Chrome, the last two div elements fit perfectly inside the first div, in MS Edge, the last div does not and starts after the first div on th ...

What is the best way to eliminate the left padding on a TimelineItem component?

When using the Timeline component to display information, there are three columns: TimelinItem, TimelineSeparator, and TimelineContent. I tried setting the padding of TimelineItem to 0 but it didn't work. The <Trace/> component is a sub-compone ...

Is it possible to set an onmousedown event to represent the value stored at a specific index in an array, rather than the entire array call?

Apologies if the question title is a bit unclear, but I'm struggling to articulate my issue. The challenge lies in this synchronous ajax call I have (designed to retrieve json file contents). $.ajax({ url: "/JsonControl/Events.json", dataTyp ...

Concealing and revealing elements through css styling

In my journey to learn HTML, CSS, and PHP, I took some online courses to gain knowledge. Now, I am venturing into creating a site using Wordpress to further enhance my skills through practical experience. Recently, I created a page with a custom video fie ...

Ignored are the white spaces within a pre tag

From what I understand, the 'pre' tag is supposed to collapse all white spaces and tabs into one space. However, I have noticed that this doesn't happen for me. I'm curious as to why this may be the case. Could it possibly be dependent ...

How can I make CSS/HTML tables display inline until they are wider than the parent div?

Is there a way to position two tables next to each other until one table grows wider than the set minimum width, and then have the second table appear below the first, with both tables centered within their parent <div>? This is the current setup: ...

What is the best way to retrieve text content along with images from an element using jQuery?

Is there a way to retrieve the combined text contents of each element in the matched set, while still including any images present? Consider the following input: <div id="test">Lorem ipsum <strong>dolor</strong> sit amet, consectetur &l ...

Creating PDFs with Puppeteer and math expressions using the page.$eval function

I have recently started using Puppeteer and have successfully converted an entire website to PDF. However, I am facing a challenge when trying to convert MathJax equations within a div to PDF. Currently, I am using the following code: // Requiring puppetee ...

When hovering over, my text and image shift to the left in a strange and abnormal manner

Recently, I made a small code addition to an existing one in order to display text below an image and have the text highlighted in blue on mouse over. Everything seemed to be working fine until I noticed that on mouseover, the text and image shifted to the ...

CSS tip: A workaround for hiding overflow without affecting 3D transforms is to use "overflow: hidden" in a different way

I'm currently working on creating a horizontal section with a parallax effect. My goal is to have an image in the background that scrolls at a different speed than the rest of the page. My challenge lies in containing the parallax element within its ...

Utilizing AggregateRating and ratingValue to represent ratings in the form of images

I'm facing a challenge while trying to incorporate rich snippets on my website, particularly in the AggregateRating section where the ratingValue is only displayed as an image. This is how my markup currently looks: <tr itemprop="reviews" itemsco ...

schemas.org: The title attribute for SiteNavigationElement should be used instead of text

Is there a way to display the name based on the attribute title instead of text for the link, which currently only shows an icon using font-awesome? Here is how my links are structured: <a itemprop="url" href="whatever.html"> <i class="fa fa-h ...

What is the best way to customize the appearance of an Angular tree component?

I'm attempting to incorporate the style of the ACE Admin theme into the angular-tree-component. Currently, my tree looks like this: https://i.sstatic.net/ktiEf.png However, I desire to apply styles from the Angular tree guide in order to achieve a ...

Steps to start a function in an HTML document using a JavaScript file (Chrome Extensions)

I am currently working with a Scoring Board Chrome App and I am relatively new to Chrome Apps. index.html - This is just a snippet of the code where users can input the number of players for the scoring board. When submitted, another HTML file is opened d ...