2014-09-22 102 views
0

我在html中搞亂了,我希望這篇文章在該部分的中心是水平和垂直的。我在網上看過不同的論壇,沒有人在工作。這是代碼。 如何將一個容器在另一個容器內水平和垂直居中?

/* Style */ 
 

 
    #section { 
 
    \t width: 70%; 
 
    \t height: 800px; 
 
    \t background: #555; 
 
    \t margin-left: auto; 
 
    \t margin-right: auto; 
 
    \t margin-top: 50px; 
 
    \t margin-bottom: 50px; 
 
    } 
 
    
 
    #nav { 
 
    \t height: 100px; 
 
    } 
 
    
 
    #article { 
 
    \t width: 90%; 
 
    \t height: 400px; 
 
    \t background-color: #fff; 
 
    } 
 
    
 
    #footer { 
 
    \t width: 100%; 
 
    \t height: 200px; 
 
    \t background-color: #fff; 
 
    } 
 

 
/* Nav */ 
 
    
 
    body { 
 
     font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; 
 
     padding: 20px 50px 150px; 
 
     font-size: 13px; 
 
     text-align: center; 
 
     background: #E3CAA1; 
 
    } 
 
    
 
    ul { 
 
     text-align: left; 
 
     display: inline; 
 
     margin: 0; 
 
     padding: 15px 4px 17px 0; 
 
     list-style: none; 
 
     -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
     -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
     box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
    } 
 
    ul li { 
 
     font: bold 12px/18px sans-serif; 
 
     display: inline-block; 
 
     margin-right: -4px; 
 
     position: relative; 
 
     padding: 15px 20px; 
 
     background: #fff; 
 
     cursor: pointer; 
 
     -webkit-transition: all 0.2s; 
 
     -moz-transition: all 0.2s; 
 
     -ms-transition: all 0.2s; 
 
     -o-transition: all 0.2s; 
 
     transition: all 0.2s; 
 
    } 
 
    ul li:hover { 
 
     background: #555; 
 
     color: #fff; 
 
    } 
 
    ul li ul { 
 
     padding: 0; 
 
     position: absolute; 
 
     top: 48px; 
 
     left: 0; 
 
     width: 150px; 
 
     -webkit-box-shadow: none; 
 
     -moz-box-shadow: none; 
 
     box-shadow: none; 
 
     display: none; 
 
     opacity: 0; 
 
     visibility: hidden; 
 
     -webkit-transiton: opacity 0.2s; 
 
     -moz-transition: opacity 0.2s; 
 
     -ms-transition: opacity 0.2s; 
 
     -o-transition: opacity 0.2s; 
 
     -transition: opacity 0.2s; 
 
    } 
 
    ul li ul li { 
 
     background: #555; 
 
     display: block; 
 
     color: #fff; 
 
     text-shadow: 0 -1px 0 #000; 
 
    } 
 
    ul li ul li:hover { background: #666; } 
 
    ul li:hover ul { 
 
     display: block; 
 
     opacity: 1; 
 
     visibility: visible; 
 
    } 
 

 

 
HTML
<!DOCTYPE html> 
 
    <html> 
 
    \t <head> 
 
    \t \t <meta charset="UTF-8"> 
 
    \t \t <link rel="stylesheet" type="text/css" href="stylesheets/style.css"> 
 
    \t \t <link rel="stylesheet" type="text/css" href="stylesheets/nav.css"> 
 
    \t \t <title>Alec Grogan | Home</title> 
 
    \t </head> 
 
    \t <body> 
 
    \t \t <header id="header"> 
 
    \t \t \t <h1>Alec Grogan</h1> 
 
    \t \t </header> 
 
    \t \t <nav id="nav"> 
 
    \t \t \t <ul> 
 
    \t \t \t \t <li>Home</li> 
 
    \t \t \t \t <li>About</li> 
 
    \t \t \t \t <li>Resume</li> 
 
    \t \t \t \t <li>Portfolio 
 
    \t \t \t \t \t <ul> 
 
    \t \t \t \t \t \t <li>Web Design</li> 
 
    \t \t \t \t \t \t <li>Photoshop</li> 
 
    \t \t \t \t \t </ul> 
 
    \t \t \t \t </li> 
 
    \t \t \t \t <li>Blog</li> 
 
    \t \t \t \t <li>Contact</li> 
 
    \t \t \t </ul> 
 
    \t \t </nav> 
 
    \t \t <section id="section"> 
 
    \t \t \t <article id="article"> 
 
    \t \t \t </article> 
 
    \t \t </section> 
 
    \t \t <footer id="footer"> 
 
    \t \t </footer> 
 
    \t </body> 
 
    </html>

回答

0

要居中的水平和垂直的使用如下:

父容器本添加到樣式:

位置:相對;

要居中這個添加到樣式的容器:

保證金:汽車;位置:絕對; top:0;左:0;底部:0;右: 0;

結果如下:

#section { 
 
    width: 70%; 
 
    height: 800px; 
 
    background: #555; 
 
    margin-left: auto; 
 
    margin-right: auto; 
 
    margin-top: 50px; 
 
    margin-bottom: 50px; 
 
    position: relative; 
 
} 
 
