【问题标题】:Cant remove padding between nav-bar and banner css无法删除导航栏和横幅 css 之间的填充
【发布时间】:2018-11-11 22:42:19
【问题描述】:

我在创建网站时遇到了一个小问题,在我的横幅和导航栏之间出现匿名填充:

这是我的代码:

<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Untitled Document</title>
  <style>
    @import 'https://fonts.googleapis.com/css?family=Work+Sans:400,500,900';
    nav {
      padding-top: 0px;
      position: fixed;
      width: 100%;
      transition: top 0.2s ease-out;
    }
    
    .banner {
      text-align: center;
      width: 100%;
      box-shadow: inset 0px -1px 0px 0px rgba(0, 0, 0, 0.13);
    }
    
    nav ul#menu {
      padding-left: 0;
      display: flex;
      padding-top: 0px;
    }
    
    nav ul li {
      flex-grow: 1;
    }
    
    .nav-bar {
      /* Rectangle 1: */
      background: #FFFFFF;
      box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.11), 0px 4px 6px 0px rgba(0, 0, 0, 0.11);
      width: 100%;
      text-align: center;
    }
    
    //-------------------------------------------------------
    /*Strip the ul of padding and list styling*/
    
    ul {
      list-style-type: none;
      margin: 0;
      position: absolute;
    }
    /*Create a horizontal list with spacing*/
    
    li {
      display: inline-block;
      float: center;
      margin-right: 1px;
    }
    /*Style for menu links*/
    
    li a {
      display: block;
      min-width: 140px;
      height: 50px;
      text-align: center;
      line-height: 50px;
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
      color: #fff;
      background: #2f3036;
      text-decoration: none;
    }
    /*Hover state for top level links*/
    
    li:hover a {
      background: #19c589;
    }
    /*Style for dropdown links*/
    
    li:hover ul a {
      background: #f3f3f3;
      color: #2f3036;
      height: 40px;
      line-height: 40px;
    }
    /*Hover state for dropdown links*/
    
    li:hover ul a:hover {
      background: #19c589;
      color: #fff;
    }
    /*Hide dropdown links until they are needed*/
    
    li ul {
      display: none;
    }
    /*Make dropdown links vertical*/
    
    li ul li {
      display: block;
      float: none;
    }
    /*Prevent text wrapping*/
    
    li ul li a {
      width: auto;
      min-width: 100px;
      padding-left: 0px;
      padding-right: 0px;
    }
    /*Display the dropdown on hover*/
    
    ul li a:hover+.hidden,
    .hidden:hover {
      display: block;
    }
    /*Style 'show menu' label button and hide it by default*/
    
    .show-menu {
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
      text-decoration: none;
      color: #fff;
      background: #19c589;
      text-align: center;
      padding-left: 0px;
      padding-right: 0px;
      display: none;
    }
    /*Hide checkbox*/
    
    input[type=checkbox] {
      display: none;
    }
    /*Show menu when invisible checkbox is checked*/
    
    input[type=checkbox]:checked~#menu {
      display: block;
    }
    /*Responsive Styles*/
    
    @media screen and (max-width: 760px) {
      /*Make dropdown links appear inline*/
      nav ul#menu {
        position: static;
        display: none;
      }
      /*Create vertical spacing*/
      li {
        margin-bottom: 1px;
      }
      /*Make all menu links full width*/
      ul li,
      li a {
        width: 100%;
      }
      /*Display 'show menu' link*/
      .show-menu {
        display: block;
      }
    }
    
    .hero-image {
      /* The image used */
      /* Set a specific height */
      height: 50%;
      width: 100%;
      /* Position and center the image to scale nicely on all screens */
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      position: relative;
    }
  </style>
</head>

<body>

  <nav>
    <div class="banner animated"><img class="hero-image" src="https://picsum.photos/1080/200/?random"></div>
    <div class="nav-bar"> <label for="show-menu" class="show-menu">Show Menu</label>
      <input type="checkbox" id="show-menu" role="button">
      <ul id="menu">
        <li><a href="#">Home</a></li>
        <li>
          <a href="#">About</a>
        </li>
        <li>
          <a href="#">Portfolio</a>
        </li>
        <li><a href="#">News</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </nav>
</body>

</html>

*全屏查看。

有什么办法可以解决这个问题吗?先感谢您! 正如我尝试添加 padding-top: 0px;到导航栏,这样它就可以粘在横幅上,但它不起作用。我的问题不是重复的,因为其他问题的问题是在不同的情况下。

