2011-12-14 87 views
0

我有一个父DIV的正方形,里面一个孩子的div它我想设置在另一个固定的宽度和高度

CSS

.parent { 
    background-color: red; 
    width: 500px; 
    height: 300px; 
    margin: 0 auto; 
} 
.child { 
    background-color: yellow; 
    width: 200px; 
    height: 100px; 
} 

现在我想设置的孩子正好在父母的中间没有改变宽度和高度,我该怎么做?谢谢。

回答

0

我明白自己,我只需要positi于:溢出;父母,然后用一些保证金的东西,我们可以把孩子放在中间。

0

这里有一个解决方案,我不知道它适合你的需求,但:

.parent { 
    background-color: red; 
    width: 500px; 
    height: 300px; 
    margin: 0 auto; 
} 

.child { 
    background-color: yellow; 
    width: 200px; 
    height: 100px; 
    /* changes */ 
    top: 100px; 
    position: relative; 
    margin: 0 auto; 
} 

Demo

+0

那么,基本上没有使用位置:相对,谢谢反正 – Sohail 2012-01-08 22:04:01

0

这似乎是你想要的东西,我认为

http://jsfiddle.net/kbPV7/7/

.parent { 
    background-color: red; 
    width: 500px; 
    height: 300px; 
    margin: 0 auto; 
    position:relative; 
    display:block; 

} 
.child { 
    background-color: yellow; 
    width: 200px; 
    height: 100px; 
    position:absolute; top:50%; margin-top:-50px; 
    left:50%; margin-left:-100px; 
}