#nav { 
 
    height: 100px; 
 
} 
 
#article { 
 
    width: 90%; 
 
    height: 400px; 
 
    margin: auto; 
 
    margin: auto; 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
    bottom: 0; 
 
    right: 0; 
 
    background-color: #fff; 
 
} 
 
#footer { 
 
    width: 100%; 
 
    height: 200px; 
 
    background-color: #fff; 
 
} 
 
body { 
 
    font-family:'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif; 
 
    padding: 20px 50px 150px; 
 
    font-size: 13px; 
 
    text-align: center; 
 
    background: #E3CAA1; 
 
} 
 
ul { 
 
    text-align: left; 
 
    display: inline; 
 
    margin: 0; 
 
    padding: 15px 4px 17px 0; 
 
    list-style: none; 
 
    -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
    -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.15); 
 
} 
 
ul li { 
 
    font: bold 12px/18px sans-serif; 
 
    display: inline-block; 
 
    margin-right: -4px; 
 
    position: relative; 
 
    padding: 15px 20px; 
 
    background: #fff; 
 
    cursor: pointer; 
 
    -webkit-transition: all 0.2s; 
 
    -moz-transition: all 0.2s; 
 
    -ms-transition: all 0.2s; 
 
    -o-transition: all 0.2s; 
 
    transition: all 0.2s; 
 
} 
 
ul li:hover { 
 
    background: #555; 
 
    color: #fff; 
 
} 
 
ul li ul { 
 
    padding: 0; 
 
    position: absolute; 
 
    top: 48px; 
 
    left: 0; 
 
    width: 150px; 
 
    -webkit-box-shadow: none; 
 
    -moz-box-shadow: none; 
 
    box-shadow: none; 
 
    display: none; 
 
    opacity: 0; 
 
    visibility: hidden; 
 
    -webkit-transiton: opacity 0.2s; 
 
    -moz-transition: opacity 0.2s; 
 
    -ms-transition: opacity 0.2s; 
 
    -o-transition: opacity 0.2s; 
 
    -transition: opacity 0.2s; 
 
} 
 
ul li ul li { 
 
    background: #555; 
 
    display: block; 
 
    color: #fff; 
 
    text-shadow: 0 -1px 0 #000; 
 
} 
 
ul li ul li:hover { 
 
    background: #666; 
 
} 
 
ul li:hover ul { 
 
    display: block; 
 
    opacity: 1; 
 
    visibility: visible; 
 
}
<header id="header"> 
 
    <h1>Alec Grogan</h1> 
 

 
</header> 
 
<nav id="nav"> 
 
    <ul> 
 
     <li>Home</li> 
 
     <li>About</li> 
 
     <li>Resume</li> 
 
     <li>Portfolio 
 
      <ul> 
 
       <li>Web Design</li> 
 
       <li>Photoshop</li> 
 
      </ul> 
 
     </li> 
 
     <li>Blog</li> 
 
     <li>Contact</li> 
 
    </ul> 
 
</nav> 
 
<section id="section"> 
 
    <article id="article"></article> 
 
</section> 
 
<footer id="footer"></footer>

0

試着用下面的方法更新你的css來實現!

的CSS:

article{ 
display:table-cell; 
vertical-align: middle; 
} 
相關問題