2009-05-19 92 views
5

我想「集中」我的網頁的文字和內容。現在我不想將文本對齊到中心位置,我仍然需要左對齊,但我需要左右兩邊的大量邊距,這樣一切都看起來相對中心。你能告訴我的HTML/CSS來實現這一目標嗎?謝謝。網頁的中心內容

回答

2
#wrapper { 
    width: 800px; 
    margin: 0 auto; 
} 

<div id="wrapper"> 
    <p>This will appear in a centered container</p> 
</div> 
+0

在某些瀏覽器,如IE瀏覽器例如,這不起作用。你將不得不添加另一個div包裝#wrapper將文本對齊設置爲居中。在#wrapper中,您應該再次將文本對齊設置爲左側。 – 2009-05-19 16:35:10

+0

我從來沒有遇到過這個問題?我在每個網頁上都使用了這一點代碼。使用XHTML文檔類型。如果您看到:http://dev.korebogen.dk - 它集中在所有瀏覽器中。而CSS只是#wrapper {width:960px;保證金:0汽車; } – janhartmann 2009-05-19 17:40:53

+0

好吧 - 也許它不會在IE5中心 - 但地獄,誰使用它呢? :-) – janhartmann 2009-05-19 18:04:23

1

我相信this可能會幫助你。

0

CSS:

#container { 
    max-width: 800px; 
    margin: 0 auto; 
} 

而在HTML,包一切:

<div id='container'> 
... 
</div> 

(請注意,這個答案從MEEP的不同之處在於我使用max-width下面給出一個流動佈局800個像素,而他使用width給一個固定的佈局。)

0

嘗試

#div { 
    margin:0 auto 
}; 
11
<html> 
<head> 
<style type="text/css"> 

body { 
    text-align: center; /* Center in IE */ 
} 

#content { 
    text-align: left; /* reset text-align for IE */ 
    margin: 0 auto; /* Center in other browsers */ 
    width: 800px; 
} 

html { 
    overflow: -moz-scrollbars-vertical; /* Force vertical scrollbar in FF */ 
} 

</head> 
<body> 
<div id="content"> 

    content here 

</div> 
</body> 
</html> 

*更新:我加了一些CSS,它強制FF垂直滾動條按下面的一些意見。

0

有內,你把所有的內容div容器:

<html> 
<head> 
    <title>a sample</title> 
</head> 
<body> 
    <div id="container"> 
     <h1>this is it</h1> 
     <p>all content goes here</p> 
    </div> 
</body> 

然後添加一些CSS指定的容器div的寬度和邊距:

#container { 
    width: 750px; 
    margin: 0 auto; 
}