2011-12-14 148 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; 
}