-1

Can i remove blank spaces between nav buttons, manteining them centered? I've tried removing inline-block in li a but i think something it's wrong, i think is the display: inline-block the problem but i'm not sure...

Can someone help me? Thanks in advance.

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>
Federico
  • 380
  • 3
  • 15

2 Answers2

1

The reason why it happens is because, when using elements with inline-block they are treated the same way as words in a text. Then the line-breaks and tabs you have between the elements will count as spaces.

To fix it, simply set nav ul to 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>

You can also remove all the spaces, line-breaks and tabs, between your elements (which I wouldn't recomment), or use Flexbox with justify-content: center like this:

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

You can read more about it her: CSS-Tricks: Fighting the Space Between Inline Block Elements

TheYaXxE
  • 4,196
  • 3
  • 22
  • 34
1

There's a weird little trick for dealing with this issue, simply remove the spaces in your <li> elements:

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

bbailes
  • 371
  • 1
  • 9