简体   繁体   English

如何在导航(HTML / CSS)中删除列表项之间的空间

[英]How to remove space between list items in navigation (HTML / CSS)

I'm having trouble removing the space between the li items in navigation, I already set the margin: 0px for the item & anchor (a link) but the space/gap still exist. 我在删除导航中li项之间的空间时遇到问题,我已经设置了页边距:0 px用于项和锚点(链接),但是空间/间隙仍然存在。

How can I remove those spaces? 如何删除这些空格?

在此处输入图片说明

 /* navigation styles */ nav { background: rgba(6, 19, 72, 1); background: linear-gradient(to bottom, rgba(6, 19, 72, 1) 0%, rgba(15, 31, 91, 1) 100%); } .nav { list-style: none; margin: 0; padding: 0; text-align: center; } .nav li { display: inline; margin: 0px; } nav ul.nav { width: 1120px; margin: 0 auto; min-width: 120px; } span.homeicon { width: 35px; height: 32px; display: inline-block; vertical-align: middle; position: relative; background-image: url('http://s16.postimg.org/cq68hbikx/home_icon.png'); background-size: cover; } .nav a { display: inline-block; padding: 10px; width: 120px; text-decoration: none; color: white; font-family: arial; line-height: 30px; height: 30px; margin: 0px; border: 1px solid #344da7; border-top: none; } a.nav_home { max-width: 50px; width: 50px !important; } .nav a:hover { background-color: #344da7; height: 100%; } 
 <nav> <ul class="nav"> <li><a href="" class="nav_home"><span class="homeicon"></span></a></li> <li><a href="">SPORTS</a></li> <li><a href="">LIVE CASINO</a></li> <li><a href="">SLOTS</a></li> <li><a href="">POKER</a></li> <li><a href="">PROMOTION</a></li> <li><a href="">BANKING</a></li> <li><a href="">AFFILIATE</a></li> </ul> </nav> 

fiddle 小提琴

These spaces are actually caused by the white space in your html. 这些空格实际上是由html中的空格引起的。

To solve, add float: left to your <li> tags: 要解决此问题,请在<li>标签中添加float: left ::

.nav li {
    float: left;
}

To see that it really is the whitespace in your HTML, try removing it and testing: 要查看它确实是HTML中的空白,请尝试将其删除并进行测试:

 .nav { list-style: none; margin: 0; padding: 0; text-align: center; } .nav li { display: inline; margin: 0px; } nav ul.nav { width: 1120px; margin: 0 auto; min-width: 120px; } span.homeicon { width: 35px; height: 32px; display: inline-block; vertical-align: middle; position: relative; background-image: url('http://s16.postimg.org/cq68hbikx/home_icon.png'); background-size: cover; } .nav a { display: inline-block; padding: 10px; width: 120px; text-decoration: none; font-family: arial; line-height: 30px; height: 30px; margin: 0px; border: 1px solid #344da7; } a.nav_home { max-width: 50px; width: 50px !important; } .nav a:hover { background-color: #344da7; height: 100%; } 
 <nav> <h1> With Whitespace </h1> <ul class="nav"> <li><a href="" class="nav_home"><span class="homeicon"></span></a></li> <li><a href="">SPORTS</a></li> <li><a href="">LIVE CASINO</a></li> <li><a href="">SLOTS</a></li> <li><a href="">POKER</a></li> <li><a href="">PROMOTION</a></li> <li><a href="">BANKING</a></li> <li><a href="">AFFILIATE</a></li> </ul> <h1> Without Whitespace </h1> <ul class="nav"> <li><a href="" class="nav_home"><span class="homeicon"></span></a></li><li><a href="">SPORTS</a></li><li><a href="">LIVE CASINO</a></li><li><a href="">SLOTS</a></li><li><a href="">POKER</a></li><li><a href="">PROMOTION</a></li><li><a href="">BANKING</a></li><li><a href="">AFFILIATE</a></li> </ul> </nav> 

The spaces are caused by white spaces. 空格是由空格引起的。 You should make sure that there are no spaces in between each li . 您应该确保每个li之间没有空格。

The best way to do so IMO: IMO的最佳方法是:

<ul class="nav"><!--
    --><li><a href="" class="nav_home"><span class="homeicon"></span></a></li><!--
    --><li><a href="">SPORTS</a></li><!--
    --><li><a href="">LIVE CASINO</a></li><!--
    --><li><a href="">SLOTS</a></li><!--
    --><li><a href="">POKER</a></li><!--
    --><li><a href="">PROMOTION</a></li><!--
    --><li><a href="">BANKING</a></li><!--
    --><li><a href="">AFFILIATE</a></li>
</ul>

Sure, you can change your entire layout to using floats, but that is really not the best way, and kind of going backwards. 当然,您可以将整个布局更改为使用浮点数,但这实际上不是最佳方法,并且有些倒退。 inline-block was created specifically to address the problems with using floats for layout. inline-block专门用于解决使用浮点数进行布局的问题。

To remove the space between the navigation items, just do this: 要删除导航项目之间的空间,只需执行以下操作:

ul {
    font-size: 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM