【问题标题】:How to remove gap between navigation and the header? Also between navigation and main content?如何消除导航和标题之间的差距?也在导航和主要内容之间?
【发布时间】:2015-01-06 00:25:25
【问题描述】:

如何消除导航和标题之间的差距?还在导航和主要内容之间? 导航按钮,即 ul 处于完美位置。但是黑色的导航栏给我带来了很多不必要的麻烦!我无法修复它!

这是我的html

        <div id="header">
        <div id="header-container">
            Logo goes here!!
        </div>
    </div>

    <div class="nav">
        <div class="nav-container">
            <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">Portfolio</a></li>
                <li><a href="#">Browse</a></li>
                <li><a href="#">Contact</a></li>
                <li><a href="#">About</a></li>
            </ul>
        </div>
    </div>


    <div class="main-body">
        <h1>Contant here!</h1>
    </div>

这是我的css代码:

{
    margin: 0;
    padding: 0;
}

.clear{
    clear: both;
}


body{

    margin: 0 auto;
    background: #cccccc;
}

#header{
    background: #FF9900;
    height: 160px;
    width: 100%;
}

#header-container{
    width: 960px;
    height: 160px;
    margin: 0px auto 0px auto;
}

.nav{
    width: 100%;
    height: 70px;
    background: #000000;
    margin-top: 0px; 
    margin-bottom: 0px;
}

.nav .nav-container{
    width: 960px;
    height: 62px;
    margin: auto;
    margin-top: 5px;
}

.nav .nav-container ul{
    margin-top: 0px;
    padding-top: 0px;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 0px;
}

.nav .nav-container ul li{
    display: inline-block;
    padding: 0px;
    border-radius: 10px;
    margin-right: 20px;
    margin-left: 7px;
    background: #000000;
}

.nav .nav-container ul li a{
    color: #FF9900;
    text-decoration: none;
    font-weight: bold;
    font-size: 22px;
    display: inline-block;
    width:150px;
    text-align: center;
    height: 58px;
    line-height: 250%;

}

.nav .nav-container ul li a:hover{
    background:#FF9900;
    color: white;
    border-radius: 10px; 
}

.main-body{
    width: 960px;
    background: white;
    margin:auto;
    margin-top: 0px;
}

【问题讨论】:

    标签: header space nav


    【解决方案1】:

    .nav .nav-container 中的margin-top: 5px; 更改为margin-top: 0;

    .nav .nav-container{
        width: 960px;
        height: 62px;
        margin: auto;
        /* margin-top: 5px; becomes */ margin-top: 0;
    }
    

    演示:http://jsfiddle.net/ue65t3gh/

    【讨论】:

    • 没用!!我还添加了 margin:auto 以使内容居中!
    • @killerprince182 我添加了一个演示。什么“没用”?
    • 菜单项和标题之间没有空格。
    • 内容和导航之间也有空间
    • @killerprince182 太好了!请在可能的情况下接受答案。