html - 导航元素之间的随机间隙

标签 html css

我正在尝试创建一个导航栏,但是当我运行我的元素时,导航元素之间会出现随机线条。像这样:enter image description here

元素: https://codepen.io/anon/pen/jKWbRv

如您所见,我使用 HTML 和 CSS 对此进行了编程,但是,我无法删除这些行,并且在最后一个 Contact 元素处也有一个不必要的行。

@import 'https://fonts.googleapis.com/css?family=Work+Sans:400,500,900';
body {
  margin: 0px 0px 0px 0px;
}

nav {
  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;
}

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;
}


/*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: 0px;
  }
  /*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;
}
<html>

<head>
  <meta charset="UTF-8">
  <title>Untitled Document</title>

</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>

这不是 How do I remove the space between inline-block elements? 的副本因为我已经尝试过任何一种解决方案,但它仍然没有奏效。

最佳答案

你有这一堆 CSS 代码,

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

删除 margin-right:1px 并删除无效的 float: center; - 这不是有效的 float 属性 - 请参阅 float 的文档, 做成这样

li {
  display: inline-block;
}

例子:

<!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';
    body {
      margin: 0px 0px 0px 0px;
    }
    
    nav {
      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;
    }
    
    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;
    }
    /*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;
    }
    /*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: 0px;
      }
      /*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;
    }
  </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>

关于html - 导航元素之间的随机间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50667096/

相关文章:

html - 表格显示问题,仅在 Chrome 中

javascript - PhantomJS - 获取元素计算样式

html - 取html整页

jquery - Bootstrap 中中间列的隐藏垂直滚动条

javascript - 现代浏览器中的多语言

html - div引导后台位置

html - 六边形 Angular 上的文字

javascript - 在同一个 CSS 水平菜单中组合复选框和按钮

html - 有什么方法可以从 EPUB 中检测 iBooks 中的深色主题是否处于事件状态?

html - 在div标签右侧显示文字