2013-04-12 30 views
6

我遇到了一個小問題,我想讓我的頁腳留在屏幕的底部position: absolute。但我的margin: auto把它放在屏幕中間不再工作。位置絕對值和保證金:自動

HTML:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'> 
     <link rel="shortcut icon" href="../IMAGES/favicon.ico"> 
     <title>TEST</title> 
     <link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'> 
     <link rel="stylesheet" href="../css/stylesheet.css"> 
    </head> 
    <body> 
     <div id="header"> 
      <div id="logo"> 
       <img src="../IMAGES/logo.png" /> 
      </div> 
      <div id="logotitel"> 
       Den Allerstrafste "Ful-Ambi" Live-Band van groot Antwerpen en omstreken! 
      </div> 
     </div> 
     <div id="nav"> 
      <div id="links"> 
       <a href="index.php"><div class="link">Home</div></a> 
       <a href="wie.php"><div class="link">Wie is wie</div></a> 
       <a href="fotos.php"><div class="link">Foto's</div></a> 
       <a href="repertoire.php"><div class="link">Repertoire</div></a> 
       <a href="links.php"><div class="link">Links</div></a> 
       <a href="contact.php"><div class="link">Contact</div></a> 
      </div> 
     </div> 
     <div class="clear"></div> 
     <div id="content"> 
      TEST 
     </div> 
     <div class="clear"></div> 
     <div id="footer"> 
      <div id="copy"> 
       Developed by Yoshi &copy vAntstAd 
      </div> 
     </div> 
    </body> 
</html> 

CSS:

/* PAGE LAYOUT */ 
html 
{ 
     padding: 0px; 
     margin: 0px; 
} 

body 
{ 
     background-image: url(../IMAGES/background.png); 
     padding: 0px; 
     margin: 0px; 
     color: white; 
     font-family: 'Source Sans Pro', serif, sans-serif; 
} 

.clear 
{ 
     clear: both; 
} 

/* HEADER */ 
#header 
{ 
     width: 1100px; 
     height: 150px; 
     background-color: #282828; 
     margin: auto; 
     border-bottom: solid; 
     border-color: red; 
} 

#logo 
{ 
     width: 283px; 
     height: 100px; 
     margin: auto; 
} 

#logotitel 
{ 
     width: 1100px; 
     height: 50px; 
     line-height: 50px; 
     text-align: center; 
     font-size: x-large; 
} 

/* NAV */ 
#nav 
{ 
     width: 1100px; 
     height: 50px; 
     margin-top: 25px; 
     margin-right: auto; 
     margin-left: auto; 
     margin-bottom: 25px; 
     background-color: red; 
} 

#links 
{ 
     width: 600px; 
     height: 50px; 
     margin: auto; 
} 

.link 
{ 
     width: 100px; 
     height: 50px; 
     line-height: 50px; 
     float: left; 
     text-align: center; 
     color: white; 
     text-decoration: none; 
} 

.link:hover 
{ 
     color: #282828; 
     text-decoration: underline; 
} 

/* CONTENT */ 

#content 
{ 
     width: 1100px; 
     height: auto; 
     margin: auto; 
     color: #282828; 
     position: relative; 
} 

/* FOOTER */ 

#footer 
{ 
     width: 1100PX; 
     height: 50px; 
     position: absolute; 
     bottom: 0; 
     margin: auto; 
     background-color: #282828; 
} 

#copy 
{ 
     width: auto; 
     float: right; 
     margin-right: 5px; 
     height: 50px; 
     line-height: 50px; 
} 
+1

你的問題有相當多的代碼。爲了更快地獲得更好的幫助,請發佈[SSCCE(鏈接)](http://sscce.org)。 – Doorknob

+0

anwnser可以在這裏找到:http://stackoverflow.com/questions/9998260/css-absolute-position-wont-work-with-margin-leftauto-margin-right-auto –

+0

可能的重複:http:// stackoverflow。 com/questions/9350775/set-position-absolute-and-margin – showdev

回答

12

你既然知道頁腳(1100px)的寬度,你可以做一個left:50%;margin-left:-550px使其居中。

例子:居中絕對定位的元素
http://jsfiddle.net/vdWQG/

因此,頁腳將成爲:

#footer 
{ 
    width: 1100PX; 
    height: 50px; 
    position: absolute; 
    bottom: 0; 
    left:50%;   /* Add this */ 
    margin-left:-550px; /* Add this (this is half of #footers width) */ 
    background-color: #282828; 
} 

如果你想要的元素來貼在頁面的底部,用戶向下滾動,使用position: fixed代替position:absolute

+0

但是,當頁面比屏幕更長時,它不起作用,它保持在它被渲染的位置上。第一。當我滾動時它不會停留在底部:( –

+0

@YoshiPeters使用'position:fixed'而不是'absolute'。更新回答 – Jace

+1

現在感謝它的工作,就像我想要的那樣! –

3

要在底部頁腳,水平居中,可以應用下面的CSS:

footer{ 
    width: 100%; 
    max-width: 600px; 
    position: fixed; 
    left: 0; 
    right: 0; 
    bottom: 0; 
    margin: 0 auto; 
} 

這將圍繞固定的元素,但也將保持它反應靈敏,當瀏覽器有它會縮水變得不比頁腳寬。

看到這個Fiddle爲例