Text with a fading black overlay

I am seeking to develop a unique transparent overlay effect for a div that includes text. The objective is to have the overlay fade in and out upon activation by pressing a button. Traditionally, this can be achieved by placing a div within another div with absolute positioning and full width/height, adjusting the opacity using an ease or animation (Black transparent overlay on image).

However, I also require the ability to select the text within the div. Switching the overlay from

display: block

to

display: none

Will disrupt the fading animation. How can I smoothly transition from block to none so that I can recycle my underlying div?

Answer ā„–1

If you're familiar with jQuery, this code snippet may be just what you need:

$(document).ready(function() {
  $("#popupbtn").click(function() {
    $(".overlay").fadeIn(1000);
    $(".overlay").css("display","block");
  });
  $("#close").click(function() {
    $(".overlay").fadeOut(1000);
    //$(".overlay").css("display","none");
  });
});
  
body,
html {
  margin: 0;
  padding: 0;
 }
.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: RGBa(0,0,0,0.5);
  display: none;
}
.popup {
  background: green;
  width: 200px;
  height: 200px;
  display: inherit;
  position: absolute;
  left: 50%;
  top: 50%;
  margin: -100px 0 0 -100px;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="popupbtn">click me</button>
<div class="overlay">
  <div class="popup">
    <p>popup text</p>
    <button id="close">close</button>
  </div>
</div>

Explanation: Once the $(".overlay").fadeIn(1000); animation finishes, the CSS for display: block will take effect.

I hope this solution proves helpful!

Answer ā„–2

If you want to play around with opacity, you can use the pointer-events property in CSS. By setting it to "none", you allow events to pass through as if the element was invisible. On the other hand, setting it to "all" makes the element clickable and interactive. More information on this property can be found at https://css-tricks.com/almanac/properties/p/pointer-events/

The browser support for pointer-events is good overall, except for older versions of Internet Explorer (pre-10). However, there is a polyfill available on GitHub to address this issue: http://caniuse.com/#search=pointer-events

$('button').on('click', function(e) {
e.preventDefault();
  $('.container').toggleClass('overlay-open');
})
.container {
  position: relative;
}

.overlay {
  position: absolute;
  top: 10px;
  right: 10px;
  bottom: 10px;
  left: 10px;
  background-color: black;
  opacity: 0;
  transition: opacity 250ms ease-in-out;
  pointer-events: none;
}

.overlay-open .overlay {
  opacity: 1;
  pointer-events: all;
}

button {
  margin-bottom: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Toggle Overlay</button>
<div class="container">
  <div class="back">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Excepturi ipsam, illum sed dolorum quos modi, quibusdam cum tempora molestias laboriosam voluptas. Fuga corporis earum modi, aut nisi in molestias explicabo voluptatem iure distinctio tempora, iusto doloremque. Inventore culpa eligendi dolore. Expedita, officia, assumenda. Id magnam molestias saepe, cupiditate, architecto autem.
  </div>
  <div class="overlay"></div>
</div>

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

Child element does not follow z-index rules

I have a website Suoi Hong Resort However, the menu block displays behind the logo. The navigation class has a position of absolute and a z-index of 3. The logo has a position of absolute and a z-index of 2. The menu block has a z-index of 10. Despite th ...

Having trouble displaying the image on the webpage

Here is the code for my header where I am trying to set the background. <header> <img src="images/MeMain.jpg" alt="Drawing of Me" class="profile-image"> <h1 class="tag name">Hello, Iā€™m Me.</h1> <p class="tag location"> ...

adjust the vertical size of a specified container

I am attempting to have the green div fill the height regardless of the content in the yellow div. Both the green and yellow divs are contained within the grey div. Refer to the following code for clarification: http://jsbin.com/ofelov/1/edit (As for th ...

The CSS3 Animation-fill-mode: An Essential Property

I am working on a cool effect where text slides into the window and remains in place after the animation is complete. Surprisingly, I have managed to get the animation to work on one part of the text, but for some reason, the second part is not cooperating ...

Angular 2's innovative approach to implementing a sticky footer concept

Is there a way to make my footer sticky without being fixed? I attempted using the CSS negative margin trick, but it did not work as expected. In the provided Plunker code, I tried to replicate the issue in my Angular 2 app. The goal is for the footer to s ...

Aligning images horizontally within individual div containers within a list

Struggling to align images horizontally, they are all stacked on top of each other. New to this and could use some assistance figuring it out! Any help would be appreciated! <div class="social"> <ul> <div class="facebook"> ...

Ensure that both tables contain columns of equal size

I am facing a challenge with 2 HTML tables that are positioned directly on top of each other. Each table has the same number of columns, however, the text within them may vary. Additionally, both tables can consist of multiple rows. My goal is to ensure th ...

WebView Background Image Displays in Simulator but Not on Actual Device

I am facing an issue while trying to showcase a background image from an HTML document in a UIWebView. Interestingly, the image appears perfectly with text overlay when I preview it on the iOS Simulator or Google Chrome. However, when testing it on my actu ...

At times, Firefox may fail to fully showcase an image in fullscreen mode

Currently, my goal is to showcase an image in fullscreen mode using JavaScript. (Refer to the Fullscreen API - https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API) JavaScript snippet: var requestFullscreen = function(el) { if(el.requestFullsc ...

PHP code for displaying images on the same level

Is there a way to position images side by side in PHP? For example: image image image image. Currently, with the following code: print "</h2><br><a href='form.php'><img src=***.jpg width=100 height=100 /><br> ...

How do I prevent the React <div id="root"></div> from disrupting the layout of my design?

I am working on implementing a ReactJS template, but the footer keeps breaking and I need it to stay at the bottom. The main culprit is the <div id="root"></div>. Does anyone know how to fix this issue? https://i.sstatic.net/xNRKe.png import ...

I am currently working on a project where I must verify the Pass or Fail icon using Selenium WebDriver for my application

This is the code snippet found in the application div id="StatusCircle" style="float: right; width: 50px; height: 50px;-webkit-border-radius: 25px;-moz-border-radius: 25px;border-radius: 25px;background: RED;" - indicating failure div id="StatusCircle" ...

The clash of interests between jQuery and Bootstrap

I'm facing a problem with conflicting jQuery and Bootstrap files in my header.php code. Whenever I include the jQuery file before the bootstrap.js file, it causes issues with the tabs on my website where clicking on them does not navigate me to the co ...

React Card with Overflowing TableCell Texts

I am facing an issue with a table inside a table card where the TableCell is overflowing horizontally when the word is too long. How can I break it to the next line? Please refer to the "code" section for details. For more information, please visit my cod ...

Issue with NPM and Local Host Preventing Proper Functionality of LESS CSS and Installation of LESS Package Control in Sublime 3

Completely new to LESS and running into trouble with my styles not showing up. I thought I had everything sorted out the other day, but now it seems like none of it is working. I attempted to install the LESS Package Control, but that didn't work out ...

Customizing the appearance of a radio button within a form

I need assistance with styling a form that contains a radio button. I'm looking to customize the appearance of the radio button by adding a background image and also changing the default view of the check image. Additionally, I want the form to be cen ...

Efficiently reducing the size of a CSS selector string with javascript

Is there a library that already performs this function? I've only been able to find online tools. The reason I am interested in accomplishing this task using JavaScript is because I need to ensure that the strings a > b, a and a> b,a are conside ...

Issues with Vertical Alignment and Navigation

Link: Please resize the browser to mobile view. Issue with Header: I am facing alignment issues in the header. The elements do not seem to be aligned properly at the center of the header. The Android logo is aligned, but the text and dash image are not. ...

Utilizing HTML values within CSS3 content

I'm looking to integrate year numbers into circular shapes along a vertical timeline. My initial concept involves using CSS content and possibly leveraging JavaScript to extract data properties from the HTML, such as year="1958", and inserting it into ...

In Firefox, the focus outline is not shown on a div that has a mask-image CSS rule applied to

In my code, I have multiple div elements that have the mask-image and -webkit-mask-image CSS rules applied to them. These divs are being used as UI slider components, so I want keyboard users to be able to tab between them and use the arrow keys to adjust ...