2012-03-26 89 views
-3

我想在右邊的左上角像一個div內放置一個div:如何在div中相對定位div?

enter image description here

,我想在像中心的專區內放置一個div:

enter image description here

這怎麼可能以最簡單的方式完成? :o

+2

爲什麼這被標記? – foreyez 2012-03-26 17:54:42

+2

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – 2012-03-26 18:00:16

+1

@foreyez它被低估了,因爲這個Q可以用一點谷歌搜索來回答。 – JKirchartz 2012-03-26 18:00:54

回答

0
#div1 { 
    position: relative; 
    top: 0; 
    left: 100%; 
} 

#div2 { 
    width: 100; 
    height: 100; 
    margin: auto auto; 
} 
0

的左上角使用此:

.ContainingDiv {position: relative;} 
.InsideDiv {position: absolute; top: 0px; right: 0px;} 

爲中心,做下:

.InsideDivCenter {margin: 0px auto;} 
2

頂/右內部元素:http://jsfiddle.net/ZAKAz/1/

.outer { height:200px; width:200px; position:relative; } 

.inner { position:absolute; top:0; right:0; width:50px; height:50px } 

集中內部元件:http://jsfiddle.net/ZAKAz/

.outer { height:200px; width:200px; position:relative; } 

.inner { position:absolute; top:50%; left:50%; 
     margin: -25px 0 0 -25px; width:50px; height:50px } 
1

HTML:

<div class="outside"> 
    <div class="inside top_right"> 
    </div>   
</div> 
<div class="outside"> 
    <div class="inside center"> 
    </div> 
</div>​ 

CSS:

.outside {position:relative;width:200px;height:200px;border:2px solid black} 
.inside {position:absolute;width:50px;height:50px;border:2px solid blue} 

.top_right {top:0;right:0;} 

.center {top:50%;left:50%;margin:-25px 0 0 -25px} 

的容器具有position:relative,以使該顯示器在那裏的意在頁面上,並允許position:absolute到容器的範圍內正常工作。

中心的CSS有把在div在50%從top & left,然後使用負的margins一半divwidth/height到拉入中心。

我認爲右上角幾乎不言自明。

演示:http://jsfiddle.net/JKirchartz/BK7qr/

0

你的第一個問題很簡單,只要:div.foo {浮動:權利; }

或者你可以valign = center ...但是上面的答案更徹底這只是我會先嚐試的方式......至少對於你想在div中間的那個。

注意:valign被棄用,贊成使用css屬性vertical-align。所以你必須用垂直對齊方式來做。對不起,我忘了。我是一個有點老派,從桌子佈局出局:)