Div是部分居中但无法动态居中

时间:2013-12-11 12:17:06

标签: html css

http://cathybrooks.freeiz.com/

所以我在一个容器中都有页眉和一个内容容器,我试图让它里面的所有东西都居中,当窗口调整大小时它会动态保持居中(包括推动投资组合显示框堆叠) 。我使用左/右属性来定位导航和页脚(主要问题),但这并不理想,因为它不是基于列表的最中间元素居中,而是通过第一个元素定位列表。如何根据列表的长度来定位页眉和页脚,而不是放置第一个元素?无论我添加多少元素,这都应该能够工作。谢谢!

<div class = "container">

        <ul class = "nav">
            <li><a href = "#">PORTFOLIO</a></li>
            <li><a href = "cv.html">CV</a></li>
            <li><a href = "contact.html">CONTACT</a></li>
        </ul>

        <a id = "name" href = "index.html">CATHERINEROSEART</a>

        <div id = "wrapper">

            <div id = "content">   

                <div class = "portfoliowrapper">
                    <div class = "left">
                        <a href = "#">
                            <div id = "box-1" class = "box">  
                                <img id = "image-1" src = "img/1.jpg"/>  
                                <div class = "caption fade-caption">  
                                    <h3>Piece 1</h3>  
                                    <p class = "captp">Piece 1</p>
                                </div>  
                            </div>
                        </a>
                    </div>
                    <div class = "middle">
                        <a href = "#">
                            <div id = "box-2" class = "box">  
                                <img id = "image-2" src = "img/1.jpg"/>  
                                <div class = "caption fade-caption">  
                                    <h3>Piece 2</h3>  
                                    <p class = "captp">Piece 2</p>  
                                </div>  
                            </div>
                        </a>
                    </div>
                    <div class = "right">
                        <a href = "#">
                            <div id = "box-2" class = "box">  
                                <img id = "image-2" src = "img/1.jpg"/>  
                                <div class = "caption fade-caption">  
                                    <h3>Piece 2</h3>  
                                    <p class = "captp">Piece 2</p>  
                                </div>  
                            </div>
                        </a>
                    </div>

                </div>

            </div>



            <div id = "footer">
                <ul class = "nav">
                    <li><a href = "#">Facebook</a></li>
                    <li><a href = "https://twitter.com/cathychYEAH" target = "_blank">Twitter</a></li>
                    <li><a href = "https://plus.google.com/u/0/+CatherineBrooks/posts" target = "_blank">Google+</a></li>
                    <li><a href = "http://uk.linkedin.com/pub/catherine-rose-brooks/61/7/563" target = "_blank">LinkedIn</a></li>
                    <li><a href = "http://catherineroseart.bigcartel.com/" target = "_blank">BigCartel</a></li>
                </ul>
                <p>© Catherine Brooks 2013 </p>
            </div>

        </div>

CSS:

.container {
    width: 1000px;
    margin: 0 auto;
    position: relative;
    top: 10%;
    text-align: center;
}

/* nav style */

.nav {
    margin: 0 auto;
    position: fixed;
    text-align: center;
    left: 0;
    right: 0;
}

.nav ul {
    list-style: none;
    width: 100%;
    margin: 0 auto;
    padding: 0; 
    display: inline;
    text-align: center;
    left: 0;
    right: 0;
}

.nav li {
    border-bottom: 1px solid #ccc;
    list-style: none; 
    display: inline-block;
    left: 0;
    right: 0;
}

.nav a {
    display: block;
    padding: 15px 25px 10px 25px;
    text-decoration: none;
    font-size: 12px;
    color: #C7C7C7;
    text-align: center;
    left: 0;
    right: 0;
}

.nav li a:hover {
    color: #A6A6A6;
}

/* end nav style */

/* content style */

#wrapper {
    background: white;
    color: black;
    width: 100%;
    float: left;
    margin: 0 auto;
    position: relative;
    border: 0;
    padding: 15px;
    display: inline-block;
    text-align: center;
}

.left, .middle, .right {
    width: 31%;
    margin: 0 auto; 
    padding: 10px;
    display: inline-block;
    text-align: center;
    left: 0;
    right: 0;
    float: left;
}

#name {
    font-family: 'Karla', sans-serif;
    color: #333333;
    display: block;
    text-align: center;
    padding-top: 20px;
    padding-bottom: 10px;
    font-size: 45px;
    letter-spacing: 5px;
    margin: 0 auto;
    left: 0;
    right: 0;    
}

a, #name {
    text-decoration: none;
    color: #333333;
}

a:visited {
    color: #333333;
}

#name {
    left: 0;
    right: 0;
    display: inline-block;
    vertical-align: middle;
}

#name:hover {
    color: #636363;
}

/* folio style */

