2012-04-13 63 views
2

我是我的新CSS ..我已经将我的psd文件转换为html/css ..现在我想将我的网页移动到窗口的中心..我尝试了一些代码并且该代码仅对齐文本的背景图像“back.gif”保持在左边..这里是我的HTML和CSS代码”如何在CSS中对齐网页

**HTML** 

<body> 
<div id="main"> 

     <div id="header"> 
    <div id="logo"> 
      <img src="logo.gif" /> 
     </div> 
     </div> 

    <div id="menu"> 
    <img src="images/menu_07.gif" /> 
      <div id="m"> 
      <ul> 
       <li> <a href="index.php">Home</a></li> 
       <li> <a href="pages.php">Pages</a></li> 
       <li> <a href="donor.php">Donor</a></li> 
       <li><a href="seeker.php">Seeker</a></li> 
       <li><a href="hospitals.php">Hospitals</a></li> 
       <li><a href="lifesaving.php">Life Saving Contacts</a></li> 
       <li><a href="contactus.php">Feedback</a></li> 
      </ul> 
      </div> 
    </div> 

    <div id="content"> 
    <div id="center" align="center"> 
      <h3>WELCOME TO ADMIN AREA</h3> 
      <p id="ajk">AJKBLOODBANK.COM</p> 
     <?php 
       echo "welcome!".$_SESSION['username']; 
       echo "<br>"; 
       echo "<a href=logout.php>Logout</a>&nbsp;"; 
      ?> 
     </div> 
    </div> 

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


**CSS** 
body 
{ 

    background:url(../back.gif) repeat-y; 
    padding-left:105px; 
    height:900px; 
    text-align: center; 
    margin:auto; 
    min-width: 760px; 
} 
div#main 
{ 
    width: 720px; 
    margin: 0 auto; 
    text-align: left; 
} 

#header 
{ 
    width:787px; 
    margin-top:10px; 
} 

#menu 
{ 

    width:787px;  
    float:left; 
    margin-top:20px; 

} 

#content 
{ 
    background-color:#FFF; 
    border:groove; 
    width:790px; 
    float:left; 
    padding-top:30px; 
    margin-top:30px; 
} 
#footer 
{ 
     float:left; 
     height:52px; 
     width:790px; 
} 

请告诉我在哪儿出错了..

the diameseion of background image "backgif" = 995x1000 

回答

5

无论你想要居中的元素 - 设置一个明确的宽度,然后设置左/右边距为auto

#main { width:995px; margin:0 auto; } 

...这会将元素居中在其父项中。如果你要居中#main,它会在页面上居中(假设你的body有100%的宽度)

编辑

尝试使你的背景图片适用于#main而不是body。或者,使用中心background-position,或简写背景图像:

body { background:url(../back.gif) repeat-y center top; } 

干杯

+0

我把这个代码,, – maham 2012-04-13 21:55:41

+0

'body { \t background:url(../ back.gif)repeat-y center top; \t} #main { \t background:url(../ back.gif)repeat-y; \t width:995px; \t margin:0 auto; \t}'这把背景图片移动到中心..但主要的div在左边.. @Madbreaks – maham 2012-04-13 21:57:02

+0

深吸一口气。为什么要在两个元素中放置背景图像?把它放在身体的元素。如果'#main'仍然在左边,请尝试添加'height:100%;宽度:100%;'到你的'身体'CSS。 – Madbreaks 2012-04-13 22:07:25

4

要居中的主体元素,我会用一些与此类似:

.body { 
    width: 940px; 
    margin-left: auto; 
    margin-right: auto; 
} 
+0

使用简写,'边距:0自动;'0为顶部和底部,自动为左和右。 – Whymarrh 2012-04-13 21:42:48

+1

@Whymarrh:不确定我明白你的观点。是的,还有其他的方式来写它。但是,您的快捷方式要求我也知道/设置顶部边距。我没有对顶部边缘进行假设。 – 2012-04-13 21:44:40

+0

@Jonathan ..我刚刚检查了你的代码..我的背景图像仍然是左边的 – maham 2012-04-13 21:44:57