The overlay div is not displaying over my main div as intended

Having trouble with my overlay design - I've set up a button that should display an overlay div on top of my main div when clicked, but for some reason it's not appearing. Despite watching tutorials and looking for similar issues, I'm unable to figure out why it's not showing.

This is the structure of my HTML page:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Baloo+Chettan+2|Liu+Jian+Mao+Cao&display=swap" 
rel="stylesheet">
<link rel="stylesheet" href="thiscss/randomqoutes.css">
<title>Document</title>
</head>

<body>

<div class="container">
    <h2>RCaT11</h2>
    <div class="qoutewrapper">
        <h1>Have an Inspirational day!</h1>
        <button id="btn">Genererate Qoute</button>
    </div>

    <div class="overlaycontainer">

    </div>
    </div>

    </body>
    <script type="text/javascript" src="javascript/randomqoutes.js"></script>

    </html>

In the CSS file, I initially set the "overlaycontainer" class display property to none so it remains hidden until the button is clicked:

body {
background: linear-gradient(to left, #ED8F20, #EBD626);
margin: 0;
padding: 50px;
     }

 .container {
 display: flex;
  }

   .qoutewrapper {
    font-family: 'Liu Jian Mao Cao', cursive, sans-serif;
    width: 900px;
    height: 250px;
    margin: 0 auto;
    margin-top: 10%;
   padding: 10px;
   display: flex
   }

 .qoutewrapper h1 {
white-space: nowrap;
font-weight: 700;
font-size: 70px;
margin: 0 auto;
position: relative;
left: 90px;
margin-top: 50px;
}

h2 {
font-family: 'Liu Jian Mao Cao', cursive, sans-serif;
font-size: 2em;
position: absolute;
left: 90%;
color: red;
float: right;
  }

.container button {
color: rgba(26, 25, 25, 0.897) !important;
text-transform: uppercase;
background: none;
padding: 12px;
border: 2px dotted rgba(26, 25, 25, 0.897) !important;
font-weight: 800;
float: right;
margin-top: 200px;
}

.container button:hover {
color: #EBD626 !important;
background: rgba(26, 25, 25, 0.897);
border-color: none !important;
transition: all 0.4s ease 0s;
display: flex;
}

.overlaycontainer {
width: 100%;
height: 100%;
background-color: black;
opacity: 0.7;
display: none;
 }

In the JavaScript file, I added code to set the display property of "overlaycontainer" to flex upon clicking the button, but it's still not displaying:

document.getElementById('btn').addEventListener('click', function() {
document.querySelector('.overlaycontainer').style.display = "flex";
 })

Answer №1

Adjust the placement of your .overlaycontainer with these CSS rules to make it visible.

    position: absolute;
    left: 0;
    top: 0;

An element needs height and width to be displayed, even though setting both may not always be necessary. The issue you were encountering was due to trying to set the height of the .overlaycontainer to 100%, which wasn't working because its parent container required a fixed width to function properly.

Usually overlays are positioned based on their context within the HTML structure.

The position property is used for controlling an element's location. When using position:absolute, the element is positioned relative to its closest ancestor with a position:relative.

Moving the values for left or top will change the position of the .overlaycontainer. If you apply position: relative to the parent or any ancestor elements of the overlay (which is often necessary), it will shift in relation to that element. In cases like ours where none of the ancestors have position: relative, the element will be positioned relative to the body.

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

Could someone assist me with understanding how to use the Load function in JQuery?

I have a set of links with the class "ajax" that are meant to fetch content from an element with the id "test" in the file "data.html" located in the same directory. This content should then be inserted into the div with the id "content" on my page: <s ...

Copying an instance in JavaScript - The copy method

Is there a way to easily duplicate all properties and methods of a class instance? class A { get prop1() { return 1; } get prop2() { return 2; } doStuff() { return this.prop1 + this.prop2; } } class B extends A { get prop1() { ...

An issue occurred with lodash during the construction of /@types/lodash/common/object.d.ts at line 1188, character 142: Expected '('

Things were going smoothly, but out of nowhere my build started failing. Here are the errors that are popping up: ERROR in /node_modules/@types/lodash/common/function.d.ts (852,68): ';' expected. ERROR in /node_modules/@types/lodash/common/commo ...

The Click Event Is Triggering Even with Correct Callbacks Being Set

I am struggling to understand why these functions are not executing properly. I know the correct syntax for binding a function, like this: $('#idOfThing').bind('click', foofunc); function foofunc() { ///do things } However, I am facin ...

Having trouble figuring out how to play an mp3 using the <audio> tag. The console is showing an error message that says, "Unable to decode media resource XYZ."

I've been frustrated while trying to make the HTML5 <audio> element function properly. My webpage has two elements - one serving a file from my server and the other serving the same file from a different source server: <audio preload="metada ...

Combining Framer Motion with Next.js: Resolving conflicts between layoutId and initial props in page transitions

As I work on creating smooth image page transitions using Framer Motion and Next.js with layoutId, I've come across a challenge. Here is my main objective: The home page displays an overview with 3 images When an image is clicked, the other images f ...

Strategies for integrating an app into router files within a Node/Express application

Below is the structure of my app.js: app = express(); setup.configure(app); //...additional configurations (such as database setup, middleware definition, etc.)... var api = require('./routes/api'); app.use('/api', api); module.exports ...

Issue encountered while utilizing PHP sessions

I'm currently developing a basic login page that utilizes JS, JQuery, and PHP. login.php <!DOCTYPE html> <head> <title>AJAX Login Learning Activity</title> <link rel="stylesheet" type="text/css" href="login.css"> ...

Uneven rotation of objects within the browser

I am currently working on visualizing objects in a web browser. The goal is to display these objects rotating around one axis at a time (either X, Y, or Z) or allow the user to interactively rotate them using their mouse. However, I have encountered severa ...

Error message "Undefined is not a function" occurred while using jQuery's .replace and scrollTop functions

I'm having issues with the scroll function in my code. It doesn't seem to be able to locate the ids in my HTML, even though I can't figure out why. I had a previous version that worked perfectly fine (unfortunately, I didn't save it D:) ...

How to maintain a child component's state without resetting it when the parent component's state changes and also retrieve the values of all child components in ReactJS with Typescript

Recently, I've been diving into React and encountered a roadblock while working on a custom dropdown filter for a table in my React application. Each column in the table has a set of dropdown values and an Apply button. To implement this, I created a ...

Validate the historical and present contents of a variable

Let's take a look at my code snippet: <button id="submit">Roll</button> <script> function generateRandomNumber(range) { var result = ''; var characters = '123456789'; var c ...

Mistakes within the react-router-dom package

I have been trying to connect MongoDB Realm to React by following a tutorial. Unfortunately, the tutorial is outdated and I encountered errors while trying to run the code. Initially, I received a warning stating that the leaf route does not have an elemen ...

The IDE is showing an error, but Jest is able to run it flawlessly

I recently created a Jest unit test for a TypeScript function called checkEmail, which internally uses showAlert. The showAlert function in the utils.ts file looks like this: export const showAlert = (message: string) => { toast(message); }; In my ...

Vue.js will trigger the updated() method only when a particular array undergoes changes

Working on a chat feature and looking for a way to automatically scroll to the end of the conversation when new messages are added. The current solution involving the updated() function works well, but there's a complication with a vue-timepicker com ...

How to stop the overflow scrollbar in a grandchild element with CSS grid

Encountered an unusual issue while using a combination of CSS grid and overflow auto. In the scenario where there are 3 elements, the outermost element has display: grid, the middle element has height: 100%, and the innermost element has both height: 100% ...

What is the best way to extract values from promises that are still pending?

Having some issues with the code below and unable to achieve the desired output. Any help in identifying what's wrong would be greatly appreciated. Thanks! Here is the code: // Making http requests const getJSON = require('get-json'); ...

"Troubleshooting Error: When accessing a property in AngularJS,

My goal is to retrieve a date value. However, I encounter an error message saying 'Cannot read property 'NTLI' of undefined' whenever the checkbox is unchecked and the date picker is invisible. Strangely enough, everything works fine wh ...

What about a lightbox with enhanced jQuery features?

As a newcomer to jQuery, I've never experimented with lightboxes before. However, I was tasked with creating something fun for April Fools' Day at work. Naively, I accepted the challenge thinking it would be simple, but now I find myself struggli ...

What is the process for setting default parameters using a recompose, lifecycle HOC?

I've created a custom recompose, lifecycle HOC: import { lifecycle } from 'recompose'; export function myHoc(title) { return lifecycle({ componentDidMount() { console.log(title) } }); } export default my ...