【问题标题】:hide spaces in navbar [duplicate]在导航栏中隐藏空格[重复]
【发布时间】:2017-05-29 11:27:49
【问题描述】:

我可以删除导航按钮之间的空格,使它们居中吗?我尝试在li a 中删除inline-block,但我认为它有问题,我认为是display: inline-block 的问题,但我不确定......

有人可以帮助我吗?提前致谢。

nav ul {
  text-align: center;
  margin: 0px;
  padding: 0px;
  position: fixed;
  width: 100%;
  height: auto;
  background: #b2bac9;
  color: #090a0d; }
  nav ul li {
    margin: 0px;
    padding: 0px;
    display: inline; }
    nav ul li a {
      text-decoration: none;
      display: inline-block;
      padding: 16px;
      text-decoration: none;
      color: #fff;
      background: red; }
      nav ul li a:hover {
        background: #949fb4; }
#tit {
  position: absolute;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title> nav </title>
  </head>
  <body>
    <header>
      <nav>
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">The Project</a></li>
          <li><a href="#">Forum</a></li>
        </ul>
      </nav>
      
      <h1 id="tit" align="center"> I need to remove the balnk space between red buttons </h1>
    </header>
  </body>
</html>

【问题讨论】:

    标签: css sass styles less


    【解决方案1】:

    发生这种情况的原因是,当使用带有inline-block 的元素时,它们的处理方式与文本中的单词相同。然后元素之间的换行符和制表符将计为空格。

    要修复它,只需将nav ul 设置为display: table:

    nav ul {
       display: table;
       text-align: center;
       margin: 0px;
       padding: 0px;
       position: fixed;
       width: 100%;
       height: auto;
       background: #b2bac9;
       color: #090a0d;
    }
    nav ul li {
       margin: 0px;
       padding: 0px;
       display: inline;
    }
    nav ul li a {
       text-decoration: none;
       display: inline-block;
       padding: 16px;
       text-decoration: none;
       color: #fff;
       background: red;
    }
    nav ul li a:hover {
       background: #949fb4;
    }
    #tit {
       position: absolute;
       top: 100px;
       left: 50%;
       transform: translateX(-50%);
    }
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title> nav </title>
      </head>
      <body>
        <header>
          <nav>
            <ul>
              <li><a href="#">Home</a></li>
              <li><a href="#">The Project</a></li>
              <li><a href="#">Forum</a></li>
            </ul>
          </nav>
          
          <h1 id="tit" align="center"> I need to remove the balnk space between red buttons </h1>
        </header>
      </body>
    </html>

    您还可以删除元素之间的所有空格、换行符和制表符(我不建议这样做),或者像这样使用带有justify-content: center 的 Flexbox:

    nav ul {
       display: flex;
       justify-content: center;
       margin: 0px;
       padding: 0px;
       position: fixed;
       width: 100%;
       height: auto;
       background: #b2bac9;
       color: #090a0d;
    }
    

    您可以阅读更多关于她的信息:CSS-Tricks: Fighting the Space Between Inline Block Elements

    【讨论】:

      【解决方案2】:

      处理这个问题有一个奇怪的小技巧,只需删除 &lt;li&gt; 元素中的空格即可:

      <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
          <title> nav </title>
        </head>
        <body>
          <header>
            <nav>
              <ul>
                <li><a href="#">Home</a></li><!--
                --><li><a href="#">The Project</a></li><!--
                --><li><a href="#">Forum</a></li>
              </ul>
            </nav>
      
            <h1 id="tit" align="center"> I need to remove the balnk space between red buttons </h1>
          </header>
        </body>
      </html>
      

      See JSFiddle

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 1970-01-01
        • 2015-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多