Position a DIV above another DIV

Recently, I was given the task of replicating a JavaScript popup that our former web developer created. While I've managed to make it very similar, there is one thing I'm struggling with - getting the close button (X) to float over the popup in the top right corner rather than being positioned directly on the top right corner of the popup itself. I've experimented with various position: values in the CSS and tried different attributes from Stack Overflow, but nothing seems to work as desired.

Here is the CSS code:

#popup {
    position: absolute;
    display: hidden;
    top: 50%;
    left: 50%;
    width: 400px;
    height: 586px;
    margin-top: -263px;
    margin-left: -200px;
    background-color: #fff;
    z-index: 2;
    padding: 5px;
}
#overlay-back {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;
    opacity: 0.7;
    filter: alpha(opacity=60);
    z-index: 1;
    display: none;
}
.close-image {
    display: block;
    float: right;
    cursor: pointer;
    z-index: 3;
}

And here is the HTML code:

<div id="overlay-back"></div>
<div id="popup">
    <img class="close-image" src="images/closebtn.png" /><span><img src="images/load_sign.png" width="400" height="566" /></span>
</div>

Answer №1

To enhance your .close-image class, simply include the following properties: position, right, and top.

.close-image {
   cursor: pointer;
   display: block;
   float: right;  
   z-index: 3;
   position: absolute; /*newly added*/
   right: 5px; /*newly added*/
   top: 5px;/*newly added*/
}

Answer №2

Here's the suggested CSS code:

.close-image {
    cursor: pointer;
    z-index: 3;
    right: 5px;
    top: 5px;
    position: absolute;
}

Answer №3

.close-image {
    cursor: pointer;
    display: block;
    float: right;
    position: relative;
    top: 22px;
    z-index: 1;
}

It seems like this could be the solution you are seeking.

Answer №4

Although this post may be a bit outdated, I have a potential solution for those facing the same issue:

To begin with, I suggest modifying the CSS display property for #popup to "none" rather than "hidden".

Next, make changes to the HTML code as shown below:

<div id="overlay-back"></div>
<div id="popup">
    <div style="position: relative;">
        <img class="close-image" src="images/closebtn.png" />
        <span><img src="images/load_sign.png" width="400" height="566" /></span>
    </div>            
</div>

Additionally, adjust the style properties as indicated below:

.close-image
{
   display: block;
   float: right;
   cursor: pointer;
   z-index: 3;
   position: absolute;
   right: 0;
   top: 0;
}

I came across this technique on kessitek.com. It provides a great demonstration of how to properly position elements:

How to position a div on top of another div

I trust this information proves useful,

Zag,

Answer №5

How do you feel about this suggestion?

.close-image{
    display:block;
    cursor:pointer;
    z-index:3;
    position:absolute;
    top:0;
    right:0;
}

Does this meet your expectations?

Answer №6

To make the child element position properly within its parent, simply add position: relative to the parent and position: absolute along with setting top/bottom/left/right values on the child.

.main {
  width: 100%;
  height: 100%;
  position: relative;
}

.pink {
  width: 150px;
  height: 150px;
  background-color: pink;
  position: absolute;
  left: 50px;
  top: 50px;
}

.blue {
  width: 100%;
  height: 300px;
  background-color: lightblue;
}
<div class="main">

<div class="pink">
</div>

<div class="blue">
</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

Why does the last-of-type selector work, but the first-of-type doesn't seem to?

