Both position absolute and position relative are failing to work effectively

Whenever I click on a div called "test", another div named "outside" appears, along with a div named "inside" that has a higher z-index.

The problem arises when I try to set the position of "inside" to absolute, as I am unable to assign a margin-bottom. And if I set the position to relative, it places my "test" div below it.

It may be a bit challenging to grasp, but the issue is quite simple. Here's a fiddle for reference: http://jsfiddle.net/malamine_kebe/QRpqs/

Here are my CSS styles:

#insideAbsolute{
        background-color:#f8f8f8;
        position: absolute;
        top:0;
        left:20%;
        width:60%;
        margin-top:35px;
        margin-bottom:35px;
        z-index:3;
        border-radius: 7px;
        box-shadow: 6px 6px 20px black; 
    }

    #insideRelative{
        background-color:#f8f8f8;
        position: relative;
        top:0;
        left:20%;
        width:60%;
        margin-top:35px;
        margin-bottom:35px;
        z-index:3;
        border-radius: 7px;
        box-shadow: 6px 6px 20px black; 
    }

    #outside{
        position: fixed;
        left:0;
        top:0;
        height: 100%;
        width: 100%;
        background-color: black;
        opacity:0.7;
        z-index:2;
        background-attachment:fixed;
    }

    .test{
        z-index:1;
    }

My HTML structure:

<div id="outside"></div>
    <div id="insideAbsolute"></div>
    <div id="insideRelative"></div>

    <div class="testAbsolute">test position absolute</div>
    <div class="testRelative">test position relative</div>

And this is the jQuery code:

$('#outside').hide();
    $('#insideAbsolute').hide();
    $('#insideRelative').hide();

    $(document).on('click', '.testAbsolute', function () {
        $('#outside').show(0, function() { 
            $('#insideAbsolute').show(0, function() {
                $(this).html('<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>');
                $(document).on('click','#outside',function(){
                    $('#insideAbsolute').html('');
                    $('#outside').hide();   
                });     
            }); 
        });

    });  

    $(document).on('click', '.testRelative', function () {
        $('#outside').show(0, function() { 
            $('#insideRelative').show(0, function() {
                $(this).html('<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>');
                $(document).on('click','#outside',function(){
                    $('#insideRelative').html('');
                    $('#outside').hide();   
                });     
            }); 
        });

    });  
     

Answer №1

Try this neat little CSS trick:

#insideAbsolute:after{
    content:'';
    height : 35px;
    width : 100%;
    position : absolute;
    bottom : -35px;
}

This will create a simulated margin effect!

Check it out on Fiddle : http://jsfiddle.net/QRpqs/1/

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

How to Access a div from another website using jQuery

