2012-08-28 48 views
2

我需要將內容塊中心對齊,而菜單塊必須「連接」到內容塊的左側。所以這些塊之間的距離在瀏覽器窗口上升時應該保持不變。你能告訴我如何實施這個嗎? :)連接到中心對齊內容塊一側的菜單塊

這裏想什麼我一些樣品圖片來實現:
Browser window is maximized
Browser window was made small
Browser window was made smaller, and scrollbar appeared

+3

不,我不能看到任何圖片 –

+1

@Mayank swami oops ...現在怎麼樣? – Matvienko

+0

難道你不能用4列進行佈局,其中16列,16%,33%和33%寬,第二和第三列是菜單和內容? – JohnB

回答

1

哎呦我錯過了「不斷提升」位,更新瞭解決問題的例子。

這是你在找什麼?

http://jsfiddle.net/r8YQc/1/

HTML:

<div id="header"></div> 
<div id="content"> 
    <div id="menu"></div> 
</div> 
<div id="footer"></div> 
​ 

CSS:

html, 
body{ 
    margin:0; 
    padding:0; 
} 

#header{ 
    margin:10px; 
    background-color:orange; 
    height:50px; 
} 

#content{ 
    position:relative; /*Create new offset context*/ 
    display:block; 
    width:300px; /*Define width*/ 
    margin: 0 auto; /*center horizontally in available space*/ 
    height:400px; 
    background-color:green; 
} 

#menu{ 
    background-color:lightgreen; 
    position:absolute; /*Use left/right/top/bottom in relation to #content's offset context*/ 
    right:310px; /*move the right edge of the element 310px left*/ 
    width:100px; 
    height:200px; 
} 

#footer{ 
    background-color: blue; 
    margin: 10px; 
    height: 50px; 
} 

附:

如果向body元素添加最小寬度爲540px(300px內容寬度+ 4 * 10px頁邊距+左右爲100px的菜單和空白空白頁),則調整大小時也不會裁剪佈局小。

+0

這是一個相當不錯的解決方案,尤其是當設置了身體的最小寬度時,但只有一個小東西......問題與最小寬度相關,當瀏覽器窗口達到540px的寬度時,塊停止移動,滾動條出現,內容塊右側留有空白空白。只有當瀏覽器的右邊界到達內容塊的右側時,纔有辦法使滾動條出現? :) – Matvienko

+0

如果我的解釋不夠清楚,我已經在主帖中添加了一些示例圖片以幫助:) – Matvienko

0

我不能看到你的照片......但你的描述出現明顯不夠:

HTML

<div id="contentBlock"> 
    <ul> 
     <li>Item</li> 
     <li>Item</li> 
     <li>Item</li> 
     <li>Item</li> 
    </ul> 
</div> 

CSS

#contentBlock { 
    width: 500px; 
    display: block; 
    margin: 0 auto; 
} 

#contentBlock ul { 
    /* You really don't need anything in here because it should be left aligned in the first place */ 
} 

但是如果你想爲文本和其他元素的contentBlock內環繞式的菜單,然後我建議以下CSS補救措施:

#contentBlock { 
    width: 500px; 
    display: block; 
    margin: 0 auto; 
    overflow: hidden; /* This is important, it clears a heights on the contentBlock and allows the creation of floated children to be taken out of the DOM */ 
} 

#contentBlock ul { 
    float: left; 
    /* Again... the menu's text should by default be left-aligned here */ 
} 
0

1使您的網站中心佈局...因此,您可以查看每個解決方案的好,因爲您的網站看起來不像一個垂直的設計。

對畫面進行佈局,你需要做的

1,HTML

<div id="header_wrapper"> 
    <div id="header"></div> 
</div> 
<div id="wrapper"> 
    <div id="menu"></div> 
    <div id="content"> 

    </div> 
</div> 
<div id="footer"></div> 
​ 

2,CSS爲header_wrapper

#header_wrapper{ 
    width:100%; //so you can set the background to the header 
} 

3,CSS的頭

#header{ 
    max-width:1200px; 
    min-width:1000px; 
} 

4,現在做一個包裝,這將有菜單和內容

CSS的內容

#wrapper{ 
    margin:0 auto; //make it on the center of the page 
    width:1000px; 
    display:block; 
    } 

5現在添加菜單和內容包裝

CSS菜單

#menu{ 
width:100px; 
height:200px; 
float:left; 
} 

6,現在的內容

#content{ 
    width:300px; /*Define width*/ 
    float:left; 
} 
+0

這樣,內容+菜單組將以中心對齊,但不僅僅是內容塊,我需要的。 – Matvienko

+0

@Matvienko現在檢查了這是有一個錯誤的打字g –

+0

現在的內容不居中 – Matvienko