2014-09-30 37 views
0

我怎樣才能垂直居中顯示窗體和旁邊的div?如果兩者的容器都小於內容高度空間,我需要能夠滾動。使用CSS中心兩個元素

HTML:

<div id="outer_1"> 
    <div id="inner_1"> 
    <form id="form"> 
     <input/> 
     <input/> 
    </form> 
    <div id="login-request"> 
     If you do not have a login, 
     <span>touch here</span> 
    </div> 
    </div> 
</div> 

CSS:

#outer_1 { 
    width: 400px; 
    height: 400px; 
    border-style: solid; 
    border-width: 2px; 
} 

#inner_1 { 
    width: 200px; 
    height: 100%; 
    display: table; 
    border-style: solid; 
    border-width: 2px; 
    border-color: red; 
    margin-left: auto; 
    margin-right: auto; 
} 

#form { 
    text-align: center; 
    vertical-align: middle; 
} 

#login-request { 
    text-align: center; 
    vertical-align: middle; 
} 

請考慮這個JSFiddle

+0

可能需要顯示:表細胞;回到顯示器上:inline-block; ? – 2014-09-30 12:38:24

回答

0

試試這個,形式和消息都是在水平和垂直居中對齊,看看它在 http://jsfiddle.net/4vtf5cn6/10/

<div id="outer_1"> 
    <div id="inner_1"> 
    <form id="form"> 
     <input/> 
     <input/> 
     <div id="login-request" class="login-request-alert"> 
     If you do not have a Travelport Mobile Agent login, 
     <span class="urlink">touch here</span> 
     </div> 
    </form> 
    </div> 
</div> 
#outer_1{ 
    width: 400px; 
    height: 400px; 
    border-style: solid; 
    border-width: 2px; 
} 
#inner_1{ 
    padding-top:30px; 
    width: 200px; 
    height: 100%; 
    display: table; 
    border-style: solid; 
    border-width: 2px; 
    border-color: red; 
    margin-left: auto; 
    margin-right: auto; 
} 
#form{ 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    float:left; 
} 
#login-request{ 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    float:left; 
} 
1

難道你不能只用這兩個div上的float:left來解決這個問題嗎?

的jsfiddle:http://jsfiddle.net/4vtf5cn6/6/

添加的行標有**,所以不只是複製下面的代碼;)

#outer_1{ 
    width: 400px; 
    height: 400px; 
    border-style: solid; 
    border-width: 2px; 

} 

#inner_1{ 
    **padding-top:30px; 
    width: 200px; 
    height: 100%; 
    display: table; 
    border-style: solid; 
    border-width: 2px; 
    border-color: red; 
    margin-left: auto; 
    margin-right: auto; 
} 

#form{ 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    **float:left; 
} 
#login-request{ 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    **float:left; 
} 

結果:

enter image description here

+0

輸入和登錄請求都不會出現在紅色正方形的中心 – mluis 2014-09-30 14:33:43

+0

他們對我有用,就像您在結果圖片中看到的那樣。 – Rvervuurt 2014-10-01 06:57:40

+0

紅色正方形的中心應該在水平和垂直的中心點 – mluis 2014-10-01 11:00:18

0

試這DEMO

#form{ 
    display: inline-block; 
    text-align: center; 
    vertical-align: middle; 
} 
#login-request{ 
    display: inline-block; 
    text-align: center; 
    vertical-align: middle; 
} 
+0

「inlinne」也在您的演示:) – Rvervuurt 2014-09-30 12:45:20

+0

更新演示)))) – 2014-09-30 12:47:30

+0

輸入和登錄請求不會出現在紅色正方形的中心 – mluis 2014-09-30 13:39:35

0

選項可以是CSS規則的margin-top屬性的百分比值。例如:

.container { 
 
    width: 150px; 
 
    height: 200px; 
 
    border: 3px solid orange; 
 
} 
 
.child { 
 
    /* Center horizontally */ 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    
 
    /* Center vertically */ 
 
    margin-top: 50%; 
 
    
 
    width: 30px; 
 
    height: 40px; 
 
    border: 3px solid yellowgreen; 
 
}
<div class="container"> 
 
    <div class="child"></div> 
 
</div>

0

最簡單的方法是添加一個<div>,其中包含#form#login-request。像:

<div id="outer_1"> 
    <div id="inner_1"> 
     <div id="enclose"> 
      <form id="form"> 
       <input/> 
       <input/> 
      </form> 
      <div id="login-request" > 
       If you do not have a login, 
       <span>touch here</span> 
      </div> 
     </div> 
    </div> 
</div> 

,並在CSS:

#enclose { 
    display: table-cell; 
    text-align: center; 
    vertical-align: middle; 
    overflow-y: auto; 
} 

和風格#form,當你需要它#login-request

+0

無法讓兩個div到apear垂直對齊。輸入和登錄請求仍然出現在其他水平旁邊 – mluis 2014-09-30 13:37:28

+0

給出#eclose的寬度,你需要。 將#form和#login-request添加到css:position:relative;寬度:100%; – lili 2014-10-01 06:49:50