【问题讨论】:

标签: html css


【解决方案1】:

你需要做两件事。 1) 将图像上的垂直对齐设置为顶部,以便消除为下降文本元素保留的间隙,以及 2) 删除 &lt;ul&gt;

上的边距

@import 'https://fonts.googleapis.com/css?family=Work+Sans:400,500,900';
nav {
  padding-top: 0px;
  position: fixed;
  width: 100%;
  transition: top 0.2s ease-out;
}

.banner {
  text-align: center;
  width: 100%;
  box-shadow: inset 0px -1px 0px 0px rgba(0, 0, 0, 0.13);
}

nav ul#menu {
  padding-left: 0;
  display: flex;
  padding-top: 0px;
}

nav ul li {
  flex-grow: 1;
}

.nav-bar {
  /* Rectangle 1: */
  background: #FFFFFF;
  box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.11), 0px 4px 6px 0px rgba(0, 0, 0, 0.11);
  width: 100%;
  text-align: center;
}

//-------------------------------------------------------

/*Strip the ul of padding and list styling*/

ul {
  list-style-type: none;
  margin: 0;
  position: absolute;
}


/*Create a horizontal list with spacing*/

li {
  display: inline-block;
  float: center;
  margin-right: 1px;
}


/*Style for menu links*/

li a {
  display: block;
  min-width: 140px;
  height: 50px;
  text-align: center;
  line-height: 50px;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #fff;
  background: #2f3036;
  text-decoration: none;
}


/*Hover state for top level links*/

li:hover a {
  background: #19c589;
}


/*Style for dropdown links*/

li:hover ul a {
  background: #f3f3f3;
  color: #2f3036;
  height: 40px;
  line-height: 40px;
}


/*Hover state for dropdown links*/

li:hover ul a:hover {
  background: #19c589;
  color: #fff;
}


/*Hide dropdown links until they are needed*/

li ul {
  display: none;
}


/*Make dropdown links vertical*/

li ul li {
  display: block;
  float: none;
}


/*Prevent text wrapping*/

li ul li a {
  width: auto;
  min-width: 100px;
  padding-left: 0px;
  padding-right: 0px;
}


/*Display the dropdown on hover*/

ul li a:hover+.hidden,
.hidden:hover {
  display: block;
}


/*Style 'show menu' label button and hide it by default*/

.show-menu {
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  text-decoration: none;
  color: #fff;
  background: #19c589;
  text-align: center;
  padding-left: 0px;
  padding-right: 0px;
  display: none;
}


/*Hide checkbox*/

input[type=checkbox] {
  display: none;
}


/*Show menu when invisible checkbox is checked*/

input[type=checkbox]:checked~#menu {
  display: block;
}


/*Responsive Styles*/

@media screen and (max-width: 760px) {
  /*Make dropdown links appear inline*/
  nav ul#menu {
    position: static;
    display: none;
  }
  /*Create vertical spacing*/
  li {
    margin-bottom: 1px;
  }
  /*Make all menu links full width*/
  ul li,
  li a {
    width: 100%;
  }
  /*Display 'show menu' link*/
  .show-menu {
    display: block;
  }
}

.hero-image {
  /* The image used */
  /* Set a specific height */
  height: 50%;
  width: 100%;
  /* Position and center the image to scale nicely on all screens */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
  vertical-align:top;
}

#menu {
margin:0;
}
<nav>
  <div class="banner animated"><img class="hero-image" src="https://picsum.photos/1080/200/?random"></div>
  <div class="nav-bar"> <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
    <ul id="menu">
      <li><a href="#">Home</a></li>
      <li>
        <a href="#">About</a>
      </li>
      <li>
        <a href="#">Portfolio</a>
      </li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact</a></li>
    </ul>
  </div>
</nav>

