2014-12-04 96 views
0

我是新來的CSS。我想保持div始終在屏幕的中心。我知道通過定位我們可以實現它,但只有當我們擁有固定的寬度和高度時。我的要求是什麼,我想要有固定寬度但沒有固定高度的div。我希望它根據其中的內容使用CSS進行調整。如何將div始終保持在固定的寬度和100%的高度?

+0

嗨,如果你能給我們一個jsFiddle玩一玩,我將不勝感激。 – w3shivers 2014-12-04 07:54:51

+0

是的,它是一個重複的問題:http://stackoverflow.com/questions/59309/how-to-vertically-center-content-with-variable-height-within-a-div – 2014-12-04 07:57:11

回答

0

您正在尋找transform: translateY(-50%)住宅。這裏有一個例子:

.main { 
    width: 200px; 
    position: fixed; 
    background-color: red; 
    position: fixed; 
    top: 50%; 
    left: 0px; 
    right: 0px; 
    transform: translateY(-50%); 
    margin: auto; 
} 

Working Fiddle

0

就做這樣的事情,你可以肯定,它將永遠是在中間。

HTML

<div id="outer"> 
<div id="content"></div> 
</div> 

CSS

* { 
    margin: 0 auto; /* centers everything except if otherwise floated 
    or affected by positioning positioning and margin property values */ 
} 


#outer { 
    width: 200px; 
    height: auto; 

} 


#content { 
    width: 200px; 
    height: 100%; 
    position: absolute; 
    background-color: green; 
    bottom: 0; 

} 

See working example here

解決方案:包裹在一個沒有問題的DIV浮動的div聽的第一個塊CSS代碼和我迪不要做任何會影響其立場的事情。

相關問題