2014-12-04 100 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代码和我迪不要做任何会影响其立场的事情。

相关问题