2013-04-23 77 views
0

嗨我想爲我的網站製作維護頁面,但是當我嘗試使用margin: auto;將主內容div放在中間位置時,但它似乎仍然保留在頂部和唯一辦法把它拉下來是用<br />,但我寧願不要使用,因爲它看起來凌亂,所以我在想,如果你有任何想法,什麼是錯的主要內容div堅持頂部

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link href="style/style.css" rel="stylesheet" type="text/css" /> 
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'> 
</head> 

<body lang="en"> 
<div class="alert">Do you want advertsing space? Contact us: <b>[email protected]</b></div> 

<div class="topBar"> 
    <div class="topBarcontainer"> 
     <img class="logo" src="images/logo.png" alt="Chattrd home"> 
    </div> 
</div> 

<div id="navigationBar"> 
    <ul> 
    <li><a href="index.htm">Home</a></li> 
    <li><a href="aboutus.htm">About us</a></li> 
    <li><a href="contactus.htm">Contact us</a></li> 
    </ul> 
</div> 
<div id="mainContent"></div> 
</body> 
</html> 

CSS:

body { 
    background: #F7F7F7; 
    font-family:Tahoma, Geneva, sans-serif; 
    margin: 0; 
    padding: 0; 
} 
.alert { 
    font-size: 10px; 
    color: #FFF; 
    background: #1f1f1f; 
    height: 14px; 
    padding-left: 10px; 
    font-family: Arial; 
    white-space: nowrap 
} 
.topBar { 
    background: #06C; 
    height: 40px; 
} 
#navigationBar { 
    background: #1f1f1f; 
    height: 28px; 
    color: white; 
    font-family: 'Open Sans'; 
} 
.topBarcontainer{ 
    margin: auto; 
    width: 1000px; 
} 
.logo { 
    padding: 7px; 
    height: 24px; 
    width: 98px; 
} 
#navigationBar ul { 
    margin: 0; 
    padding: 3px; 
    list-style-type: none; 
    text-align: center; 
    } 

#navigationBar ul li { 
    display: inline; 
    } 

#navigationBar ul li a { 
    text-decoration: none; 
    padding: .2em 1em; 
    color: #fff; 
    } 

#navigationBar ul li a:hover { 
    color: #FFF; 
    background-color: #06C; 
    } 
#mainContent { 
    margin: auto; 
    width: 500px; 
    height: 200px; 
    border: 1px solid #D8D8D8; 
    border-radius: 5px; 
    background: #E6E6E6; 
} 
+0

CSS'保證金:auto'不垂直運行。 http://stackoverflow.com/questions/8620200/why-does-auto-attribute-for-margin-not-work-vertically-while-it-works-horizo​​ntal – showdev 2013-04-23 20:59:32

回答

0

垂直居中不起作用,因爲沒有任何東西居中 - body與默認的所有HTML元素一樣,只是容納其內容所需的高度。

因爲你的主要內容是固定大小的,你可以簡單地用絕對定位解決它:

#mainContent { 
    width: 500px; 
    height: 200px; 
    position:fixed; 
    left:50%; 
    top:50%; 
    margin:-100px 0 0 -250px; 
    border: 1px solid #D8D8D8; 
    border-radius: 5px; 
    background: #E6E6E6; 
} 

Fiddled for your pleasure.

+0

謝謝,尼爾斯! – user2279169 2013-04-23 21:05:45