【问题标题】:Weird space between container and navbar容器和导航栏之间的奇怪空间
【发布时间】:2012-03-29 23:37:53
【问题描述】:

好吧,最奇怪的事情发生了,我将导航栏与容器分开,现在应该没有空间,我尝试了所有设置 div 边距和填充的方法,我希望它干净而不必使用负边距,非常感谢任何帮助。

HTML

 <html>
   <head>
   <meta charset="utf-8">
   <title>Test</title>
   <link href="body.css" rel="stylesheet" type="text/css">
    <link href="navbar1.css" rel="stylesheet" type="text/css">
    </head>

    <body>
       <div id="navbar">
        <ul>
         <li>black</li>
         <li>blue</li>
         <li>green</li>
         <li>yellow</li>
         <li>haha</li>
         <li>good</li>
        </ul>
    </div><!-- End of navbar -->
    <div id="container">
    <div class="logo">
            <img src="" alt="sun" />
    </div><!-- End of logo -->

    <div id="header1">
            <span>Hello World</span>
    </div><!-- End of header -->

    <div class="logo">
            <img src="" alt="sun" />
    </div><!-- End of logo -->
    <div id="p1">
            <p> up </p>
    </div><!-- End of p1 -->
        </div><!-- End of container -->
    </body>
    </html>

CSS

#navbar /* navbar container */
{
    background-color:#999;
    border-bottom:solid 3px #333;
    margin:0px;
}
#navbar ul /* unordered lists withinin navbar container */
{
    width:1000px;
    text-align:center;
    margin-top:0px;
    margin-bottom:0px;
    list-style:none;
    margin-left:auto;
    margin-right:auto;
    padding:0px;
    font-family:"Times New Roman", Times, serif;
    font-size:16px;
    color:#9900FF;

}

#navbar li /* list items within navbar container */
{
    padding-left:20px;
    padding-right:20px;
    display:inline;
    border-right:solid 2px #33FFFF;
    border-left:solid 2px #33FFFF;
}

CSS #2

body /* this is to set the body element, but mostly used for removing margins. */
{
    margin:0px;
    background-color:#666666;
}

#container /* this is our overall content container. */
{
    width:1000px;
    margin-right:auto;
    margin-left:auto;
    background-color:#CCCCCC;
    border-left:groove 5px #444;
    border-right:groove 5px #444;
    border-bottom:groove 5px #444;
}

.logo img /* this is logo on the right side. */
{
    height:100px;
    width:200px;
    float:left;

}

#header1 /* This is the header directly between the logos */
{
    height:96px;
    width:592px;
    font-family:Georgia, "Times New Roman", Times, serif;
    font-size:48px;
    float:left;
    text-align:center;
    alignment-baseline:middle;
    border-left:dashed 4px #00F;
    border-right:dashed 4px #00F;
    border-bottom:dashed 4px #00F;
}

【问题讨论】:

  • 这可能会有所帮助;如果您在容器或徽标周围放置边框;空间消失了。

标签: html css whitespace containers navbar


【解决方案1】:

这很简单。你需要一个像“reset.css”这样的东西......它将所有默认的css设置为它应该是......但现在这将是一个简单的修复:

*
{
   margin:  0;
   padding: 0;
}

【讨论】:

    【解决方案2】:

    将此添加到您的 CSS 中,您应该没问题:

    #p1 p {
        margin:0;
    }
    

    jsFiddle example。 p1 div 中段落标签的默认边距是原因。

    【讨论】:

    • 非常感谢!不知道今天我的思绪在哪里,没有意识到我还没有尝试过。
    【解决方案3】:

    我已更新您的代码,请检查:-

    HTML

        <title>Test</title>
       <link href="body.css" rel="stylesheet" type="text/css">
        <link href="navbar1.css" rel="stylesheet" type="text/css">
        </head>
    
        <body>
           <div id="navbar">
            <ul>
             <li>black</li>
             <li>blue</li>
             <li>green</li>
             <li>yellow</li>
             <li>haha</li>
             <li>good</li>
            </ul>
        </div><!-- End of navbar -->
        <div id="container">
        <div class="logo">
                <img src="" alt="sun" />
        </div><!-- End of logo -->
    
        <div id="header1">
                <span>Hello World</span>
        </div><!-- End of header -->
    
        <div class="logo">
                <img src="" alt="sun" />
        </div><!-- End of logo -->
        <div id="p1">
                <p> up </p>
        </div><!-- End of p1 -->
            </div><!-- End of container -->
        </body>
        </html>
    

    CSS

    body /* this is to set the body element, but mostly used for removing margins. */
    {
        margin:0px;
        padding:0px;
        background-color:#666666;
    }
    
    #navbar /* navbar container */
    {
        background-color:#999;
        border-bottom:solid 3px #333;
        margin:0px;
    }
    #navbar ul /* unordered lists withinin navbar container */
    {
        width:1000px;
        text-align:center;
        margin-top:0px;
        margin-bottom:0px;
        list-style:none;
        margin-left:auto;
        margin-right:auto;
        padding:0px;
        font-family:"Times New Roman", Times, serif;
        font-size:16px;
        color:#9900FF;
    
    }
    
    #navbar li /* list items within navbar container */
    {
        padding-left:20px;
        padding-right:20px;
        display:inline;
        border-right:solid 2px #33FFFF;
        border-left:solid 2px #33FFFF;
    }
    
    #container /* this is our overall content container. */
    {
        width:1000px;
        margin:auto;
        background-color:#CCCCCC;
        border-left:groove 5px #444;
        border-right:groove 5px #444;
        border-bottom:groove 5px #444;
    }
    
    .logo img /* this is logo on the right side. */
    {
        height:100px;
        width:200px;
        float:left;
    
    }
    
    #header1 /* This is the header directly between the logos */
    {
        height:96px;
        width:592px;
        font-family:Georgia, "Times New Roman", Times, serif;
        font-size:48px;
        float:left;
        text-align:center;
        alignment-baseline:middle;
        border-left:dashed 4px #00F;
        border-right:dashed 4px #00F;
        border-bottom:dashed 4px #00F;
    }
    

    我添加了边距填充:0;在您的 CSS 中,您可以使用 reset.css 以便保存默认的额外边距填充......

    查看演示:- http://jsfiddle.net/6xuSp/35/

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2020-07-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多