【讨论】:

    【解决方案2】:

    你可以添加

    img.hero-image {
        float: left;
    }
    

    img.hero-image {
        float: left;
    }
    <!doctype html>
    <html>
    
    <head>
      <meta charset="UTF-8">
      <title>Untitled Document</title>
      <style>
        @import 'https://fonts.googleapis.com/css?family=Work+Sans:400,500,900';
        nav {
          padding-top: 0px;
          position: fixed;
          width: 100%;
          transition: top 0.2s ease-out;
        }
        
        .banner {
          text-align: center;
          width: 100%;
          box-shadow: inset 0px -1px 0px 0px rgba(0, 0, 0, 0.13);
        }
        
        nav ul#menu {
          padding-left: 0;
          display: flex;
          padding-top: 0px;
        }
        
        nav ul li {
          flex-grow: 1;
        }
        
        .nav-bar {
          /* Rectangle 1: */
          background: #FFFFFF;
          box-shadow: 0px 2px 2px 0px rgba(0, 0, 0, 0.11), 0px 4px 6px 0px rgba(0, 0, 0, 0.11);
          width: 100%;
          text-align: center;
        }
        
        //-------------------------------------------------------
        /*Strip the ul of padding and list styling*/
        
        ul {
          list-style-type: none;
          margin: 0;
          position: absolute;
        }
        /*Create a horizontal list with spacing*/
        
        li {
          display: inline-block;
          float: center;
          margin-right: 1px;
        }
        /*Style for menu links*/
        
        li a {
          display: block;
          min-width: 140px;
          height: 50px;
          text-align: center;
          line-height: 50px;
          font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
          color: #fff;
          background: #2f3036;
          text-decoration: none;
        }
        /*Hover state for top level links*/
        
        li:hover a {
          background: #19c589;
        }
        /*Style for dropdown links*/
        
        li:hover ul a {
          background: #f3f3f3;
          color: #2f3036;
          height: 40px;
          line-height: 40px;
        }
        /*Hover state for dropdown links*/
        
        li:hover ul a:hover {
          background: #19c589;
          color: #fff;
        }
        /*Hide dropdown links until they are needed*/
        
        li ul {
          display: none;
        }
        /*Make dropdown links vertical*/
        
        li ul li {
          display: block;
          float: none;
        }
        /*Prevent text wrapping*/
        
        li ul li a {
          width: auto;
          min-width: 100px;
          padding-left: 0px;
          padding-right: 0px;
        }
        /*Display the dropdown on hover*/
        
        ul li a:hover+.hidden,
        .hidden:hover {
          display: block;
        }
        /*Style 'show menu' label button and hide it by default*/
        
        .show-menu {
          font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
          text-decoration: none;
          color: #fff;
          background: #19c589;
          text-align: center;
          padding-left: 0px;
          padding-right: 0px;
          display: none;
        }
        /*Hide checkbox*/
        
        input[type=checkbox] {
          display: none;
        }
        /*Show menu when invisible checkbox is checked*/
        
        input[type=checkbox]:checked~#menu {
          display: block;
        }
        /*Responsive Styles*/
        
        @media screen and (max-width: 760px) {
          /*Make dropdown links appear inline*/
          nav ul#menu {
            position: static;
            display: none;
          }
          /*Create vertical spacing*/
          li {
            margin-bottom: 1px;
          }
          /*Make all menu links full width*/
          ul li,
          li a {
            width: 100%;
          }
          /*Display 'show menu' link*/
          .show-menu {
            display: block;
          }
        }
        
        .hero-image {
          /* The image used */
          /* Set a specific height */
          height: 50%;
          width: 100%;
          /* Position and center the image to scale nicely on all screens */
          background-position: center;
          background-repeat: no-repeat;
          background-size: cover;
          position: relative;
        }
      </style>
    </head>
    
    <body>
    
      <nav>
        <div class="banner animated"><img class="hero-image" src="https://picsum.photos/1080/200/?random"></div>
        <div class="nav-bar"> <label for="show-menu" class="show-menu">Show Menu</label>
          <input type="checkbox" id="show-menu" role="button">
          <ul id="menu">
            <li><a href="#">Home</a></li>
            <li>
              <a href="#">About</a>
            </li>
            <li>
              <a href="#">Portfolio</a>
            </li>
            <li><a href="#">News</a></li>
            <li><a href="#">Contact</a></li>
          </ul>
        </div>
      </nav>
    </body>
    
    </html>

    【讨论】:

    • 当我全屏显示您的示例时,菜单消失
    【解决方案3】:

    将此添加到您的样式中以删除默认边距和填充。

    * {
        padding: 0px;
        margin: 0px;
    }
    

    【讨论】:

    • 嗯...这是什么意思?
    【解决方案4】:

    ul 是罪魁祸首。您需要将 margin-top 添加到 0 到您的 ul 菜单选择器。

     nav ul#menu {
            padding-left: 0;
            display: flex;
            padding-top: 0px;
            margin-top: 0;
        }
    

    如@j08691 所述,要消除微小间隙,请将vertical-align: top; 添加到您的hero-image 类中。

    【讨论】:

      猜你喜欢
      • 2018-02-07
      • 2017-05-16
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多