I have a question. How can I call a div from another website using JavaScript? Here is the page: Now, I want to call the <div id="testa"> in another page. The other page is called Otherpage.html: jQuery(function($){ $(&ap ...

What is the best way to place tfoot at the top of the table

Below is the structure of my table: https://i.stack.imgur.com/QmlVn.png In the table, there are search input fields at the bottom of each column. I would like to move them to the top, right after the <thead> </thead>. The basic HTML code for ...

Unable to send email through Ajax/PHP contact form

It's quite amusing that it worked perfectly for one evening. After reaching out to my host, they assured me that there should be no issues with it not working. I even tried testing it in Firebug, but it appeared to be sending successfully. Additionall ...

What is causing this console to output twice?

My Objective: I aim to utilize Node.js to launch two child processes sequentially at a specific time, displaying their `stdout` as it streams, occasionally alternating between the two processes. The Desired Output: `Proc 1 log # 1` `Proc 1 log # 2` `Pr ...

DiscordJS bot using Typescript experiences audio playback issues that halt after a short period of time

I am currently experiencing difficulties with playing audio through a discord bot that I created. The bot is designed to download a song from YouTube using ytdl-core and then play it, but for some reason, the song stops after a few seconds of playing. Bel ...

Troubleshooting: Bootstrap PopoverX functions failing

I've been attempting to make the bootstrap extension popover X function properly () I created a JS fiddle (http://jsfiddle.net/wp80d5ux/2/), but unfortunately, the popover doesn't appear to be loading. Here is the example I have: Could it be t ...

Employing Ajax for interacting with a database

I am attempting to query my database using a JavaScript function for the first time, and I am struggling to get it to work. While unsure if this is the correct approach, it is the one that I have come across. Below is a simple snippet of my HTML code: < ...

Issue with web form layout and heading malfunction

I'm experimenting with creating a form and dynamically setting the pattern and title fields using JavaScript. I am facing an issue with the current code as it is preventing me from entering anything into the form field, and displaying an error message ...

Develop content specific to different geographic locations for a web application using PHP

I have been working on a PHP-based online shopping website but have not had much success with it. example.com After trying multiple approaches without success, I have decided to implement an admin feature for adding locations. For instance, if I add "Mad ...

Using the double class within the .solution environment and adjusting the label of the .solution environment using CSS styling

Currently constructing a book with the use of quarto, where the inclusion of theorems, lemmas, corollaries, and other elements is necessary. In the quarto documentation, as seen here, it is advised to utilize the proof, remark, and solution environments fo ...

Challenges with Hover Dropdowns in Bootstrap Navigation Menu

Check out this screenshot of a navbar PSD design I'm aiming for: To make my navbar dropdown show up upon hovering, I've implemented the following JavaScript: $('.navbar .dropdown').hover(function() { $(this).find('.dropdown-men ...

Begin an HTML document by loading it with xml.Load

I am looking to access an HTML document stored as a string retrieved from a StreamReader on the web and then convert it into an XMLDocument. Currently, I am using the following code: XmlDocument doc = new XmlDocument doc.Load(string containing the retrie ...

Methods for passing JavaScript variables to PHP

I have encountered this problem on Stack Overflow before, but I couldn't find a solution that worked for me. I am using Codeigniter and have a form where users can rate a product. What I need to achieve is to insert the user's rating into the dat ...

swapping out an external CSS file in React for a new CSS file

My React app is quite large and includes four main CSS files (darkLTR, lightLTR, darkRTL, lightRTL), which may not be the most efficient setup. The templates were provided by my boss, and I was instructed to use them instead of Material UI, which I initial ...

Invoke the express function on the client using the callable method

When I'm listening on a local port with my browser, the following method will return Hello world. //Node app.get('/', (req,res)=>{ res.send('Hello world') }); I've successfully exported the app as a ca ...

I am having trouble retrieving the properties of "2d" objects using tiles[i-1], unlike standard objects in JavaScript

I've been working on constructing a random map generator by utilizing a grid composed of tiles within a canvas. In my process, I'm investigating the properties of each tile tiles[i] before handling tiles[i-1]. While this procedure seems to functi ...

how to choose the :after pseudo-element with jQuery

Below are the codes I have tried. When this popup appears, I want to be able to close the entire popbox using the close button. CSS code .bigdiv{ display:none; background-color:#efefef; box-shadow: 10px 10px 10px 100000px rgba(0, 0, 0, 0.4); ...

How to Implement Horizontal Sorting with jQuery Using Various HTML Elements

Here is the current setup I have: http://www.bootply.com/sfLr2AJ7EU The issue I am facing is with moving the image (id) boxes horizontally. While I have managed to make it work vertically, attempting to do so horizontally has been unsuccessful. The image ...

What is the best way to pass a Python string to a JavaScript script?

I attempted to utilize the JSON module for this task, but encountered errors in both my JavaScript script and Firebug. For example, I have a line that looks like this: {"fruit":{"apple":100}} When trying to send this to a JavaScript script called jquery. ...

Let the numerical tally commence once we arrive at said juncture

Our script is designed to count numbers, but the issue arises when the page is refreshed and the number count starts over. We want the count to start only when a user reaches that specific number or point. Css:- .office{padding-right: 5px; padding-left: ...