user2461031
user2461031

Reputation: 513

Neither position absolute neither position relative is working well

When I'm clicking on a div named "test", a div named "outside" comes, also comes, with a higher z-index a div named "inside".

My issue is when I'm setting the postion to absolute to "inside" I can't attribut a margin-bottom. And when I'm setting the position to relative it puts my div "test" below it.

It might be a little be difficule to understand but the issue is really simple. Here a fiddle : http://jsfiddle.net/malamine_kebe/QRpqs/

my css are:

#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:

<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 my jQuery

$('#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><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><br><br><br><br><br><br>');
                $(document).on('click','#outside',function(){
                    $('#insideRelative').html('');
                    $('#outside').hide();   
                });     
            }); 
        });

    });  

Upvotes: 1

Views: 247

Answers (1)

Karl-Andr&#233; Gagnon
Karl-Andr&#233; Gagnon

Reputation: 33870

Here a little trick for you. Add this in your CSS :

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

That create a "fake" margin!

Fiddle : http://jsfiddle.net/QRpqs/1/

Upvotes: 1

Related Questions