2012-07-09 61 views
0

我的頁面如下所示。
風格:CSS中心的div並排排列

.featuredcontainer { 

width: 450px; 
height: 700px; 
margin-left: auto; 
margin-right: auto; 
position: relative; 
right: 160px; 
top: 30px; 
border: 1px groove grey; 
} 



.navcontainer 
{ 
margin-left: auto; 
margin-right: -8px; 
position: relative; 
top: 75px; 
height: 600px; 
width: 300px; 
} 

而例如HTML:

<div class="featuredcontainer"> 
content 
</div> 
<div class="lessonnavcontainer"> 
menu 
</div> 

當顯示的頁面。 navcontainer在右側(正如它應該),但在featuredcontainer下。當我使用相對定位將navcontainer向上移動時,它看起來很正確,但頁面底部有一堆空的空間。我該怎麼辦?

+0

嘗試在兩個類中添加'float:left' – 2012-07-09 18:03:18

+0

現在它們彼此相鄰,但不會隨頁面大小調整並位於混亂的位置。 – Jaxkr 2012-07-09 18:04:56

+0

刪除所有'頂部'和'右',並適合父div內的那兩個div

#parent {width:750px; margin:0px auto 0px auto; } – 2012-07-09 18:08:31

回答

0

環繞你的兩個div與 「包裝」 的div像這樣:

<div id="wrapper"> 
    <div class="featuredcontainer">content</div> 
    <div class="lessonnavcontainer">menu</div> 
</div> 

然後到中心他們,加邊距包裝:

#wrapper { margin: 0px auto; } 

然後有兩個div並排,加浮動:

.featuredcontainer { float: left; } 
.lessonavcontainer { float: left; } 

爲了使中心工作,你需要聲明的包裝寬度:

#wrapper { width: 800px; } 
+0

使用包裝div打破了粘貼在頁面右側的菜單。 http://jaxtests.comule.com/ – Jaxkr 2012-07-09 21:47:56

0

使用float屬性。使用float,css可以將div彼此水平放置在一起。

.featuredcontainer { 

width: 450px; 
height: 700px; 
margin-left: auto; 
margin-right: auto; 
position: relative; 
right: 160px; 
top: 30px; 
border: 1px groove grey; 
float: left; 
} 



.navcontainer 
{ 
margin-left: auto; 
margin-right: -8px; 
position: relative; 
top: 75px; 
height: 600px; 
width: 300px; 
float: left; 
} 

這是一個起點,嘗試使用float left或float right並查看會發生什麼。搗鼓它,直到它看起來完全如何你想要它。

+0

謝謝。但是,當我應用它時,它們不會在頁面調整大小時移動。 – Jaxkr 2012-07-09 18:08:17

+0

謝謝。這是我的頁面。 http://jaxtests.comule.com/ 請注意,右側的菜單粘在屏幕的右側?我需要保留這一點,並將其移至featurecontainer的位置,然後將其打散。 – Jaxkr 2012-07-09 21:47:07

+0

由於你希望最終產品看起來像什麼而感到困惑。你想看起來有什麼不同? – ryno 2012-07-09 22:43:39

0

爲了讓它們並排,你需要在CSS中添加float屬性。要讓它們調整頁面寬度,你需要爲它們添加相對寬度。要將它們居中放置在頁面上(水平),您需要將div放在html中相對定位的div內。這是一個小提琴。 http://jsfiddle.net/Ne5zs/

+0

謝謝。這是我的頁面。 http://jaxtests.comule.com/ 請注意,右側的菜單粘在屏幕的右側?我需要保留這一點,並使用float打破它。 – Jaxkr 2012-07-09 21:46:37

+0

@Jaxkr你需要右鍵菜單留在屏幕的右側或包含包裝div的右側? – smilebomb 2012-07-10 15:10:36

0

將導航和精選容器放入另一個包裝div。

HTML

<div class='wrapper'> 
    <div class="navcontainer"> 
     menu 
    </div> 
    <div class="featuredcontainer"> 
     content 
    </div> 
</div> 

,擺脫所有的相對定位。相對定位不推薦用於這樣的基本佈局問題。改爲使用花車。包裝應該有一個固定的寬度,這使得它可以用margin:0自動適當居中。

CSS

.wrapper{ 
    width:752px; 
    margin: 0 auto; 
    overflow:auto; 
} 
.featuredcontainer { 
    width: 450px; 
    height: 700px; 
    float:left; 
    border: 1px groove grey; 
} 
.navcontainer{ 
    float:left; 
    height: 600px; 
    width: 300px; 
    background:#ff0; 
} 

的jsfiddlehttp://jsfiddle.net/5w5SC/

+0

謝謝。這是我的頁面。 http://jaxtests.comule.com/ 請注意,右側的菜單粘在屏幕的右側?我需要保留這一點,並將其移至featurecontainer所在的位置。如果使用定位將其向上移動,則會在頁面底部留下一堆空白區域。 – Jaxkr 2012-07-09 21:45:36

0

一定要在任何浮動對象上引入一個清零(there aremanyversions ofthis technique);然後使用margin: 0 auto將它們的包含塊元素居中。

+0

謝謝。這是我的頁面。 http://jaxtests.comule.com/ 請注意,右側的菜單粘在屏幕的右側?我需要保留這一點,並將其移至featurecontainer所在的位置。如果使用定位將其向上移動,則會在頁面底部留下一堆空白區域。 – Jaxkr 2012-07-09 21:45:56