The SCSS code snippet below is causing me some confusion: li { &.menu-item-type-custom { margin-bottom: 10px; a { // } &:last-of-type { margin-bottom: 40px; } &:first-of-type { margin-top: 40px; ...

Tips for avoiding the freezing of bootstrap-select scroll when a large number of options are present

I have integrated a bootstrap-select with a total of 1000 options. However, I am encountering an issue where when I attempt to scroll down the list of options, it only goes down approximately 60 options and then freezes in that position. Can anyone provi ...

Discover an effective way to display Ajax search results above a different div element!

I am having trouble with positioning my search results on top of other divs. To see the issue, you can watch the screen recording by clicking on this link: https://drive.google.com/file/d/1kU_vl2ZAmSCok0pxnTTvY hacxqcZZ9m_/view?usp=sharing These are the s ...

Is there a way to adjust the speed of the transitions between animations?

I've been experimenting with ways to increase the time between animations in my cycle. Adjusting the duration of the keyframes animation hasn't yielded the desired effect. span { animation: rotateWordsFirst 15s linear infinite 0s; ...

Creating a dynamic menu structure by tailoring it to the specific elements found on each page

Currently, I am facing issues while attempting to generate a dynamic menu based on the elements present on the page. Is there a way to develop a menu using the following code structure?: <div class="parent"> <div class="one child" id="first"& ...

What is the best way to create a footer in this scenario and is there a method to perfectly center an

What is the best way to position the footer at the bottom of the page without overlapping the top content? Is there a method to center both the height and width of the header element on the page? Can you review the layout of this webpage and provide feed ...

Tips for identifying HTML tags that include a specific attribute automatically

Is there a way to automatically identify an HTML tag that contains a specific attribute like data-wow-delay? This query revolves around wow.js. The goal is to have the classes wow bounce added automatically to any HTML tag that has this particular attribu ...

Checking form entries and updating HTML

Prompt Description: In my endeavor to authenticate a <form> on the server side, I have opted to send form inputs as JSON to the back-end (an express.js app) using AJAX. Post-validation, another JSON is dispatched to the front-end with a structure ak ...

Utilizing the <figure> element in my HTML5 project

What is the best approach for achieving hover effects on both text and image areas in this case? <figure> <a href="#" class="portfolio-item"> <div class="picture"><img src="images/portfolio/recent-0 ...

What are the steps to incorporating ng2-dnd with a background-image?

I am currently utilizing the ng2-dnd module to enable sorting of a list. While the functionality for sorting and dragging is working smoothly, there's one issue - users can only drag by clicking on the text within my element. My goal is to allow user ...

Prevent the Esc key from closing panel in jQuery Mobile

As per the jQuery Mobile documentation: "If you click the link that opened the panel, swipe left or right, or tap the Esc key, the panel will close. By default, panels can also be closed by clicking outside of the panel onto the page contents." Visit t ...

The Lifecycle of an HTML Page

Trying to grasp the life cycle of an HTML page has been a challenge for me. Unable to find comprehensive resources online, I took matters into my own hands by experimenting with the f12 tool in Internet Explorer. Through these experiments, I have formulate ...

Instructions on how to assign a class number to a div matching the cookie number

Cookie: $(document).ready(function(){ //hide all divs for the purpose of this example, you should have them hidden with your CSS //$('.picture.1').hide(); //check if the cookie is already set if ($.cookie("currentDiv") === ...

Creating a flexible grid layout with DIVs that share equal width dimensions

Struggling with my HTML and CSS code, I need a way to structure nested DIV blocks into a responsive grid where each block has the same width. Despite my efforts, nothing seems to work as expected. Currently, the nested DIVs are stacked vertically one on to ...

Manage several scrolls using overflow:auto

There are numerous popups on a page with the overflow:auto property. The structure is as follows - <div id="popup1"> <div>SomeHTMLSTRUC</div> <div>SomeHTMLSTRUC</div> <ul class="scroll"></ul> </div> ...

Encountered an error saying "Unable to locate property '0' of undefined" while trying to access an environment variable in MongoDB

I am attempting to access a variable that is defined in my JavaScript code. This variable holds data fetched from a MongoDB using a GET request. The entire database is stored under this variable and then read by the HTML for display. The output of the GET ...

In Internet Explorer 7, the combination of having a hasLayout property along with setting the position to relative,

Issue: Encountering a challenge in IE7 with a div containing a gradient and a flyout menu on hover. Although it may seem simple, applying position:relative to the container for accurate menu positioning conflicts with the filter/hasLayout combination. Ch ...

Using Angular-Translate to Replace Variables in Title Tags

In my AngularJS project, I am using Angular-Translate version 2.6.1. One of the challenges I am facing is how to include a variable in a translated title attribute within a span element. <span title={{'translationID'|translate:'{username ...

What is the optimal method for delivering HTML content in a Node.js application using Express.js?

My current approach involves serving all HTML content directly from my app.js/server.js file, as shown below: app.get('/', function(req, res) { res.render('index.html'); }); app.get('/about', function(req, res) { res. ...

There seems to be an issue with the SQL syntax. Please refer to the corresponding manual for MySQL

An SQL error has occurred. Please refer to the MySQL server manual for the correct syntax near ' product_code, product_name, product_desc, price FROM products' at line 1 <?php include('include/conn.php'); $per_page = 5; $adjacen ...