.portfoliowrapper {  
    font: 10pt normal Arial, sans-serif;  
    height: auto;  
    margin: 0 auto;  
    text-align: center;  
    width: 100%;  
    display: inline-block;
    left: 0;
    right: 0;
}  

.portfoliowrapper .box {  
    border: 5px solid #fff;  
    cursor: pointer;  
    height: 250px;  
    left: 0;
    right: 0; 
    margin: 5px;  
    position: relative;  
    overflow: hidden;  
    width: 250px;  
    -webkit-box-shadow: 1px 1px 1px 1px #ccc;  
    -moz-box-shadow: 1px 1px 1px 1px #ccc;  
    box-shadow: 1px 1px 1px 1px #ccc;  
    display: inline-block;
}  

.portfoliowrapper .box img {  
    position: absolute;  
    left: 0;  
    -webkit-transition: all 300ms ease-out;  
    -moz-transition: all 300ms ease-out;  
    -o-transition: all 300ms ease-out;  
    -ms-transition: all 300ms ease-out;  
    transition: all 300ms ease-out;  
}  

.portfoliowrapper .box .caption {  
    transition-delay: 0.5s;
    background-color: rgba(0,0,0,0.8);  
    position: absolute;  
    color: #fff;  
    z-index: 100;  
    -webkit-transition: all 500ms ease-out;  
    -moz-transition: all 500ms ease-out;  
    -o-transition: all 500ms ease-out;  
    -ms-transition: all 500ms ease-out;  
    transition: all 500ms ease-out;  
    left: 0;  
}  

.portfoliowrapper .box .fade-caption, .portfoliowrapper .box .scale-caption {  
    opacity: 0;  
    width: 250px;  
    height: 250px;  
    text-align: left;  
    padding: 15px;  
}  

.portfoliowrapper .box:hover .fade-caption {  
    opacity: 1;  
}    

/* end folio style */

/* footer style */

#footer {
    bottom: 0;
    position: fixed;
    clear: both;
    left: 0;
    right: 0;
}

#footer p {
    text-align: center;
    bottom: 0px;
    color: #C7C7C7;
    font-size: 8px;
}

#footer .nav {
    bottom: 5%;
}

#footer .nav li a {
    color: #333333;
}

#footer .nav li a:hover {
    color: #A6A6A6;
}

2 个答案:

答案 0 :(得分:1)

你的元素偏离中心的原因是因为默认情况下<ul>有一些左边填充(通常是为子弹点腾出空间)。

添加.nav{padding:0},你应该都很好。

另请注意,您的.nav ul样式没有做任何事情。为了应用,选择器应该是ul.nav,没有空格。但是在这种情况下,它们不是必需的,实际上会损害您的布局。只需删除该块。

答案 1 :(得分:1)

试图清理一下:

HTML:

<div class = "header">
    <ul class = "nav">
        <li><a href = "#">PORTFOLIO</a></li>
        <li><a href = "cv.html">CV</a></li>
        <li><a href = "contact.html">CONTACT</a></li>
    </ul>
</div>

CSS - Body和NAV:

body{
    margin: 0;
    padding: 0;
}

.container {
    width: 100%;
    margin: 0 auto;
    position: relative;
    padding: 0;
    top: 10%;
    text-align: center;
}

/* nav style */
.header{
    margin: 0 auto;
    position: fixed;
    width: 100%;
    padding: 0;
}

.nav {
    margin: 0 auto;
    width: 100%;
    padding: 0;
}

.nav ul {
    list-style: none;
    margin: 0 auto;
    padding: 0; 
    width: 100%;
}

.nav li {
    border-bottom: 1px solid #ccc;
    padding: 15px 25px 10px 25px;
    font-size: 12px;
    list-style: none; 
    display: inline-block;
    text-align: center;
}

.nav a {
    text-decoration: none;
    color: #C7C7C7;
}

.nav li a:hover {
    color: #A6A6A6;
}

/* end nav style */

CSS - Wrapper(没有编辑单个图片框架等)

#wrapper {
    background: white;
    color: black;
    width: 100%;
    margin: 0 auto;
    display: inline-block;
    text-align: center;
}
.left, .middle, .right { /*Why not just 1 class*/
    width: 31%;
    margin: 0 auto; 
    padding: 10px;
    display: inline-block;
    text-align: center;
}

CSS - 页脚

/* footer style */

#footer {
    margin: 0 auto;
    position: fixed;
    width: 100%;
    padding: 0;
    bottom: 0;
    clear: both;
}

#footer p {
    padding: 5px; /*suggestion, since the copyright text changed from the original*/
    text-align: center;
    color: #C7C7C7;
    font-size: 8px;
}

#footer .nav li a {
    color: #333333;
}

#footer .nav li a:hover {
    color: #A6A6A6;
}