2013-04-13 128 views
0

目標是將此佈局居中在屏幕上。我有600px的最小寬度和1200px的最大寬度,因此佈局周圍可能會有很多空白區域。帶顯示的中心固定寬度/液體css佈局:表格屬性和最小/最大寬度

我一直在嘗試很多不同的方法,但對display:table屬性以及它在這種情況下的表現方式感到困惑。

這裏是的jsfiddle和代碼:http://jsfiddle.net/7M9rg/6/

非常感謝有這方面的建議!

HTML:

<div id="wrapper"> 
<div id="header"> 
</div> 

<div id="main"> 

<div id="side"> 
<div id="side-stuff"> 
<ul> 
<li><a href="../English/index.html">Home</a></li> 
</ul> 
</div> 
</div> 

<div id="content"> 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has</p> 
</div> 
</div> 

<div id="footer">&copy; 2013 </div> 
</div> 

CSS:

/*css reset*/ 
html,body {position:relative;margin:0;padding:0;min-height:100%;width:100%; 
height:100%;} 
div,p,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input, 
textarea,p,blockquote,th,td, figure {margin:0;padding:0;} 
ol,ul {list-style:none;} 
li {list-style-type: none;} 
* { -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: 
border-box; } 


html, body { 
font-family: Helvetica; 
height: 100%; /*important for equal height columns*/ 
min-width: 650px; 
} 

#wrapper{ 
height: 100%; /*important for equal height columns*/ 
padding-bottom:130px; /*This must equal the height of your header*/} 

#header{ 
height: 130px; /*This must equal padding bottom of wrap*/ 
display:block; 
padding: 5px; 
color: #fff; 
border: thin solid #ebebeb; 
border-radius: 10px; 
margin: 10px; 
background-image: url(Images/gradient.png); 
background-repeat: repeat-x;  
width: 99%;} 

#main { 
position: relative; 
height: 100%; /*important for equal height columns*/ 
width: 99%; 
overflow:auto; 
display: table; /* This is needed fo children elements using display table cell*/ 
table-layout: fixed; 
padding-bottom: 50px; /*This needs to match footer height*/ 
overflow: auto; 
margin-left: 10px;} 

#side{ 
background-color: #fff; 
width: 150px; 
margin: 10px; 
vertical-align: top; 
padding-top: 20px; 
padding-right: 10px; 
display: table-cell; 
border-radius: 10px; 
border: thin solid #CCC;} 

#side-stuff{ 
display: block; 
padding-left: 10px;} 

#content{ 
background-color: #fff; 
padding: 10px; 
display: table-cell; /*To make sibling columns equal in height*/ 
margin-bottom:10px; 
border-radius: 10px; 
border: thin solid #CCC;} 

#content-stuff{ 
width: auto; 
height: auto;} 

#footer{ 
position: relative; 
height: 40px; 
margin-top: -40px; /* margin-top is negative value of height */ 
margin-left: 10px; 
clear: both; /* Use if floating elements */ 
color: #999; 
width: 99%; 
border: thin solid #ebebeb; 
border-radius: 10px; 
background-image: url(Images/footer_gradient.png); 
background-repeat: repeat-x; 
background-position: bottom;} 

回答

1

添加到您的#wrapper指定CSS:

max-width: 1200px; 
margin: 0 auto; 

http://jsfiddle.net/tshz6/

+0

感謝這麼多的答覆。是的,我首先做到了這一點,但對我來說卻行不通。我很難過,它似乎工作在jsFiddle,但不是當我在瀏覽器中測試它。嗯... – user2275661

0

你需要爲指定寬度或#wrapper元素。 min-width只指定了最小值寬度可以縮小,而widthmax-width設置爲auto,這是所有可用的寬度(在這種情況下是屏幕寬度)。 嘗試更新html, body規則如下

html, body { 
    font-family: Helvetica; 
    height: 100%; 
    margin: 0 auto; 
    min-width: 650px; 
    max-width: 1200px; 
} 
+0

感謝您的快速回復。我已經嘗試設置您的示例中指定的邊距,但由於某種原因,這並不是這樣做的。我不確定我是否理解你對於指定寬度的評論(我不能用半液體佈局來做);我應該做別的事嗎? – user2275661

+0

'我有600px的最小寬度和1200px的最大寬度,所以佈局周圍可能會有很多空白區域。'如果我正確理解了這一點,則希望頁面的寬度在650px和1200px之間,具體取決於用戶的屏幕分辨率。答案中的CSS設置'max-width',以便它不會比'1200px'和'margin:0 auto'居中。嘗試這個jsfiddle http://jsfiddle.net/7M9rg/8/ – Ejaz

+0

順便說一句,嘗試更新您的HTML,身體規則以下'我的意思是替換_reset rules_ – Ejaz

相關問題