2016-07-06 95 views
1

我想中心包含一個窗體的DIV。我已經設法使背景變灰,並希望將窗體置於窗口中央。以下是我迄今爲止所做的,但我不知道如何進一步取得我需要的結果。如何居中DIV彈出窗口?

我能夠'自動保證金'水平,但我不能夠垂直做到這一點(請參閱圖像)。如果您進一步垂直拉伸瀏覽器窗口,表單會拉伸以佔據所有的垂直空間。

enter image description here

#idOfForm{ 
margin: auto; 
z-index: 10000; 
position: absolute; 
background-color: white; 
top: 0; 
bottom: 0; 
left: 0; 
right: 0; 
padding: 10px; 
width: 500px; 
min-height: 250px; 
border: 1px solid #ccc; 
border-radius: 10px; 
box-shadow: 3px 3px 3px #b8b8b8; 
font-family: sans-serif; 
color: #484848; 
} 

回答

2

這是如何您可以輕鬆地居中元素:

假設你有以下幾點:

<div class="aaa"> 
    fsdfsd 
</div> 

使用下面的CSS:

.aaa { 
    transform: translate3d(-50%, -50%, 0); 
    position: absolute; 
    top: 50%; 
    left: 50%; 
} 

這裏的jsfiddle:https://jsfiddle.net/ffnvjz4q/
這是你需要的代碼:

#idOfForm{ 
    transform: translate3d(-50%, -50%, 0); 
    z-index: 10000; 
    position: absolute; 
    background-color: white; 
    top: 50%; 
    bottom: 0; 
    left: 50%; 
    right: 0; 
    padding: 10px; 
    width: 500px; 
    min-height: 250px; 
    border: 1px solid #ccc; 
    border-radius: 10px; 
    box-shadow: 3px 3px 3px #b8b8b8; 
    font-family: sans-serif; 
    color: #484848; 
} 
1
<div class="modal"> 
<div class="modal-body"> 
    <!-----put your form code here ---> 
</div> 
</div> 


.modal { 
position: fixed; 
top: 0; 
bottom: 0; 
left: 0; 
width: 100%; 
height: 100%; 
background-color: rgba(0,0,0,0.4); 
z-index: 2; 
} 

.modal-body { 
position: relative; 
max-width: 500px; 
width: 100%; 
margin: 50px auto 20px; 
background-color: #fff; 
min-height: 50px; 
}