简体   繁体   中英

Fixed position div in middel

Created a div popup with position fixed. This div includes form element. If screen size is small then popup gets cut form bottom and we can scroll through browser stroller.

I added solution to keep it up from bottom

.popup {
    bottom: 15%;
    position: fixed;
}

But it comes in bottom and doesn't look in center if screen size is big.

I am looking for solution like popup should be always in center of page and if screen size is small then it should be up from bottom. Should not cut from bottom.

To center div you have use following code

.popup {
    width:200px;
    height:200px;
    position: fixed;
    left:0;
    right:0;
    top:0;
    bottom:0;
    margin:auto;
}

Setting left,right,top,bottom to 0 with margin auto will center div having position fixed or absolute.

div {
    background:#333;
    width:200px;
    height:200px;
    position:fixed;
    top:50%;
    left:50%;
    margin-left:-100px;
    margin-top:-100px;
}
.popup{
    width:80%;
    height:200px;
    position:fixed;
    top:10%;
    left:0;
    margin:0 auto;
}

This is my custom Responsive popup that i use most frequently. To get the div centered i use JQuery height and my script calculates the device window height and adjust popup accordingly.

As per me this is the most easiest and awesome way to create popups

I hope it might help you.

JSFiddle : Demo

HTML

<div class="popup"> 
    <span class="p_box">
        <span class="close">X</span>
        <h2 id="popup_head"> Popup Alert !</h2>
        <p> This is popup content. The dummy text is here.</p>
        Some more dummy text. 
    </span>
</div>
<div class="content">
    <h1>Hello There...</h1>
    <p>This is just a dummy text.</p>
    <p>This is just a dummy text.</p>
    <p>This is just a dummy text.</p>
    </br>
    <h2>Content</h2>
    <p>
        Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar. The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek, then
    </p>

    <button id="popup_bt">Popup</button>
</div>

CSS

body {
    margin:0px;
    width:100%;
}
.content {
    padding:55px;
    text-align:justify;
}
.popup {
    display:block;
    visibility:hidden;
    position:fixed;
    top:0px;
    left:0px;
    min-width:100%;
    min-height:100%;
    background:rgba(0, 0, 0, 0.7);
    z-index:9999;
    overflow:hidden;
    text-align:center;
}
@media(max-width: 1020px) {
    .p_box {
        width:30%;
    }
}
@media(max-width: 800px) {
    .p_box {
        width:40%;
    }
}
@media(max-width: 640px) {
    .p_box {
        width:60%;
    }
}
@media(max-width: 420px) {
    .p_box {
        width:80%;
    }
}
.p_box {
    position:relative;
    margin:0px auto;
    display:block;
    height:auto;
    width:25%;
    padding:20px;
    background:white;
    text-align:left;
}
.close {
    display:inline;
    position:absolute;
    right:0px;
    top:5px;
    margin-right:5px;
    background:#E32222;
    color:white;
    height:20px;
    width:20px;
    border-radius:5px;
    z-index:9999;
    cursor:pointer;
    text-align:center;
}
#popup_head {
    color:#E32222;
    text-align:center;
}

JQuery/JavaScript

$(document).ready(function(){

        var dh = window.innerHeight;
        var pbox_h = $(".p_box").innerHeight();
        var mid_scr = dh/2;
        var mid_box = pbox_h/2;
        var topPos = mid_scr - mid_box;
        $(".p_box").css("top","" + topPos + "px");

    $("#popup_bt").click(function(e){
        $(".popup").fadeIn();
        $(".popup").css("visibility","visible");
    });
    $(".close").click(function(event){
        $(".popup").fadeOut();
    });
});

Note : You could custom your popup box i had added some dummy content.

Centering things is a piece of cake:

.centereddiv {position:absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);}

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM